3 check_community_deleted_or_removed,
4 community::send_activity_in_community,
5 create_or_update::get_comment_notif_recipients,
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
12 mentions::MentionOrValue,
13 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
15 activities::{create_or_update::note::CreateOrUpdateNote, CreateOrUpdateType},
20 use activitypub_federation::{
21 core::object_id::ObjectId,
23 traits::{ActivityHandler, Actor, ApubObject},
24 utils::verify_domains_match,
26 use activitystreams_kinds::public;
27 use lemmy_api_common::utils::check_post_deleted_or_removed;
28 use lemmy_db_schema::{
30 comment::{CommentLike, CommentLikeForm},
34 traits::{Crud, Likeable},
36 use lemmy_utils::error::LemmyError;
37 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
40 impl CreateOrUpdateNote {
41 #[tracing::instrument(skip(comment, actor, kind, context))]
45 kind: CreateOrUpdateType,
46 context: &LemmyContext,
47 request_counter: &mut i32,
48 ) -> Result<(), LemmyError> {
49 // TODO: might be helpful to add a comment method to retrieve community directly
50 let post_id = comment.post_id;
51 let post = Post::read(context.pool(), post_id).await?;
52 let community_id = post.community_id;
53 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
55 let id = generate_activity_id(
57 &context.settings().get_protocol_and_hostname(),
59 let note = comment.into_apub(context).await?;
61 let create_or_update = CreateOrUpdateNote {
62 actor: ObjectId::new(actor.actor_id()),
65 tag: note.tag.clone(),
69 audience: Some(ObjectId::new(community.actor_id())),
72 let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
76 if let MentionOrValue::Mention(t) = t {
82 .map(|t| t.href.clone())
85 let mut inboxes = vec![];
86 for t in tagged_users {
88 .dereference(context, local_instance(context).await, request_counter)
90 inboxes.push(person.shared_inbox_or_inbox());
93 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
94 send_activity_in_community(activity, actor, &community, inboxes, false, context).await
98 #[async_trait::async_trait(?Send)]
99 impl ActivityHandler for CreateOrUpdateNote {
100 type DataType = LemmyContext;
101 type Error = LemmyError;
103 fn id(&self) -> &Url {
107 fn actor(&self) -> &Url {
111 #[tracing::instrument(skip_all)]
114 context: &Data<LemmyContext>,
115 request_counter: &mut i32,
116 ) -> Result<(), LemmyError> {
117 verify_is_public(&self.to, &self.cc)?;
118 let post = self.object.get_parents(context, request_counter).await?.0;
119 let community = self.community(context, request_counter).await?;
121 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
122 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
123 check_community_deleted_or_removed(&community)?;
124 check_post_deleted_or_removed(&post)?;
126 ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
130 #[tracing::instrument(skip_all)]
133 context: &Data<LemmyContext>,
134 request_counter: &mut i32,
135 ) -> Result<(), LemmyError> {
136 let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
138 // author likes their own comment by default
139 let like_form = CommentLikeForm {
140 comment_id: comment.id,
141 post_id: comment.post_id,
142 person_id: comment.creator_id,
145 CommentLike::like(context.pool(), &like_form).await?;
147 let do_send_email = self.kind == CreateOrUpdateType::Create;
148 let recipients = get_comment_notif_recipients(
156 let notif_type = match self.kind {
157 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
158 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
160 send_comment_ws_message(
161 comment.id, notif_type, None, None, None, recipients, context,