From 4b6e964e7096f29aceb9f3034df506eea133befb Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 30 Apr 2024 08:42:57 -0400 Subject: [PATCH] Dont show own comments for liked and disliked_only. Fixes #4675 --- crates/db_views/src/comment_view.rs | 61 +++++++++++++++++++++++------ crates/db_views/src/post_view.rs | 48 +++++++++++++++++++---- 2 files changed, 88 insertions(+), 21 deletions(-) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index ec4faf0e7..f6c443dcb 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -264,10 +264,13 @@ fn queries<'a>() -> Queries< .then_order_by(is_saved(person_id_join).desc()); } - if options.liked_only { - query = query.filter(score(person_id_join).eq(1)); - } else if options.disliked_only { - query = query.filter(score(person_id_join).eq(-1)); + if let Some(my_id) = my_person_id { + let not_creator_filter = comment::creator_id.ne(my_id); + if options.liked_only { + query = query.filter(not_creator_filter).filter(score(my_id).eq(1)); + } else if options.disliked_only { + query = query.filter(not_creator_filter).filter(score(my_id).eq(-1)); + } } if !options @@ -682,8 +685,10 @@ mod tests { .await?; assert_eq!( - expected_comment_view_no_person, - read_comment_views_no_person[0] + &expected_comment_view_no_person, + read_comment_views_no_person + .first() + .ok_or(LemmyErrorType::CouldntFindComment)? ); let read_comment_views_with_person = CommentQuery { @@ -714,18 +719,45 @@ mod tests { // Make sure block set the creator blocked assert!(read_comment_from_blocked_person.creator_blocked); + cleanup(data, pool).await + } + + #[tokio::test] + #[serial] + async fn test_liked_only() -> LemmyResult<()> { + let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); + let data = init_data(pool).await; + + // Unblock sara first + let timmy_unblocks_sara_form = PersonBlockForm { + person_id: data.timmy_local_user_view.person.id, + target_id: data.inserted_sara_person.id, + }; + PersonBlock::unblock(pool, &timmy_unblocks_sara_form).await?; + + // Like a new comment + let comment_like_form = CommentLikeForm { + comment_id: data.inserted_comment_1.id, + post_id: data.inserted_post.id, + person_id: data.timmy_local_user_view.person.id, + score: 1, + }; + CommentLike::like(pool, &comment_like_form).await.unwrap(); + let read_liked_comment_views = CommentQuery { local_user: (Some(&data.timmy_local_user_view)), liked_only: (true), ..Default::default() } .list(pool) - .await?; + .await? + .into_iter() + .map(|c| c.comment.content) + .collect::>(); - assert_eq!( - expected_comment_view_with_person, - read_liked_comment_views[0] - ); + // Shouldn't include your own post, only other peoples + assert_eq!(data.inserted_comment_1.content, read_liked_comment_views[0]); assert_length!(1, read_liked_comment_views); @@ -835,7 +867,7 @@ mod tests { // change user lang to finnish, should only show one post in finnish and one undetermined let finnish_id = Language::read_id_from_code(pool, Some("fi")) .await? - .unwrap(); + .ok_or(LemmyErrorType::LanguageNotAllowed)?; LocalUserLanguage::update( pool, vec![finnish_id], @@ -855,7 +887,10 @@ mod tests { assert!(finnish_comment.is_some()); assert_eq!( data.inserted_comment_2.content, - finnish_comment.unwrap().comment.content + finnish_comment + .ok_or(LemmyErrorType::CouldntFindComment)? + .comment + .content ); // now show all comments with undetermined language (which is the default value) diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 4c7ecd088..9f3a29411 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -452,11 +452,12 @@ fn queries<'a>() -> Queries< } } - if let Some(person_id) = my_person_id { + if let Some(my_id) = my_person_id { + let not_creator_filter = post_aggregates::creator_id.ne(my_id); if options.liked_only { - query = query.filter(score(person_id).eq(1)); + query = query.filter(not_creator_filter).filter(score(my_id).eq(1)); } else if options.disliked_only { - query = query.filter(score(person_id).eq(-1)); + query = query.filter(not_creator_filter).filter(score(my_id).eq(-1)); } }; @@ -1121,6 +1122,36 @@ mod tests { .await?; assert_eq!(vec![expected_post_with_upvote], read_post_listing); + let like_removed = + PostLike::remove(pool, data.local_user_view.person.id, data.inserted_post.id).await?; + assert_eq!(1, like_removed); + cleanup(data, pool).await + } + + #[tokio::test] + #[serial] + async fn post_listing_liked_only() -> LemmyResult<()> { + let pool = &build_db_pool().await?; + let pool = &mut pool.into(); + let data = init_data(pool).await?; + + // Like both the bot post, and your own + // The liked_only should not show your own post + let post_like_form = PostLikeForm { + post_id: data.inserted_post.id, + person_id: data.local_user_view.person.id, + score: 1, + }; + PostLike::like(pool, &post_like_form).await?; + + let bot_post_like_form = PostLikeForm { + post_id: data.inserted_bot_post.id, + person_id: data.local_user_view.person.id, + score: 1, + }; + PostLike::like(pool, &bot_post_like_form).await?; + + // Read the liked only let read_liked_post_listing = PostQuery { community_id: Some(data.inserted_community.id), liked_only: true, @@ -1128,7 +1159,9 @@ mod tests { } .list(&data.site, pool) .await?; - assert_eq!(read_post_listing, read_liked_post_listing); + + // This should only include the bot post, not the one you created + assert_eq!(vec![POST_BY_BOT], names(&read_liked_post_listing)); let read_disliked_post_listing = PostQuery { community_id: Some(data.inserted_community.id), @@ -1137,11 +1170,10 @@ mod tests { } .list(&data.site, pool) .await?; + + // Should be no posts assert_eq!(read_disliked_post_listing, vec![]); - let like_removed = - PostLike::remove(pool, data.local_user_view.person.id, data.inserted_post.id).await?; - assert_eq!(1, like_removed); cleanup(data, pool).await } @@ -1554,7 +1586,7 @@ mod tests { assert!( &post_listings_show_hidden .first() - .expect("first post should exist") + .ok_or(LemmyErrorType::CouldntFindPost)? .hidden );