3 check_community_deleted_or_removed,
4 comment::get_notif_recipients,
5 community::{announce::GetCommunity, send_activity_in_community},
9 verify_person_in_community,
11 activity_lists::AnnouncableActivities,
12 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
13 protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
15 use activitystreams_kinds::public;
16 use lemmy_api_common::{blocking, check_post_deleted_or_removed};
20 traits::{ActivityHandler, ActorType, ApubObject},
21 verify::verify_domains_match,
23 use lemmy_db_schema::{
24 source::{community::Community, post::Post},
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
30 impl CreateOrUpdateComment {
31 #[tracing::instrument(skip(comment, actor, kind, context))]
35 kind: CreateOrUpdateType,
36 context: &LemmyContext,
37 request_counter: &mut i32,
38 ) -> Result<(), LemmyError> {
39 // TODO: might be helpful to add a comment method to retrieve community directly
40 let post_id = comment.post_id;
41 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
42 let community_id = post.community_id;
43 let community: ApubCommunity = blocking(context.pool(), move |conn| {
44 Community::read(conn, community_id)
49 let id = generate_activity_id(
51 &context.settings().get_protocol_and_hostname(),
53 let note = comment.into_apub(context).await?;
55 let create_or_update = CreateOrUpdateComment {
56 actor: ObjectId::new(actor.actor_id()),
59 tag: note.tag.clone(),
63 unparsed: Default::default(),
66 let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
69 .map(|t| t.href.clone())
72 let mut inboxes = vec![];
73 for t in tagged_users {
74 let person = t.dereference(context, request_counter).await?;
75 inboxes.push(person.shared_inbox_or_inbox_url());
78 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
79 send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
83 #[async_trait::async_trait(?Send)]
84 impl ActivityHandler for CreateOrUpdateComment {
85 type DataType = LemmyContext;
87 #[tracing::instrument(skip_all)]
90 context: &Data<LemmyContext>,
91 request_counter: &mut i32,
92 ) -> Result<(), LemmyError> {
93 verify_is_public(&self.to, &self.cc)?;
94 let post = self.object.get_parents(context, request_counter).await?.0;
95 let community = self.get_community(context, request_counter).await?;
97 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
98 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
99 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
100 check_community_deleted_or_removed(&community)?;
101 check_post_deleted_or_removed(&post)?;
103 ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
107 #[tracing::instrument(skip_all)]
110 context: &Data<LemmyContext>,
111 request_counter: &mut i32,
112 ) -> Result<(), LemmyError> {
113 let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
114 let do_send_email = self.kind == CreateOrUpdateType::Create;
115 let recipients = get_notif_recipients(
123 let notif_type = match self.kind {
124 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
125 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
127 send_comment_ws_message(
128 comment.id, notif_type, None, None, None, recipients, context,
135 #[async_trait::async_trait(?Send)]
136 impl GetCommunity for CreateOrUpdateComment {
137 #[tracing::instrument(skip_all)]
138 async fn get_community(
140 context: &LemmyContext,
141 request_counter: &mut i32,
142 ) -> Result<ApubCommunity, LemmyError> {
143 let post = self.object.get_parents(context, request_counter).await?.0;
144 let community = blocking(context.pool(), move |conn| {
145 Community::read(conn, post.community_id)