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 fetcher::object_id::ObjectId,
14 objects::{comment::Note, FromApub, ToApub},
17 use activitystreams::{base::AnyBase, link::Mention, primitives::OneOrMany, unparsed::Unparsed};
18 use lemmy_api_common::blocking;
19 use lemmy_apub_lib::{values::PublicUrl, verify_domains_match, ActivityFields, ActivityHandler};
20 use lemmy_db_queries::Crud;
21 use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
22 use lemmy_utils::LemmyError;
23 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
24 use serde::{Deserialize, Serialize};
27 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
28 #[serde(rename_all = "camelCase")]
29 pub struct CreateOrUpdateComment {
30 actor: ObjectId<Person>,
35 #[serde(rename = "type")]
36 kind: CreateOrUpdateType,
38 #[serde(rename = "@context")]
39 context: OneOrMany<AnyBase>,
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(
62 &context.settings().get_protocol_and_hostname(),
64 let maa = collect_non_local_mentions(comment, &community, context).await?;
66 let create_or_update = CreateOrUpdateComment {
67 actor: ObjectId::new(actor.actor_id()),
68 to: [PublicUrl::Public],
69 object: comment.to_apub(context.pool()).await?,
74 context: lemmy_context(),
75 unparsed: Default::default(),
78 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
79 send_to_community_new(activity, &id, actor, &community, maa.inboxes, context).await
83 #[async_trait::async_trait(?Send)]
84 impl ActivityHandler for CreateOrUpdateComment {
87 context: &LemmyContext,
88 request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
90 let community = extract_community(&self.cc, context, request_counter).await?;
91 let community_id = ObjectId::new(community.actor_id());
93 verify_activity(self, &context.settings())?;
94 verify_person_in_community(&self.actor, &community_id, context, request_counter).await?;
95 verify_domains_match(self.actor.inner(), self.object.id_unchecked())?;
96 // TODO: should add a check that the correct community is in cc (probably needs changes to
97 // comment deserialization)
98 self.object.verify(context, request_counter).await?;
104 context: &LemmyContext,
105 request_counter: &mut i32,
106 ) -> Result<(), LemmyError> {
108 Comment::from_apub(&self.object, context, self.actor.inner(), request_counter).await?;
109 let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?;
110 let notif_type = match self.kind {
111 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
112 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
114 send_comment_ws_message(
115 comment.id, notif_type, None, None, None, recipients, context,