3 comment::{collect_non_local_mentions, get_notif_recipients, send_websocket_message},
4 community::announce::AnnouncableActivities,
8 verify_person_in_community,
11 activity_queue::send_to_community_new,
12 extensions::context::lemmy_context,
13 objects::{comment::Note, FromApub, ToApub},
16 use activitystreams::link::Mention;
17 use lemmy_api_common::blocking;
24 use lemmy_db_queries::Crud;
25 use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
26 use lemmy_utils::LemmyError;
27 use lemmy_websocket::{LemmyContext, UserOperationCrud};
28 use serde::{Deserialize, Serialize};
31 #[derive(Clone, Debug, Deserialize, Serialize)]
32 #[serde(rename_all = "camelCase")]
33 pub struct CreateOrUpdateComment {
38 #[serde(rename = "type")]
39 kind: CreateOrUpdateType,
41 common: ActivityCommonFields,
44 impl CreateOrUpdateComment {
48 kind: CreateOrUpdateType,
49 context: &LemmyContext,
50 ) -> Result<(), LemmyError> {
51 // TODO: might be helpful to add a comment method to retrieve community directly
52 let post_id = comment.post_id;
53 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
54 let community_id = post.community_id;
55 let community = blocking(context.pool(), move |conn| {
56 Community::read(conn, community_id)
60 let id = generate_activity_id(kind.clone())?;
61 let maa = collect_non_local_mentions(comment, &community, context).await?;
63 let create_or_update = CreateOrUpdateComment {
64 to: PublicUrl::Public,
65 object: comment.to_apub(context.pool()).await?,
69 common: ActivityCommonFields {
70 context: lemmy_context(),
72 actor: actor.actor_id(),
73 unparsed: Default::default(),
77 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
78 send_to_community_new(activity, &id, actor, &community, maa.inboxes, context).await
82 #[async_trait::async_trait(?Send)]
83 impl ActivityHandler for CreateOrUpdateComment {
86 context: &LemmyContext,
87 request_counter: &mut i32,
88 ) -> Result<(), LemmyError> {
89 let community = extract_community(&self.cc, context, request_counter).await?;
91 verify_activity(self.common())?;
92 verify_person_in_community(
94 &community.actor_id(),
99 verify_domains_match(&self.common.actor, self.object.id_unchecked())?;
100 // TODO: should add a check that the correct community is in cc (probably needs changes to
101 // comment deserialization)
102 self.object.verify(context, request_counter).await?;
108 context: &LemmyContext,
109 request_counter: &mut i32,
110 ) -> Result<(), LemmyError> {
112 Comment::from_apub(&self.object, context, &self.common.actor, request_counter).await?;
114 get_notif_recipients(&self.common.actor, &comment, context, request_counter).await?;
115 let notif_type = match self.kind {
116 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
117 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
119 send_websocket_message(comment.id, recipients, notif_type, context).await
122 fn common(&self) -> &ActivityCommonFields {