3 check_community_deleted_or_removed,
4 community::{announce::GetCommunity, send_activity_in_community},
5 create_or_update::get_comment_notif_recipients,
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 {
75 .dereference(context, context.client(), request_counter)
77 inboxes.push(person.shared_inbox_or_inbox_url());
80 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
81 send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
85 #[async_trait::async_trait(?Send)]
86 impl ActivityHandler for CreateOrUpdateComment {
87 type DataType = LemmyContext;
89 #[tracing::instrument(skip_all)]
92 context: &Data<LemmyContext>,
93 request_counter: &mut i32,
94 ) -> Result<(), LemmyError> {
95 verify_is_public(&self.to, &self.cc)?;
96 let post = self.object.get_parents(context, request_counter).await?.0;
97 let community = self.get_community(context, request_counter).await?;
99 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
100 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
101 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
102 check_community_deleted_or_removed(&community)?;
103 check_post_deleted_or_removed(&post)?;
105 ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
109 #[tracing::instrument(skip_all)]
112 context: &Data<LemmyContext>,
113 request_counter: &mut i32,
114 ) -> Result<(), LemmyError> {
115 let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
116 let do_send_email = self.kind == CreateOrUpdateType::Create;
117 let recipients = get_comment_notif_recipients(
125 let notif_type = match self.kind {
126 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
127 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
129 send_comment_ws_message(
130 comment.id, notif_type, None, None, None, recipients, context,
137 #[async_trait::async_trait(?Send)]
138 impl GetCommunity for CreateOrUpdateComment {
139 #[tracing::instrument(skip_all)]
140 async fn get_community(
142 context: &LemmyContext,
143 request_counter: &mut i32,
144 ) -> Result<ApubCommunity, LemmyError> {
145 let post = self.object.get_parents(context, request_counter).await?.0;
146 let community = blocking(context.pool(), move |conn| {
147 Community::read(conn, post.community_id)