From 2899ba013106a3462d6a4d95eb19bf37ae451117 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 19 Dec 2023 04:46:41 -0500 Subject: [PATCH] Fixing broken post_read logic. Fixes #4290 (#4297) --- crates/db_views/src/post_view.rs | 48 ++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index a40e38480..857e7f644 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -412,7 +412,8 @@ fn queries<'a>() -> Queries< .unwrap_or(true) { // Do not hide read posts when it is a user profile view - if let (Some(_creator_id), Some(person_id)) = (options.creator_id, my_person_id) { + // Or, only hide read posts on non-profile views + if let (None, Some(person_id)) = (options.creator_id, my_person_id) { query = query.filter(not(is_read(person_id))); } } @@ -741,7 +742,7 @@ mod tests { local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm}, person::{Person, PersonInsertForm}, person_block::{PersonBlock, PersonBlockForm}, - post::{Post, PostInsertForm, PostLike, PostLikeForm, PostUpdateForm}, + post::{Post, PostInsertForm, PostLike, PostLikeForm, PostRead, PostUpdateForm}, }, traits::{Blockable, Crud, Joinable, Likeable}, utils::{build_db_pool_for_tests, DbPool, RANK_DEFAULT}, @@ -749,7 +750,7 @@ mod tests { SubscribedType, }; use serial_test::serial; - use std::time::Duration; + use std::{collections::HashSet, time::Duration}; struct Data { inserted_instance: Instance, @@ -1497,6 +1498,47 @@ mod tests { cleanup(data, pool).await; } + #[tokio::test] + #[serial] + async fn post_listings_hide_read() { + let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); + let mut data = init_data(pool).await; + + // Make sure local user hides read posts + let local_user_form = LocalUserUpdateForm { + show_read_posts: Some(false), + ..Default::default() + }; + let inserted_local_user = + LocalUser::update(pool, data.local_user_view.local_user.id, &local_user_form) + .await + .unwrap(); + data.local_user_view.local_user = inserted_local_user; + + // Mark a post as read + PostRead::mark_as_read( + pool, + HashSet::from([data.inserted_bot_post.id]), + data.local_user_view.person.id, + ) + .await + .unwrap(); + + // Make sure you don't see the read post in the results + let post_listings_hide_read = PostQuery { + sort: Some(SortType::New), + local_user: Some(&data.local_user_view), + ..Default::default() + } + .list(pool) + .await + .unwrap(); + assert_eq!(1, post_listings_hide_read.len()); + + cleanup(data, pool).await; + } + async fn cleanup(data: Data, pool: &mut DbPool<'_>) { let num_deleted = Post::delete(pool, data.inserted_post.id).await.unwrap(); Community::delete(pool, data.inserted_community.id)