Skip to content

Commit

Permalink
Handle displaying of deleted and removed posts/comments (fixes #2624)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Jun 26, 2023
1 parent c588640 commit c8fc93e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 22 deletions.
8 changes: 1 addition & 7 deletions crates/apub/src/api/list_posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use activitypub_federation::config::Data;
use lemmy_api_common::{
context::LemmyContext,
post::{GetPosts, GetPostsResponse},
utils::{check_private_instance, is_mod_or_admin_opt, local_user_view_from_jwt_opt},
utils::{check_private_instance, local_user_view_from_jwt_opt},
};
use lemmy_db_schema::source::{community::Community, local_site::LocalSite};
use lemmy_db_views::post_view::PostQuery;
Expand Down Expand Up @@ -41,11 +41,6 @@ impl PerformApub for GetPosts {

let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?;

let is_mod_or_admin =
is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), community_id)
.await
.is_ok();

let posts = PostQuery::builder()
.pool(context.pool())
.local_user(local_user_view.map(|l| l.local_user).as_ref())
Expand All @@ -55,7 +50,6 @@ impl PerformApub for GetPosts {
.saved_only(saved_only)
.page(page)
.limit(limit)
.is_mod_or_admin(Some(is_mod_or_admin))
.build()
.list()
.await
Expand Down
9 changes: 6 additions & 3 deletions crates/apub/src/api/read_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ impl PerformApub for GetPersonDetails {
let limit = data.limit;
let saved_only = data.saved_only;
let community_id = data.community_id;
let local_user = local_user_view.map(|l| l.local_user);
let local_user = local_user_view.as_ref().map(|l| l.local_user.clone());
let local_user_clone = local_user.clone();
let is_own_profile = Some(Some(person_details_id) == local_user_view.map(|l| l.person.id));

let posts_query = PostQuery::builder()
.pool(context.pool())
.sort(sort)
.saved_only(saved_only)
.local_user(local_user.as_ref())
.community_id(community_id)
.is_mod_or_admin(is_admin)
.show_removed(is_admin)
.show_deleted(is_own_profile)
.page(page)
.limit(limit);

Expand All @@ -90,7 +92,8 @@ impl PerformApub for GetPersonDetails {
.local_user(local_user_clone.as_ref())
.sort(sort.map(post_to_comment_sort_type))
.saved_only(saved_only)
.show_deleted_and_removed(Some(false))
.show_removed(is_admin)
.show_deleted(is_own_profile)
.community_id(community_id)
.page(page)
.limit(limit);
Expand Down
3 changes: 0 additions & 3 deletions crates/apub/src/api/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ impl PerformApub for Search {
.creator_id(creator_id)
.local_user(local_user.as_ref())
.search_term(Some(q))
.is_mod_or_admin(is_admin)
.page(page)
.limit(limit)
.build()
Expand Down Expand Up @@ -127,7 +126,6 @@ impl PerformApub for Search {
.creator_id(creator_id)
.local_user(local_user_.as_ref())
.search_term(Some(q))
.is_mod_or_admin(is_admin)
.page(page)
.limit(limit)
.build()
Expand Down Expand Up @@ -194,7 +192,6 @@ impl PerformApub for Search {
.community_id(community_id)
.creator_id(creator_id)
.url_search(Some(q))
.is_mod_or_admin(is_admin)
.page(page)
.limit(limit)
.build()
Expand Down
8 changes: 6 additions & 2 deletions crates/db_views/src/comment_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ pub struct CommentQuery<'a> {
local_user: Option<&'a LocalUser>,
search_term: Option<String>,
saved_only: Option<bool>,
show_deleted_and_removed: Option<bool>,
show_deleted: Option<bool>,
show_removed: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
max_depth: Option<i32>,
Expand Down Expand Up @@ -298,8 +299,11 @@ impl<'a> CommentQuery<'a> {
query = query.filter(comment_saved::comment_id.is_not_null());
}

if !self.show_deleted_and_removed.unwrap_or(true) {
if !self.show_deleted.unwrap_or(false) {
query = query.filter(comment::deleted.eq(false));
}

if !self.show_removed.unwrap_or(false) {
query = query.filter(comment::removed.eq(false));
}

Expand Down
104 changes: 97 additions & 7 deletions crates/db_views/src/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ pub struct PostQuery<'a> {
search_term: Option<String>,
url_search: Option<String>,
saved_only: Option<bool>,
/// Used to show deleted or removed posts for admins
is_mod_or_admin: Option<bool>,
show_removed: Option<bool>,
show_deleted: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
}
Expand Down Expand Up @@ -305,16 +305,18 @@ impl<'a> PostQuery<'a> {
))
.into_boxed();

// Hide deleted and removed for non-admins or mods
// TODO This eventually needs to show posts where you are the creator
if !self.is_mod_or_admin.unwrap_or(false) {
if !self.show_deleted.unwrap_or(false) {
query = query
.filter(community::removed.eq(false))
.filter(community::deleted.eq(false))
.filter(post::removed.eq(false))
.filter(post::deleted.eq(false));
}

if !self.show_removed.unwrap_or(false) {
query = query
.filter(community::removed.eq(false))
.filter(post::removed.eq(false));
}

if self.community_id.is_none() {
query = query.then_order_by(post_aggregates::featured_local.desc());
} else if let Some(community_id) = self.community_id {
Expand Down Expand Up @@ -874,6 +876,94 @@ mod tests {
cleanup(data, pool).await;
}

#[tokio::test]
#[serial]
async fn post_listings_removed() {
let pool = &build_db_pool_for_tests().await;
let data = init_data(pool).await;

// Remove the post
Post::update(
pool,
data.inserted_post.id,
&PostUpdateForm::builder().removed(Some(true)).build(),
)
.await
.unwrap();

// Make sure you don't see the removed post in the results
let post_listings_no_admin = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.show_removed(Some(false))
.build()
.list()
.await
.unwrap();

assert_eq!(1, post_listings_no_admin.len());

// Make sure they see both
let post_listings_is_admin = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.include_removed(Some(true))
.build()
.list()
.await
.unwrap();

assert_eq!(2, post_listings_is_admin.len());

cleanup(data, pool).await;
}

#[tokio::test]
#[serial]
async fn post_listings_deleted() {
let pool = &build_db_pool_for_tests().await;
let data = init_data(pool).await;

// Delete the post
Post::update(
pool,
data.inserted_post.id,
&PostUpdateForm::builder().deleted(Some(true)).build(),
)
.await
.unwrap();

// Make sure you don't see the deleted post in the results
let post_listings_no_admin = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.show_deleted(Some(false))
.build()
.list()
.await
.unwrap();

assert_eq!(1, post_listings_no_admin.len());

// Make sure they see both
let post_listings_is_admin = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.include_removed(Some(true))
.build()
.list()
.await
.unwrap();

assert_eq!(2, post_listings_is_admin.len());

cleanup(data, pool).await;
}

#[tokio::test]
#[serial]
async fn post_listings_deleted() {
Expand Down

0 comments on commit c8fc93e

Please sign in to comment.