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};
31 impl CreateOrUpdateComment {
32 #[tracing::instrument(skip(comment, actor, kind, context))]
36 kind: CreateOrUpdateType,
37 context: &LemmyContext,
38 request_counter: &mut i32,
39 ) -> Result<(), LemmyError> {
40 // TODO: might be helpful to add a comment method to retrieve community directly
41 let post_id = comment.post_id;
42 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
43 let community_id = post.community_id;
44 let community: ApubCommunity = blocking(context.pool(), move |conn| {
45 Community::read(conn, community_id)
50 let id = generate_activity_id(
52 &context.settings().get_protocol_and_hostname(),
54 info!("Sending Create/Comment for {} as {}", comment.ap_id, id);
55 let note = comment.into_apub(context).await?;
57 let create_or_update = CreateOrUpdateComment {
58 actor: ObjectId::new(actor.actor_id()),
61 tag: note.tag.clone(),
65 unparsed: Default::default(),
68 let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
71 .map(|t| t.href.clone())
74 let mut inboxes = vec![];
75 for t in tagged_users {
77 .dereference(context, context.client(), request_counter)
79 inboxes.push(person.shared_inbox_or_inbox_url());
82 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
83 send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
87 #[async_trait::async_trait(?Send)]
88 impl ActivityHandler for CreateOrUpdateComment {
89 type DataType = LemmyContext;
91 #[tracing::instrument(skip_all)]
94 context: &Data<LemmyContext>,
95 request_counter: &mut i32,
96 ) -> Result<(), LemmyError> {
97 verify_is_public(&self.to, &self.cc)?;
98 let post = self.object.get_parents(context, request_counter).await?.0;
99 let community = self.get_community(context, request_counter).await?;
101 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
102 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
103 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
104 check_community_deleted_or_removed(&community)?;
105 check_post_deleted_or_removed(&post)?;
107 ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
111 #[tracing::instrument(skip_all)]
114 context: &Data<LemmyContext>,
115 request_counter: &mut i32,
116 ) -> Result<(), LemmyError> {
117 let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
118 let do_send_email = self.kind == CreateOrUpdateType::Create;
119 let recipients = get_comment_notif_recipients(
127 let notif_type = match self.kind {
128 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
129 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
131 send_comment_ws_message(
132 comment.id, notif_type, None, None, None, recipients, context,
139 #[async_trait::async_trait(?Send)]
140 impl GetCommunity for CreateOrUpdateComment {
141 #[tracing::instrument(skip_all)]
142 async fn get_community(
144 context: &LemmyContext,
145 request_counter: &mut i32,
146 ) -> Result<ApubCommunity, LemmyError> {
147 let post = self.object.get_parents(context, request_counter).await?.0;
148 let community = blocking(context.pool(), move |conn| {
149 Community::read(conn, post.community_id)