3 comment::{collect_non_local_mentions, get_notif_recipients},
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::{base::AnyBase, link::Mention, primitives::OneOrMany, unparsed::Unparsed};
17 use lemmy_api_common::blocking;
18 use lemmy_apub_lib::{values::PublicUrl, verify_domains_match, ActivityFields, ActivityHandler};
19 use lemmy_db_queries::Crud;
20 use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
21 use lemmy_utils::LemmyError;
22 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
23 use serde::{Deserialize, Serialize};
26 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
27 #[serde(rename_all = "camelCase")]
28 pub struct CreateOrUpdateComment {
34 #[serde(rename = "type")]
35 kind: CreateOrUpdateType,
37 #[serde(rename = "@context")]
38 context: OneOrMany<AnyBase>,
43 impl CreateOrUpdateComment {
47 kind: CreateOrUpdateType,
48 context: &LemmyContext,
49 ) -> Result<(), LemmyError> {
50 // TODO: might be helpful to add a comment method to retrieve community directly
51 let post_id = comment.post_id;
52 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
53 let community_id = post.community_id;
54 let community = blocking(context.pool(), move |conn| {
55 Community::read(conn, community_id)
59 let id = generate_activity_id(kind.clone())?;
60 let maa = collect_non_local_mentions(comment, &community, context).await?;
62 let create_or_update = CreateOrUpdateComment {
63 actor: actor.actor_id(),
64 to: PublicUrl::Public,
65 object: comment.to_apub(context.pool()).await?,
70 context: lemmy_context(),
71 unparsed: Default::default(),
74 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
75 send_to_community_new(activity, &id, actor, &community, maa.inboxes, context).await
79 #[async_trait::async_trait(?Send)]
80 impl ActivityHandler for CreateOrUpdateComment {
83 context: &LemmyContext,
84 request_counter: &mut i32,
85 ) -> Result<(), LemmyError> {
86 let community = extract_community(&self.cc, context, request_counter).await?;
88 verify_activity(self)?;
89 verify_person_in_community(&self.actor, &community.actor_id(), context, request_counter)
91 verify_domains_match(&self.actor, self.object.id_unchecked())?;
92 // TODO: should add a check that the correct community is in cc (probably needs changes to
93 // comment deserialization)
94 self.object.verify(context, request_counter).await?;
100 context: &LemmyContext,
101 request_counter: &mut i32,
102 ) -> Result<(), LemmyError> {
103 let comment = Comment::from_apub(&self.object, context, &self.actor, request_counter).await?;
104 let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?;
105 let notif_type = match self.kind {
106 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
107 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
109 send_comment_ws_message(
110 comment.id, notif_type, None, None, None, recipients, context,