diff --git a/api_tests/src/comment.spec.ts b/api_tests/src/comment.spec.ts index c3f4b3efe..edc588db7 100644 --- a/api_tests/src/comment.spec.ts +++ b/api_tests/src/comment.spec.ts @@ -156,7 +156,6 @@ test("Delete a comment", async () => { commentRes.comment_view.comment.id, ); expect(deleteCommentRes.comment_view.comment.deleted).toBe(true); - expect(deleteCommentRes.comment_view.comment.content).toBe(""); // Make sure that comment is deleted on beta await waitUntil( @@ -254,7 +253,6 @@ test("Remove a comment from admin and community on different instance", async () betaComment.comment.id, ); expect(removeCommentRes.comment_view.comment.removed).toBe(true); - expect(removeCommentRes.comment_view.comment.content).toBe(""); // Comment text is also hidden from list let listComments = await getComments( @@ -263,7 +261,6 @@ test("Remove a comment from admin and community on different instance", async () ); expect(listComments.comments.length).toBe(1); expect(listComments.comments[0].comment.removed).toBe(true); - expect(listComments.comments[0].comment.content).toBe(""); // Make sure its not removed on alpha let refetchedPostComments = await getComments( diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 1037cf6ff..2cf751f9f 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -316,17 +316,14 @@ impl CommentView { comment_id: CommentId, my_local_user: Option<&'_ LocalUser>, ) -> Result { + let is_admin = my_local_user.map(|u| u.admin).unwrap_or(false); // If a person is given, then my_vote (res.9), if None, should be 0, not null // Necessary to differentiate between other person's votes - let res = queries().read(pool, (comment_id, my_local_user)).await?; - let mut new_view = res.clone(); + let mut res = queries().read(pool, (comment_id, my_local_user)).await?; if my_local_user.is_some() && res.my_vote.is_none() { - new_view.my_vote = Some(0); + res.my_vote = Some(0); } - if res.comment.deleted || res.comment.removed { - new_view.comment.content = String::new(); - } - Ok(new_view) + Ok(handle_deleted(res, is_admin)) } } @@ -350,22 +347,25 @@ pub struct CommentQuery<'a> { impl CommentQuery<'_> { pub async fn list(self, site: &Site, pool: &mut DbPool<'_>) -> Result, Error> { + let is_admin = self.local_user.map(|u| u.admin).unwrap_or(false); Ok( queries() .list(pool, (self, site)) .await? .into_iter() - .map(|mut c| { - if c.comment.deleted || c.comment.removed { - c.comment.content = String::new(); - } - c - }) + .map(|c| handle_deleted(c, is_admin)) .collect(), ) } } +fn handle_deleted(mut c: CommentView, is_admin: bool) -> CommentView { + if !is_admin && (c.comment.deleted || c.comment.removed) { + c.comment.content = String::new(); + } + c +} + #[cfg(test)] #[expect(clippy::indexing_slicing)] mod tests { @@ -1301,4 +1301,65 @@ mod tests { cleanup(data, pool).await } + + #[tokio::test] + #[serial] + async fn comment_removed() -> LemmyResult<()> { + let pool = &build_db_pool_for_tests(); + let pool = &mut pool.into(); + let mut data = init_data(pool).await?; + + // Mark a comment as removed + let form = CommentUpdateForm { + removed: Some(true), + ..Default::default() + }; + Comment::update(pool, data.inserted_comment_0.id, &form).await?; + + // Read as normal user, content is cleared + data.timmy_local_user_view.local_user.admin = false; + let comment_view = CommentView::read( + pool, + data.inserted_comment_0.id, + Some(&data.timmy_local_user_view.local_user), + ) + .await?; + assert_eq!("", comment_view.comment.content); + let comment_listing = CommentQuery { + community_id: Some(data.inserted_community.id), + local_user: Some(&data.timmy_local_user_view.local_user), + sort: Some(CommentSortType::Old), + ..Default::default() + } + .list(&data.site, pool) + .await?; + assert_eq!("", comment_listing[0].comment.content); + + // Read as admin, content is returned + data.timmy_local_user_view.local_user.admin = true; + let comment_view = CommentView::read( + pool, + data.inserted_comment_0.id, + Some(&data.timmy_local_user_view.local_user), + ) + .await?; + assert_eq!( + data.inserted_comment_0.content, + comment_view.comment.content + ); + let comment_listing = CommentQuery { + community_id: Some(data.inserted_community.id), + local_user: Some(&data.timmy_local_user_view.local_user), + sort: Some(CommentSortType::Old), + ..Default::default() + } + .list(&data.site, pool) + .await?; + assert_eq!( + data.inserted_comment_0.content, + comment_listing[0].comment.content + ); + + cleanup(data, pool).await + } }