]> Untitled Git - lemmy.git/commitdiff
Merge logic for comment create and update
authorFelix Ableitner <me@nutomic.com>
Sat, 31 Jul 2021 15:09:43 +0000 (17:09 +0200)
committerFelix Ableitner <me@nutomic.com>
Sat, 31 Jul 2021 15:47:08 +0000 (17:47 +0200)
crates/api_crud/src/comment/create.rs
crates/api_crud/src/comment/update.rs
crates/apub/src/activities/comment/create_or_update.rs [moved from crates/apub/src/activities/comment/create.rs with 75% similarity]
crates/apub/src/activities/comment/mod.rs
crates/apub/src/activities/comment/update.rs [deleted file]
crates/apub/src/activities/community/announce.rs
crates/apub/src/http/inbox_enums.rs

index 4418c14e28b4595075d68a5d61ccb1c34ca505e2..899555fc884030a366e673ab9785a286fe6ccdc1 100644 (file)
@@ -9,7 +9,7 @@ use lemmy_api_common::{
   send_local_notifs,
 };
 use lemmy_apub::{
-  activities::comment::create::CreateComment as CreateApubComment,
+  activities::comment::create_or_update::{CreateOrUpdateComment, CreateOrUpdateType},
   generate_apub_endpoint,
   ApubLikeableType,
   EndpointType,
@@ -88,7 +88,13 @@ impl PerformCrud for CreateComment {
       .await?
       .map_err(|_| ApiError::err("couldnt_create_comment"))?;
 
-    CreateApubComment::send(&updated_comment, &local_user_view.person, context).await?;
+    CreateOrUpdateComment::send(
+      &updated_comment,
+      &local_user_view.person,
+      CreateOrUpdateType::Create,
+      context,
+    )
+    .await?;
 
     // Scan the comment for user mentions, add those rows
     let post_id = post.id;
index 4bd35884df59697a861bc980eb556d0394688000..f3911faecdf4ecb48bb8cee90d4e417c2031d752 100644 (file)
@@ -7,7 +7,10 @@ use lemmy_api_common::{
   get_local_user_view_from_jwt,
   send_local_notifs,
 };
-use lemmy_apub::activities::comment::update::UpdateComment;
+use lemmy_apub::activities::comment::create_or_update::{
+  CreateOrUpdateComment,
+  CreateOrUpdateType,
+};
 use lemmy_db_queries::{source::comment::Comment_, DeleteableOrRemoveable};
 use lemmy_db_schema::source::comment::*;
 use lemmy_db_views::comment_view::CommentView;
@@ -58,8 +61,13 @@ impl PerformCrud for EditComment {
     .await?
     .map_err(|_| ApiError::err("couldnt_update_comment"))?;
 
-    // Send the apub update
-    UpdateComment::send(&updated_comment, &local_user_view.person, context).await?;
+    CreateOrUpdateComment::send(
+      &updated_comment,
+      &local_user_view.person,
+      CreateOrUpdateType::Update,
+      context,
+    )
+    .await?;
 
     // Do the mentions / recipients
     let updated_comment_content = updated_comment.content.to_owned();
similarity index 75%
rename from crates/apub/src/activities/comment/create.rs
rename to crates/apub/src/activities/comment/create_or_update.rs
index 6c79dec483bda7fbcca8bd89d3d86014ae4d04c0..f67040987fe625a363f3e9d9b351fbf9c5a7675a 100644 (file)
@@ -24,28 +24,36 @@ use lemmy_db_queries::Crud;
 use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
 use lemmy_utils::LemmyError;
 use lemmy_websocket::{LemmyContext, UserOperationCrud};
+use serde::{Deserialize, Serialize};
 use url::Url;
 
-#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
+#[derive(Clone, Debug, Deserialize, Serialize)]
+pub enum CreateOrUpdateType {
+  Create,
+  Update,
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
 #[serde(rename_all = "camelCase")]
-pub struct CreateComment {
+pub struct CreateOrUpdateComment {
   to: PublicUrl,
   object: Note,
   cc: Vec<Url>,
   tag: Vec<Mention>,
   #[serde(rename = "type")]
-  kind: CreateType,
+  kind: CreateOrUpdateType,
   #[serde(flatten)]
   common: ActivityCommonFields,
 }
 
-impl CreateComment {
+impl CreateOrUpdateComment {
   pub async fn send(
     comment: &Comment,
     actor: &Person,
+    kind: CreateOrUpdateType,
     context: &LemmyContext,
   ) -> Result<(), LemmyError> {
-    // TODO: would be helpful to add a comment method to retrieve community directly
+    // TODO: might be helpful to add a comment method to retrieve community directly
     let post_id = comment.post_id;
     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
     let community_id = post.community_id;
@@ -53,15 +61,19 @@ impl CreateComment {
       Community::read(conn, community_id)
     })
     .await??;
-    let id = generate_activity_id(CreateType::Create)?;
+
+    let id = match kind {
+      CreateOrUpdateType::Create => generate_activity_id(CreateType::Create),
+      CreateOrUpdateType::Update => generate_activity_id(CreateType::Create),
+    }?;
     let maa = collect_non_local_mentions(comment, &community, context).await?;
 
-    let create = CreateComment {
+    let create_or_update = CreateOrUpdateComment {
       to: PublicUrl::Public,
       object: comment.to_apub(context.pool()).await?,
       cc: maa.ccs,
       tag: maa.tags,
-      kind: Default::default(),
+      kind,
       common: ActivityCommonFields {
         context: lemmy_context(),
         id: id.clone(),
@@ -70,13 +82,13 @@ impl CreateComment {
       },
     };
 
-    let activity = AnnouncableActivities::CreateComment(create);
+    let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
     send_to_community_new(activity, &id, actor, &community, maa.inboxes, context).await
   }
 }
 
 #[async_trait::async_trait(?Send)]
-impl ActivityHandler for CreateComment {
+impl ActivityHandler for CreateOrUpdateComment {
   async fn verify(
     &self,
     context: &LemmyContext,
@@ -114,13 +126,11 @@ impl ActivityHandler for CreateComment {
     .await?;
     let recipients =
       get_notif_recipients(&self.common.actor, &comment, context, request_counter).await?;
-    send_websocket_message(
-      comment.id,
-      recipients,
-      UserOperationCrud::CreateComment,
-      context,
-    )
-    .await
+    let notif_type = match self.kind {
+      CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
+      CreateOrUpdateType::Update => UserOperationCrud::EditComment,
+    };
+    send_websocket_message(comment.id, recipients, notif_type, context).await
   }
 
   fn common(&self) -> &ActivityCommonFields {
index 1bf94ed6cd4900c56a21fc97b11e3fbf016b0169..e7499718cb8e0b829c9be35cb2591d72ef8f92f3 100644 (file)
@@ -24,8 +24,7 @@ use log::debug;
 use reqwest::Client;
 use url::Url;
 
-pub mod create;
-pub mod update;
+pub mod create_or_update;
 
 async fn get_notif_recipients(
   actor: &Url,
diff --git a/crates/apub/src/activities/comment/update.rs b/crates/apub/src/activities/comment/update.rs
deleted file mode 100644 (file)
index 8dd44b8..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-use crate::{
-  activities::{
-    comment::{collect_non_local_mentions, get_notif_recipients, send_websocket_message},
-    community::announce::AnnouncableActivities,
-    extract_community,
-    generate_activity_id,
-    verify_activity,
-    verify_person_in_community,
-  },
-  activity_queue::send_to_community_new,
-  extensions::context::lemmy_context,
-  objects::{comment::Note, FromApub, ToApub},
-  ActorType,
-};
-use activitystreams::{activity::kind::UpdateType, link::Mention};
-use lemmy_api_common::blocking;
-use lemmy_apub_lib::{
-  values::PublicUrl,
-  verify_domains_match,
-  ActivityCommonFields,
-  ActivityHandler,
-};
-use lemmy_db_queries::Crud;
-use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
-use lemmy_utils::LemmyError;
-use lemmy_websocket::{LemmyContext, UserOperationCrud};
-use url::Url;
-
-#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
-#[serde(rename_all = "camelCase")]
-pub struct UpdateComment {
-  to: PublicUrl,
-  object: Note,
-  cc: Vec<Url>,
-  tag: Vec<Mention>,
-  #[serde(rename = "type")]
-  kind: UpdateType,
-  #[serde(flatten)]
-  common: ActivityCommonFields,
-}
-
-impl UpdateComment {
-  pub async fn send(
-    comment: &Comment,
-    actor: &Person,
-    context: &LemmyContext,
-  ) -> Result<(), LemmyError> {
-    // TODO: would be helpful to add a comment method to retrieve community directly
-    let post_id = comment.post_id;
-    let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
-    let community_id = post.community_id;
-    let community = blocking(context.pool(), move |conn| {
-      Community::read(conn, community_id)
-    })
-    .await??;
-    let id = generate_activity_id(UpdateType::Update)?;
-    let maa = collect_non_local_mentions(comment, &community, context).await?;
-
-    let update = UpdateComment {
-      to: PublicUrl::Public,
-      object: comment.to_apub(context.pool()).await?,
-      cc: maa.ccs,
-      tag: maa.tags,
-      kind: Default::default(),
-      common: ActivityCommonFields {
-        context: lemmy_context(),
-        id: id.clone(),
-        actor: actor.actor_id(),
-        unparsed: Default::default(),
-      },
-    };
-
-    let activity = AnnouncableActivities::UpdateComment(update);
-    send_to_community_new(activity, &id, actor, &community, maa.inboxes, context).await
-  }
-}
-
-#[async_trait::async_trait(?Send)]
-impl ActivityHandler for UpdateComment {
-  async fn verify(
-    &self,
-    context: &LemmyContext,
-    request_counter: &mut i32,
-  ) -> Result<(), LemmyError> {
-    let community = extract_community(&self.cc, context, request_counter).await?;
-
-    verify_activity(self.common())?;
-    verify_person_in_community(
-      &self.common.actor,
-      &community.actor_id(),
-      context,
-      request_counter,
-    )
-    .await?;
-    verify_domains_match(&self.common.actor, &self.object.id)?;
-    self.object.verify(context, request_counter).await?;
-    Ok(())
-  }
-
-  async fn receive(
-    &self,
-    context: &LemmyContext,
-    request_counter: &mut i32,
-  ) -> Result<(), LemmyError> {
-    let comment = Comment::from_apub(
-      &self.object,
-      context,
-      self.common.actor.clone(),
-      request_counter,
-      false,
-    )
-    .await?;
-
-    let recipients =
-      get_notif_recipients(&self.common.actor, &comment, context, request_counter).await?;
-    send_websocket_message(
-      comment.id,
-      recipients,
-      UserOperationCrud::EditComment,
-      context,
-    )
-    .await
-  }
-
-  fn common(&self) -> &ActivityCommonFields {
-    &self.common
-  }
-}
index bc72d80fe91ae15b6287f256700007078b3c69e5..4beee5d293caaa3d6b24874fde050e2bb7cc04f9 100644 (file)
@@ -1,6 +1,6 @@
 use crate::{
   activities::{
-    comment::{create::CreateComment, update::UpdateComment},
+    comment::create_or_update::CreateOrUpdateComment,
     community::{
       add_mod::AddMod,
       block_user::BlockUserFromCommunity,
@@ -44,8 +44,7 @@ use url::Url;
 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler)]
 #[serde(untagged)]
 pub enum AnnouncableActivities {
-  CreateComment(CreateComment),
-  UpdateComment(UpdateComment),
+  CreateOrUpdateComment(CreateOrUpdateComment),
   CreatePost(CreatePost),
   UpdatePost(UpdatePost),
   LikePostOrComment(LikePostOrComment),
index 0f31f534e57686e318a3e0079ecf893bd8ed0337..20b311505d44b4a9947361ec61c4a75351d2ab4e 100644 (file)
@@ -1,5 +1,5 @@
 use crate::activities::{
-  comment::{create::CreateComment, update::UpdateComment},
+  comment::create_or_update::CreateOrUpdateComment,
   community::{
     add_mod::AddMod,
     announce::AnnounceActivity,
@@ -48,8 +48,7 @@ pub enum PersonInboxActivities {
 pub enum GroupInboxActivities {
   FollowCommunity(FollowCommunity),
   UndoFollowCommunity(UndoFollowCommunity),
-  CreateComment(CreateComment),
-  UpdateComment(UpdateComment),
+  CreateOrUpdateComment(CreateOrUpdateComment),
   CreatePost(CreatePost),
   UpdatePost(UpdatePost),
   LikePostOrComment(LikePostOrComment),
@@ -72,8 +71,7 @@ pub enum SharedInboxActivities {
   // received by group
   FollowCommunity(FollowCommunity),
   UndoFollowCommunity(UndoFollowCommunity),
-  CreateComment(CreateComment),
-  UpdateComment(UpdateComment),
+  CreateOrUpdateComment(CreateOrUpdateComment),
   CreatePost(CreatePost),
   UpdatePost(UpdatePost),
   LikePostOrComment(LikePostOrComment),