From d09854a7220d9585d10977f1aff7326032014a6c Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 21 Jun 2024 17:39:40 -0400 Subject: [PATCH] Adding a show_read override to GetPosts. (#4846) * Adding a show_read override to GetPosts. - If show_read is true, it overrides the local user show_read setting. - Fixes #4124 * Addressing PR comments. * Update crates/db_views/src/post_view.rs Co-authored-by: dullbananas * Fixing formatting. --------- Co-authored-by: dullbananas --- crates/api_common/src/post.rs | 2 + crates/apub/src/api/list_comments.rs | 8 ++-- crates/apub/src/api/list_posts.rs | 12 +++--- crates/apub/src/api/read_person.rs | 4 +- crates/db_views/src/comment_view.rs | 22 +++++------ crates/db_views/src/post_view.rs | 56 ++++++++++++++++++++-------- 6 files changed, 66 insertions(+), 38 deletions(-) diff --git a/crates/api_common/src/post.rs b/crates/api_common/src/post.rs index 3d1bc4078..f42a468b5 100644 --- a/crates/api_common/src/post.rs +++ b/crates/api_common/src/post.rs @@ -79,6 +79,8 @@ pub struct GetPosts { pub liked_only: Option, pub disliked_only: Option, pub show_hidden: Option, + /// If true, then show the read posts (even if your user setting is to hide them) + pub show_read: Option, pub page_cursor: Option, } diff --git a/crates/apub/src/api/list_comments.rs b/crates/apub/src/api/list_comments.rs index d83b9a135..12d18110e 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -37,11 +37,11 @@ pub async fn list_comments( }; let sort = data.sort; let max_depth = data.max_depth; - let saved_only = data.saved_only.unwrap_or_default(); + let saved_only = data.saved_only; - let liked_only = data.liked_only.unwrap_or_default(); - let disliked_only = data.disliked_only.unwrap_or_default(); - if liked_only && disliked_only { + let liked_only = data.liked_only; + let disliked_only = data.disliked_only; + if liked_only.unwrap_or_default() && disliked_only.unwrap_or_default() { return Err(LemmyError::from(LemmyErrorType::ContradictingFilters)); } diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index 31bdd0c8e..c00c87f4c 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -40,12 +40,13 @@ pub async fn list_posts( } else { data.community_id }; - let saved_only = data.saved_only.unwrap_or_default(); - let show_hidden = data.show_hidden.unwrap_or_default(); + let saved_only = data.saved_only; + let show_hidden = data.show_hidden; + let show_read = data.show_read; - let liked_only = data.liked_only.unwrap_or_default(); - let disliked_only = data.disliked_only.unwrap_or_default(); - if liked_only && disliked_only { + let liked_only = data.liked_only; + let disliked_only = data.disliked_only; + if liked_only.unwrap_or_default() && disliked_only.unwrap_or_default() { return Err(LemmyError::from(LemmyErrorType::ContradictingFilters)); } @@ -82,6 +83,7 @@ pub async fn list_posts( page_after, limit, show_hidden, + show_read, ..Default::default() } .list(&local_site.site, &mut context.pool()) diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index a6b3560aa..149e06a17 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -55,11 +55,11 @@ pub async fn read_person( let sort = data.sort; let page = data.page; let limit = data.limit; - let saved_only = data.saved_only.unwrap_or_default(); + let saved_only = data.saved_only; let community_id = data.community_id; // If its saved only, you don't care what creator it was // Or, if its not saved, then you only want it for that specific creator - let creator_id = if !saved_only { + let creator_id = if !saved_only.unwrap_or_default() { Some(person_details_id) } else { None diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 61dbceb4b..cd7560a00 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -252,7 +252,7 @@ fn queries<'a>() -> Queries< } // If its saved only, then filter, and order by the saved time, not the comment creation time. - if options.saved_only { + if options.saved_only.unwrap_or_default() { query = query .filter(comment_saved::person_id.is_not_null()) .then_order_by(comment_saved::published.desc()); @@ -260,9 +260,9 @@ fn queries<'a>() -> Queries< if let Some(my_id) = options.local_user.person_id() { let not_creator_filter = comment::creator_id.ne(my_id); - if options.liked_only { + if options.liked_only.unwrap_or_default() { query = query.filter(not_creator_filter).filter(score(my_id).eq(1)); - } else if options.disliked_only { + } else if options.disliked_only.unwrap_or_default() { query = query.filter(not_creator_filter).filter(score(my_id).eq(-1)); } } @@ -398,9 +398,9 @@ pub struct CommentQuery<'a> { pub creator_id: Option, pub local_user: Option<&'a LocalUser>, pub search_term: Option, - pub saved_only: bool, - pub liked_only: bool, - pub disliked_only: bool, + pub saved_only: Option, + pub liked_only: Option, + pub disliked_only: Option, pub page: Option, pub limit: Option, pub max_depth: Option, @@ -711,8 +711,8 @@ mod tests { CommentLike::like(pool, &comment_like_form).await?; let read_liked_comment_views = CommentQuery { - local_user: (Some(&data.timmy_local_user_view.local_user)), - liked_only: (true), + local_user: Some(&data.timmy_local_user_view.local_user), + liked_only: Some(true), ..Default::default() } .list(pool) @@ -727,8 +727,8 @@ mod tests { assert_length!(1, read_liked_comment_views); let read_disliked_comment_views: Vec = CommentQuery { - local_user: (Some(&data.timmy_local_user_view.local_user)), - disliked_only: (true), + local_user: Some(&data.timmy_local_user_view.local_user), + disliked_only: Some(true), ..Default::default() } .list(pool) @@ -980,7 +980,7 @@ mod tests { // Fetch the saved comments let comments = CommentQuery { local_user: Some(&data.timmy_local_user_view.local_user), - saved_only: true, + saved_only: Some(true), ..Default::default() } .list(pool) diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index d472fc604..0e22f689b 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -403,14 +403,17 @@ fn queries<'a>() -> Queries< }; // If its saved only, then filter, and order by the saved time, not the comment creation time. - if options.saved_only { + if options.saved_only.unwrap_or_default() { query = query .filter(post_saved::person_id.is_not_null()) .then_order_by(post_saved::published.desc()); } // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read // setting wont be able to see saved posts. - else if !options.local_user.show_read_posts() { + else if !options + .show_read + .unwrap_or(options.local_user.show_read_posts()) + { // Do not hide read posts when it is a user profile view // Or, only hide read posts on non-profile views if let (None, Some(person_id)) = (options.creator_id, options.local_user.person_id()) { @@ -418,7 +421,7 @@ fn queries<'a>() -> Queries< } } - if !options.show_hidden { + if !options.show_hidden.unwrap_or_default() { // If a creator id isn't given (IE its on home or community pages), hide the hidden posts if let (None, Some(person_id)) = (options.creator_id, options.local_user.person_id()) { query = query.filter(not(is_hidden(person_id))); @@ -427,9 +430,9 @@ fn queries<'a>() -> Queries< if let Some(my_id) = options.local_user.person_id() { let not_creator_filter = post_aggregates::creator_id.ne(my_id); - if options.liked_only { + if options.liked_only.unwrap_or_default() { query = query.filter(not_creator_filter).filter(score(my_id).eq(1)); - } else if options.disliked_only { + } else if options.disliked_only.unwrap_or_default() { query = query.filter(not_creator_filter).filter(score(my_id).eq(-1)); } }; @@ -476,7 +479,7 @@ fn queries<'a>() -> Queries< let page_after = options.page_after.map(|c| c.0); let page_before_or_equal = options.page_before_or_equal.map(|c| c.0); - if options.page_back { + if options.page_back.unwrap_or_default() { query = query .before(page_after) .after_or_equal(page_before_or_equal) @@ -604,15 +607,16 @@ pub struct PostQuery<'a> { pub local_user: Option<&'a LocalUser>, pub search_term: Option, pub url_search: Option, - pub saved_only: bool, - pub liked_only: bool, - pub disliked_only: bool, + pub saved_only: Option, + pub liked_only: Option, + pub disliked_only: Option, pub page: Option, pub limit: Option, pub page_after: Option, pub page_before_or_equal: Option, - pub page_back: bool, - pub show_hidden: bool, + pub page_back: Option, + pub show_hidden: Option, + pub show_read: Option, } impl<'a> PostQuery<'a> { @@ -683,7 +687,7 @@ impl<'a> PostQuery<'a> { if (v.len() as i64) < limit { Ok(Some(self.clone())) } else { - let item = if self.page_back { + let item = if self.page_back.unwrap_or_default() { // for backward pagination, get first element instead v.into_iter().next() } else { @@ -1122,7 +1126,7 @@ mod tests { // Read the liked only let read_liked_post_listing = PostQuery { community_id: Some(data.inserted_community.id), - liked_only: true, + liked_only: Some(true), ..data.default_post_query() } .list(&data.site, pool) @@ -1133,7 +1137,7 @@ mod tests { let read_disliked_post_listing = PostQuery { community_id: Some(data.inserted_community.id), - disliked_only: true, + disliked_only: Some(true), ..data.default_post_query() } .list(&data.site, pool) @@ -1459,7 +1463,7 @@ mod tests { loop { let post_listings = PostQuery { page_after: page_before, - page_back: true, + page_back: Some(true), ..options.clone() } .list(&data.site, pool) @@ -1517,6 +1521,26 @@ mod tests { let post_listings_hide_read = data.default_post_query().list(&data.site, pool).await?; assert_eq!(vec![POST], names(&post_listings_hide_read)); + // Test with the show_read override as true + let post_listings_show_read_true = PostQuery { + show_read: Some(true), + ..data.default_post_query() + } + .list(&data.site, pool) + .await?; + assert_eq!( + vec![POST_BY_BOT, POST], + names(&post_listings_show_read_true) + ); + + // Test with the show_read override as false + let post_listings_show_read_false = PostQuery { + show_read: Some(false), + ..data.default_post_query() + } + .list(&data.site, pool) + .await?; + assert_eq!(vec![POST], names(&post_listings_show_read_false)); cleanup(data, pool).await } @@ -1543,7 +1567,7 @@ mod tests { let post_listings_show_hidden = PostQuery { sort: Some(SortType::New), local_user: Some(&data.local_user_view.local_user), - show_hidden: true, + show_hidden: Some(true), ..Default::default() } .list(&data.site, pool)