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(kind.clone())?;
61 let maa = collect_non_local_mentions(comment, &community, context).await?;
63 let create_or_update = CreateOrUpdateComment {
64 actor: ObjectId::new(actor.actor_id()),
65 to: [PublicUrl::Public],
66 object: comment.to_apub(context.pool()).await?,
71 context: lemmy_context(),
72 unparsed: Default::default(),
75 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
76 send_to_community_new(activity, &id, actor, &community, maa.inboxes, context).await
80 #[async_trait::async_trait(?Send)]
81 impl ActivityHandler for CreateOrUpdateComment {
84 context: &LemmyContext,
85 request_counter: &mut i32,
86 ) -> Result<(), LemmyError> {
87 let community = extract_community(&self.cc, context, request_counter).await?;
88 let community_id = ObjectId::new(community.actor_id());
90 verify_activity(self)?;
91 verify_person_in_community(&self.actor, &community_id, context, request_counter).await?;
92 verify_domains_match(self.actor.inner(), self.object.id_unchecked())?;
93 // TODO: should add a check that the correct community is in cc (probably needs changes to
94 // comment deserialization)
95 self.object.verify(context, request_counter).await?;
101 context: &LemmyContext,
102 request_counter: &mut i32,
103 ) -> Result<(), LemmyError> {
105 Comment::from_apub(&self.object, context, self.actor.inner(), request_counter).await?;
106 let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?;
107 let notif_type = match self.kind {
108 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
109 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
111 send_comment_ws_message(
112 comment.id, notif_type, None, None, None, recipients, context,