Return content of removed comments for admins (ref #5232) (#5245)

* Return content of removed comments for admins (ref #5232)

* fmt

* remove dbg
This commit is contained in:
Nutomic 2024-12-02 22:06:39 +00:00 committed by GitHub
parent 9505d1d205
commit dcf1cfca9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 74 additions and 16 deletions

View file

@ -156,7 +156,6 @@ test("Delete a comment", async () => {
commentRes.comment_view.comment.id, commentRes.comment_view.comment.id,
); );
expect(deleteCommentRes.comment_view.comment.deleted).toBe(true); expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
expect(deleteCommentRes.comment_view.comment.content).toBe("");
// Make sure that comment is deleted on beta // Make sure that comment is deleted on beta
await waitUntil( await waitUntil(
@ -254,7 +253,6 @@ test("Remove a comment from admin and community on different instance", async ()
betaComment.comment.id, betaComment.comment.id,
); );
expect(removeCommentRes.comment_view.comment.removed).toBe(true); expect(removeCommentRes.comment_view.comment.removed).toBe(true);
expect(removeCommentRes.comment_view.comment.content).toBe("");
// Comment text is also hidden from list // Comment text is also hidden from list
let listComments = await getComments( 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.length).toBe(1);
expect(listComments.comments[0].comment.removed).toBe(true); expect(listComments.comments[0].comment.removed).toBe(true);
expect(listComments.comments[0].comment.content).toBe("");
// Make sure its not removed on alpha // Make sure its not removed on alpha
let refetchedPostComments = await getComments( let refetchedPostComments = await getComments(

View file

@ -316,17 +316,14 @@ impl CommentView {
comment_id: CommentId, comment_id: CommentId,
my_local_user: Option<&'_ LocalUser>, my_local_user: Option<&'_ LocalUser>,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
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 // 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 // Necessary to differentiate between other person's votes
let res = queries().read(pool, (comment_id, my_local_user)).await?; let mut res = queries().read(pool, (comment_id, my_local_user)).await?;
let mut new_view = res.clone();
if my_local_user.is_some() && res.my_vote.is_none() { 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 { Ok(handle_deleted(res, is_admin))
new_view.comment.content = String::new();
}
Ok(new_view)
} }
} }
@ -350,22 +347,25 @@ pub struct CommentQuery<'a> {
impl CommentQuery<'_> { impl CommentQuery<'_> {
pub async fn list(self, site: &Site, pool: &mut DbPool<'_>) -> Result<Vec<CommentView>, Error> { pub async fn list(self, site: &Site, pool: &mut DbPool<'_>) -> Result<Vec<CommentView>, Error> {
let is_admin = self.local_user.map(|u| u.admin).unwrap_or(false);
Ok( Ok(
queries() queries()
.list(pool, (self, site)) .list(pool, (self, site))
.await? .await?
.into_iter() .into_iter()
.map(|mut c| { .map(|c| handle_deleted(c, is_admin))
if c.comment.deleted || c.comment.removed {
c.comment.content = String::new();
}
c
})
.collect(), .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)] #[cfg(test)]
#[expect(clippy::indexing_slicing)] #[expect(clippy::indexing_slicing)]
mod tests { mod tests {
@ -1301,4 +1301,65 @@ mod tests {
cleanup(data, pool).await 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
}
} }