]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activities/create_or_update/comment.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / apub / src / activities / create_or_update / comment.rs
index 451cb867030ba5edfde006956d6e7a680a6a51d8..dad6ada9bf96e5ddcfeead994ef646ef35c56347 100644 (file)
@@ -4,29 +4,35 @@ use crate::{
     community::{announce::GetCommunity, send_activity_in_community},
     create_or_update::get_comment_notif_recipients,
     generate_activity_id,
-    verify_activity,
     verify_is_public,
     verify_person_in_community,
   },
   activity_lists::AnnouncableActivities,
+  local_instance,
+  mentions::MentionOrValue,
   objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
   protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
+  ActorType,
 };
-use activitystreams_kinds::public;
-use lemmy_api_common::{blocking, check_post_deleted_or_removed};
-use lemmy_apub_lib::{
+use activitypub_federation::{
+  core::object_id::ObjectId,
   data::Data,
-  object_id::ObjectId,
-  traits::{ActivityHandler, ActorType, ApubObject},
-  verify::verify_domains_match,
+  traits::{ActivityHandler, Actor, ApubObject},
+  utils::verify_domains_match,
 };
+use activitystreams_kinds::public;
+use lemmy_api_common::utils::{blocking, check_post_deleted_or_removed};
 use lemmy_db_schema::{
-  source::{community::Community, post::Post},
-  traits::Crud,
+  source::{
+    comment::{CommentLike, CommentLikeForm},
+    community::Community,
+    post::Post,
+  },
+  traits::{Crud, Likeable},
 };
-use lemmy_utils::LemmyError;
+use lemmy_utils::error::LemmyError;
 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
-use tracing::info;
+use url::Url;
 
 impl CreateOrUpdateComment {
   #[tracing::instrument(skip(comment, actor, kind, context))]
@@ -51,7 +57,6 @@ impl CreateOrUpdateComment {
       kind.clone(),
       &context.settings().get_protocol_and_hostname(),
     )?;
-    info!("Sending Create/Comment for {} as {}", comment.ap_id, id);
     let note = comment.into_apub(context).await?;
 
     let create_or_update = CreateOrUpdateComment {
@@ -68,25 +73,41 @@ impl CreateOrUpdateComment {
     let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
       .tag
       .iter()
+      .filter_map(|t| {
+        if let MentionOrValue::Mention(t) = t {
+          Some(t)
+        } else {
+          None
+        }
+      })
       .map(|t| t.href.clone())
       .map(ObjectId::new)
       .collect();
     let mut inboxes = vec![];
     for t in tagged_users {
       let person = t
-        .dereference(context, context.client(), request_counter)
+        .dereference(context, local_instance(context), request_counter)
         .await?;
-      inboxes.push(person.shared_inbox_or_inbox_url());
+      inboxes.push(person.shared_inbox_or_inbox());
     }
 
     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
-    send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
+    send_activity_in_community(activity, actor, &community, inboxes, context).await
   }
 }
 
 #[async_trait::async_trait(?Send)]
 impl ActivityHandler for CreateOrUpdateComment {
   type DataType = LemmyContext;
+  type Error = LemmyError;
+
+  fn id(&self) -> &Url {
+    &self.id
+  }
+
+  fn actor(&self) -> &Url {
+    self.actor.inner()
+  }
 
   #[tracing::instrument(skip_all)]
   async fn verify(
@@ -98,7 +119,6 @@ impl ActivityHandler for CreateOrUpdateComment {
     let post = self.object.get_parents(context, request_counter).await?.0;
     let community = self.get_community(context, request_counter).await?;
 
-    verify_activity(&self.id, self.actor.inner(), &context.settings())?;
     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
     verify_domains_match(self.actor.inner(), self.object.id.inner())?;
     check_community_deleted_or_removed(&community)?;
@@ -115,6 +135,19 @@ impl ActivityHandler for CreateOrUpdateComment {
     request_counter: &mut i32,
   ) -> Result<(), LemmyError> {
     let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
+
+    // author likes their own comment by default
+    let like_form = CommentLikeForm {
+      comment_id: comment.id,
+      post_id: comment.post_id,
+      person_id: comment.creator_id,
+      score: 1,
+    };
+    blocking(context.pool(), move |conn: &mut _| {
+      CommentLike::like(conn, &like_form)
+    })
+    .await??;
+
     let do_send_email = self.kind == CreateOrUpdateType::Create;
     let recipients = get_comment_notif_recipients(
       &self.actor,