From f76499674578ba49e2f07e9e9b0cc4488627f12a Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 11 Dec 2023 14:36:12 -0500 Subject: [PATCH] Show federated posts on user profile. Fixes #4228 (#4229) * Show federated posts on user profile. Fixes #4228 * Make sure posts are hidden if no listing_type is given. --- crates/apub/src/api/read_person.rs | 2 - crates/db_views/src/comment_view.rs | 15 ++-- crates/db_views/src/post_view.rs | 106 +++++++++++++++------------- 3 files changed, 67 insertions(+), 56 deletions(-) diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index 26ad287f1..b32fc5b5f 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -65,7 +65,6 @@ pub async fn read_person( saved_only, local_user: local_user_view.as_ref(), community_id, - is_profile_view: true, page, limit, creator_id, @@ -79,7 +78,6 @@ pub async fn read_person( sort: sort.map(post_to_comment_sort_type), saved_only, community_id, - is_profile_view: true, page, limit, creator_id, diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 9d853df3f..9e87d902e 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -177,14 +177,18 @@ fn queries<'a>() -> Queries< }; let list = move |mut conn: DbConn<'a>, options: CommentQuery<'a>| async move { - let person_id = options.local_user.map(|l| l.person.id); - let local_user_id = options.local_user.map(|l| l.local_user.id); + let my_person_id = options.local_user.map(|l| l.person.id); + let my_local_user_id = options.local_user.map(|l| l.local_user.id); // The left join below will return None in this case - let person_id_join = person_id.unwrap_or(PersonId(-1)); - let local_user_id_join = local_user_id.unwrap_or(LocalUserId(-1)); + let person_id_join = my_person_id.unwrap_or(PersonId(-1)); + let local_user_id_join = my_local_user_id.unwrap_or(LocalUserId(-1)); - let mut query = all_joins(comment::table.into_boxed(), person_id, options.saved_only); + let mut query = all_joins( + comment::table.into_boxed(), + my_person_id, + options.saved_only, + ); if let Some(creator_id) = options.creator_id { query = query.filter(comment::creator_id.eq(creator_id)); @@ -373,7 +377,6 @@ pub struct CommentQuery<'a> { pub saved_only: bool, pub liked_only: bool, pub disliked_only: bool, - pub is_profile_view: bool, pub page: Option, pub limit: Option, pub max_depth: Option, diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index fcffe214a..bd768cc38 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -301,16 +301,16 @@ fn queries<'a>() -> Queries< }; let list = move |mut conn: DbConn<'a>, options: PostQuery<'a>| async move { - let person_id = options.local_user.map(|l| l.person.id); - let local_user_id = options.local_user.map(|l| l.local_user.id); + let my_person_id = options.local_user.map(|l| l.person.id); + let my_local_user_id = options.local_user.map(|l| l.local_user.id); // The left join below will return None in this case - let person_id_join = person_id.unwrap_or(PersonId(-1)); - let local_user_id_join = local_user_id.unwrap_or(LocalUserId(-1)); + let person_id_join = my_person_id.unwrap_or(PersonId(-1)); + let local_user_id_join = my_local_user_id.unwrap_or(LocalUserId(-1)); let mut query = all_joins( post_aggregates::table.into_boxed(), - person_id, + my_person_id, options.saved_only, ); @@ -318,7 +318,7 @@ fn queries<'a>() -> Queries< query = query.filter(community::deleted.eq(false)); // only show deleted posts to creator - if let Some(person_id) = person_id { + if let Some(person_id) = my_person_id { query = query.filter(post::deleted.eq(false).or(post::creator_id.eq(person_id))); } else { query = query.filter(post::deleted.eq(false)); @@ -329,7 +329,7 @@ fn queries<'a>() -> Queries< .map(|l| l.local_user.admin) .unwrap_or(false); // only show removed posts to admin when viewing user profile - if !(options.is_profile_view && is_admin) { + if !(options.creator_id.is_some() && is_admin) { query = query .filter(community::removed.eq(false)) .filter(post::removed.eq(false)); @@ -352,41 +352,47 @@ fn queries<'a>() -> Queries< query = query.filter(post_aggregates::creator_id.eq(creator_id)); } - if let Some(person_id) = person_id { - let is_subscribed = exists( - community_follower::table.filter( - post_aggregates::community_id - .eq(community_follower::community_id) - .and(community_follower::person_id.eq(person_id)), - ), - ); - match options.listing_type.unwrap_or_default() { - ListingType::Subscribed => query = query.filter(is_subscribed), - ListingType::Local => { - query = query - .filter(community::local.eq(true)) - .filter(community::hidden.eq(false).or(is_subscribed)); + if let Some(listing_type) = options.listing_type { + if let Some(person_id) = my_person_id { + let is_subscribed = exists( + community_follower::table.filter( + post_aggregates::community_id + .eq(community_follower::community_id) + .and(community_follower::person_id.eq(person_id)), + ), + ); + match listing_type { + ListingType::Subscribed => query = query.filter(is_subscribed), + ListingType::Local => { + query = query + .filter(community::local.eq(true)) + .filter(community::hidden.eq(false).or(is_subscribed)); + } + ListingType::All => query = query.filter(community::hidden.eq(false).or(is_subscribed)), + ListingType::ModeratorView => { + query = query.filter(exists( + community_moderator::table.filter( + post::community_id + .eq(community_moderator::community_id) + .and(community_moderator::person_id.eq(person_id)), + ), + )); + } } - ListingType::All => query = query.filter(community::hidden.eq(false).or(is_subscribed)), - ListingType::ModeratorView => { - query = query.filter(exists( - community_moderator::table.filter( - post::community_id - .eq(community_moderator::community_id) - .and(community_moderator::person_id.eq(person_id)), - ), - )); + } + // If your person_id is missing, only show local + else { + match listing_type { + ListingType::Local => { + query = query + .filter(community::local.eq(true)) + .filter(community::hidden.eq(false)); + } + _ => query = query.filter(community::hidden.eq(false)), } } } else { - match options.listing_type.unwrap_or_default() { - ListingType::Local => { - query = query - .filter(community::local.eq(true)) - .filter(community::hidden.eq(false)); - } - _ => query = query.filter(community::hidden.eq(false)), - } + query = query.filter(community::hidden.eq(false)); } if let Some(url_search) = &options.url_search { @@ -420,7 +426,7 @@ fn queries<'a>() -> Queries< query = query.filter(person::bot_account.eq(false)); }; - if let (true, Some(person_id)) = (options.saved_only, person_id) { + if let (true, Some(person_id)) = (options.saved_only, my_person_id) { query = query.filter(is_saved(person_id)); } // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read @@ -431,12 +437,12 @@ fn queries<'a>() -> Queries< .unwrap_or(true) { // Do not hide read posts when it is a user profile view - if let (false, Some(person_id)) = (options.is_profile_view, person_id) { + if let (Some(_creator_id), Some(person_id)) = (options.creator_id, my_person_id) { query = query.filter(not(is_read(person_id))); } } - if let Some(person_id) = person_id { + if let Some(person_id) = my_person_id { if options.liked_only { query = query.filter(score(person_id).eq(1)); } else if options.disliked_only { @@ -446,7 +452,7 @@ fn queries<'a>() -> Queries< // Dont filter blocks or missing languages for moderator view type if let (Some(person_id), false) = ( - person_id, + my_person_id, options.listing_type.unwrap_or_default() == ListingType::ModeratorView, ) { // Filter out the rows with missing languages @@ -630,7 +636,6 @@ pub struct PostQuery<'a> { pub liked_only: bool, pub disliked_only: bool, pub moderator_view: bool, - pub is_profile_view: bool, pub page: Option, pub limit: Option, pub page_after: Option, @@ -766,6 +771,7 @@ mod tests { inserted_bot: Person, inserted_community: Community, inserted_post: Post, + inserted_bot_post: Post, } async fn init_data(pool: &mut DbPool<'_>) -> Data { @@ -850,7 +856,7 @@ mod tests { .community_id(inserted_community.id) .build(); - let _inserted_bot_post = Post::create(pool, &new_bot_post).await.unwrap(); + let inserted_bot_post = Post::create(pool, &new_bot_post).await.unwrap(); let local_user_view = LocalUserView { local_user: inserted_local_user, person: inserted_person, @@ -864,6 +870,7 @@ mod tests { inserted_bot, inserted_community, inserted_post, + inserted_bot_post, } } @@ -1253,7 +1260,7 @@ mod tests { // Remove the post Post::update( pool, - data.inserted_post.id, + data.inserted_bot_post.id, &PostUpdateForm { removed: Some(true), ..Default::default() @@ -1273,18 +1280,21 @@ mod tests { .unwrap(); assert_eq!(1, post_listings_no_admin.len()); - // Removed post is shown to admins on profile page + // Removed bot post is shown to admins on its profile page data.local_user_view.local_user.admin = true; let post_listings_is_admin = PostQuery { sort: Some(SortType::New), + creator_id: Some(data.inserted_bot.id), local_user: Some(&data.local_user_view), - is_profile_view: true, ..Default::default() } .list(pool) .await .unwrap(); - assert_eq!(2, post_listings_is_admin.len()); + assert_eq!( + data.inserted_bot.id, + post_listings_is_admin[0].post.creator_id + ); cleanup(data, pool).await; }