From 8e54a4a6ccc9491bb9defbfe82ef6353846098b8 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 8 Apr 2024 10:26:24 -0400 Subject: [PATCH] Fixing bug where comment replies wouldn't be sent to blocked instances. (#4595) * Fixing bug where comment replies wouldn't be sent to blocked instances. - Instance blocks should only affect communities, not comments. - Fixes #4590 * Revert "Fixing bug where comment replies wouldn't be sent to blocked instances." This reverts commit 1349aa351a195fdf0d179c27a8057257ca37e255. * Only block replies from the community's instance id. - Also refactor send_local_notifs slightly, since it has to fetch the community now. - Fixes #4590 --------- Co-authored-by: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com> --- crates/api_common/src/build_response.rs | 23 +++++++++++-------- crates/api_common/src/utils.rs | 4 ++-- crates/api_crud/src/comment/create.rs | 3 +-- crates/api_crud/src/comment/delete.rs | 18 +++------------ crates/api_crud/src/comment/remove.rs | 6 +---- crates/api_crud/src/comment/update.rs | 3 +-- .../activities/create_or_update/comment.rs | 4 +--- 7 files changed, 23 insertions(+), 38 deletions(-) diff --git a/crates/api_common/src/build_response.rs b/crates/api_common/src/build_response.rs index 19e0bb46b..990b96544 100644 --- a/crates/api_common/src/build_response.rs +++ b/crates/api_common/src/build_response.rs @@ -19,7 +19,6 @@ use lemmy_db_schema::{ comment_reply::{CommentReply, CommentReplyInsertForm}, person::Person, person_mention::{PersonMention, PersonMentionInsertForm}, - post::Post, }, traits::Crud, }; @@ -91,16 +90,19 @@ pub async fn build_post_response( #[tracing::instrument(skip_all)] pub async fn send_local_notifs( mentions: Vec, - comment: &Comment, + comment_id: CommentId, person: &Person, - post: &Post, do_send_email: bool, context: &LemmyContext, ) -> Result, LemmyError> { let mut recipient_ids = Vec::new(); let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname()); - let community_id = post.community_id; + // Read the comment view to get extra info + let comment_view = CommentView::read(&mut context.pool(), comment_id, None).await?; + let comment = comment_view.comment; + let post = comment_view.post; + let community = comment_view.community; // Send the local mentions for mention in mentions @@ -117,7 +119,7 @@ pub async fn send_local_notifs( let user_mention_form = PersonMentionInsertForm { recipient_id: mention_user_view.person.id, - comment_id: comment.id, + comment_id, read: None, }; @@ -152,8 +154,9 @@ pub async fn send_local_notifs( let check_blocks = check_person_instance_community_block( person.id, parent_creator_id, - person.instance_id, - community_id, + // Only block from the community's instance_id + community.instance_id, + community.id, &mut context.pool(), ) .await @@ -194,11 +197,13 @@ pub async fn send_local_notifs( } } } else { + // Use the post creator to check blocks let check_blocks = check_person_instance_community_block( person.id, post.creator_id, - person.instance_id, - community_id, + // Only block from the community's instance_id + community.instance_id, + community.id, &mut context.pool(), ) .await diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index ad416ffbb..dd0a8c0c6 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -295,12 +295,12 @@ async fn check_instance_block( pub async fn check_person_instance_community_block( my_id: PersonId, potential_blocker_id: PersonId, - instance_id: InstanceId, + community_instance_id: InstanceId, community_id: CommunityId, pool: &mut DbPool<'_>, ) -> Result<(), LemmyError> { check_person_block(my_id, potential_blocker_id, pool).await?; - check_instance_block(instance_id, potential_blocker_id, pool).await?; + check_instance_block(community_instance_id, potential_blocker_id, pool).await?; check_community_block(community_id, potential_blocker_id, pool).await?; Ok(()) } diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index acb386c60..f8092268f 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -138,9 +138,8 @@ pub async fn create_comment( let mentions = scrape_text_for_mentions(&content); let recipient_ids = send_local_notifs( mentions, - &updated_comment, + inserted_comment_id, &local_user_view.person, - &post, true, &context, ) diff --git a/crates/api_crud/src/comment/delete.rs b/crates/api_crud/src/comment/delete.rs index 2de2a7955..9db1ed034 100644 --- a/crates/api_crud/src/comment/delete.rs +++ b/crates/api_crud/src/comment/delete.rs @@ -8,10 +8,7 @@ use lemmy_api_common::{ utils::check_community_user_action, }; use lemmy_db_schema::{ - source::{ - comment::{Comment, CommentUpdateForm}, - post::Post, - }, + source::comment::{Comment, CommentUpdateForm}, traits::Crud, }; use lemmy_db_views::structs::{CommentView, LocalUserView}; @@ -56,17 +53,8 @@ pub async fn delete_comment( .await .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?; - let post_id = updated_comment.post_id; - let post = Post::read(&mut context.pool(), post_id).await?; - let recipient_ids = send_local_notifs( - vec![], - &updated_comment, - &local_user_view.person, - &post, - false, - &context, - ) - .await?; + let recipient_ids = + send_local_notifs(vec![], comment_id, &local_user_view.person, false, &context).await?; let updated_comment_id = updated_comment.id; ActivityChannel::submit_activity( diff --git a/crates/api_crud/src/comment/remove.rs b/crates/api_crud/src/comment/remove.rs index 5bb6f55b1..d735c4462 100644 --- a/crates/api_crud/src/comment/remove.rs +++ b/crates/api_crud/src/comment/remove.rs @@ -12,7 +12,6 @@ use lemmy_db_schema::{ comment::{Comment, CommentUpdateForm}, comment_report::CommentReport, moderator::{ModRemoveComment, ModRemoveCommentForm}, - post::Post, }, traits::{Crud, Reportable}, }; @@ -61,13 +60,10 @@ pub async fn remove_comment( }; ModRemoveComment::create(&mut context.pool(), &form).await?; - let post_id = updated_comment.post_id; - let post = Post::read(&mut context.pool(), post_id).await?; let recipient_ids = send_local_notifs( vec![], - &updated_comment, + comment_id, &local_user_view.person.clone(), - &post, false, &context, ) diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index e814ebd6b..b35333ec5 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -79,9 +79,8 @@ pub async fn update_comment( let mentions = scrape_text_for_mentions(&updated_comment_content); let recipient_ids = send_local_notifs( mentions, - &updated_comment, + comment_id, &local_user_view.person, - &orig_comment.post, false, &context, ) diff --git a/crates/apub/src/activities/create_or_update/comment.rs b/crates/apub/src/activities/create_or_update/comment.rs index c8d9a017e..ea6af2fcf 100644 --- a/crates/apub/src/activities/create_or_update/comment.rs +++ b/crates/apub/src/activities/create_or_update/comment.rs @@ -159,8 +159,6 @@ impl ActivityHandler for CreateOrUpdateNote { CommentAggregates::update_hot_rank(&mut context.pool(), comment.id).await?; let do_send_email = self.kind == CreateOrUpdateType::Create; - let post_id = comment.post_id; - let post = Post::read(&mut context.pool(), post_id).await?; let actor = self.actor.dereference(context).await?; // Note: @@ -169,7 +167,7 @@ impl ActivityHandler for CreateOrUpdateNote { // anyway. // TODO: for compatibility with other projects, it would be much better to read this from cc or tags let mentions = scrape_text_for_mentions(&comment.content); - send_local_notifs(mentions, &comment.0, &actor, &post, do_send_email, context).await?; + send_local_notifs(mentions, comment.id, &actor, do_send_email, context).await?; Ok(()) } }