]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activities/create_or_update/comment.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / apub / src / activities / create_or_update / comment.rs
index 55d5e3f0201754d4d525f3b1e29c972589a9b444..804f1827bb23a1cc7ade6e0f5a9f5b17961b2ca4 100644 (file)
@@ -2,7 +2,6 @@ use crate::{
   activities::{
     check_community_deleted_or_removed,
     community::send_activity_in_community,
-    create_or_update::get_comment_notif_recipients,
     generate_activity_id,
     verify_is_public,
     verify_person_in_community,
@@ -25,12 +24,13 @@ use activitypub_federation::{
   traits::{ActivityHandler, Actor, Object},
 };
 use lemmy_api_common::{
+  build_response::send_local_notifs,
   comment::{CommentResponse, CreateComment, EditComment},
   context::LemmyContext,
   utils::{check_post_deleted_or_removed, is_mod_or_admin},
-  websocket::{send::send_comment_ws_message, UserOperationCrud},
 };
 use lemmy_db_schema::{
+  aggregates::structs::CommentAggregates,
   newtypes::PersonId,
   source::{
     comment::{Comment, CommentLike, CommentLikeForm},
@@ -40,7 +40,7 @@ use lemmy_db_schema::{
   },
   traits::{Crud, Likeable},
 };
-use lemmy_utils::error::LemmyError;
+use lemmy_utils::{error::LemmyError, utils::mention::scrape_text_for_mentions};
 use url::Url;
 
 #[async_trait::async_trait]
@@ -91,10 +91,12 @@ impl CreateOrUpdateNote {
   ) -> Result<(), LemmyError> {
     // TODO: might be helpful to add a comment method to retrieve community directly
     let post_id = comment.post_id;
-    let post = Post::read(context.pool(), post_id).await?;
+    let post = Post::read(&mut context.pool(), post_id).await?;
     let community_id = post.community_id;
-    let person: ApubPerson = Person::read(context.pool(), person_id).await?.into();
-    let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
+    let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into();
+    let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
+      .await?
+      .into();
 
     let id = generate_activity_id(
       kind.clone(),
@@ -177,7 +179,7 @@ impl ActivityHandler for CreateOrUpdateNote {
       if distinguished != existing_comment.distinguished {
         let creator = self.actor.dereference(context).await?;
         let (post, _) = self.object.get_parents(context).await?;
-        is_mod_or_admin(context.pool(), creator.id, post.community_id).await?;
+        is_mod_or_admin(&mut context.pool(), creator.id, post.community_id).await?;
       }
     }
 
@@ -190,19 +192,23 @@ impl ActivityHandler for CreateOrUpdateNote {
       person_id: comment.creator_id,
       score: 1,
     };
-    CommentLike::like(context.pool(), &like_form).await?;
+    CommentLike::like(&mut context.pool(), &like_form).await?;
+
+    // Calculate initial hot_rank
+    CommentAggregates::update_hot_rank(&mut context.pool(), comment.id).await?;
 
     let do_send_email = self.kind == CreateOrUpdateType::Create;
-    let recipients =
-      get_comment_notif_recipients(&self.actor, &comment, do_send_email, context).await?;
-    let notif_type = match self.kind {
-      CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
-      CreateOrUpdateType::Update => UserOperationCrud::EditComment,
-    };
-    send_comment_ws_message(
-      comment.id, notif_type, None, None, None, recipients, context,
-    )
-    .await?;
+    let post_id = comment.post_id;
+    let post = Post::read(&mut context.pool(), post_id).await?;
+    let actor = self.actor.dereference(context).await?;
+
+    // Note:
+    // Although mentions could be gotten from the post tags (they are included there), or the ccs,
+    // Its much easier to scrape them from the comment body, since the API has to do that
+    // 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?;
     Ok(())
   }
 }