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::{
28 utils::check_post_deleted_or_removed,
29 websocket::{send::send_comment_ws_message, UserOperationCrud},
32 use lemmy_db_schema::{
34 comment::{CommentLike, CommentLikeForm},
38 traits::{Crud, Likeable},
40 use lemmy_utils::error::LemmyError;
43 impl CreateOrUpdateNote {
44 #[tracing::instrument(skip(comment, actor, kind, context))]
48 kind: CreateOrUpdateType,
49 context: &LemmyContext,
50 request_counter: &mut i32,
51 ) -> Result<(), LemmyError> {
52 // TODO: might be helpful to add a comment method to retrieve community directly
53 let post_id = comment.post_id;
54 let post = Post::read(context.pool(), post_id).await?;
55 let community_id = post.community_id;
56 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
58 let id = generate_activity_id(
60 &context.settings().get_protocol_and_hostname(),
62 let note = comment.into_apub(context).await?;
64 let create_or_update = CreateOrUpdateNote {
65 actor: ObjectId::new(actor.actor_id()),
68 tag: note.tag.clone(),
72 audience: Some(ObjectId::new(community.actor_id())),
75 let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
79 if let MentionOrValue::Mention(t) = t {
85 .map(|t| t.href.clone())
88 let mut inboxes = vec![];
89 for t in tagged_users {
91 .dereference(context, local_instance(context).await, request_counter)
93 inboxes.push(person.shared_inbox_or_inbox());
96 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
97 send_activity_in_community(activity, actor, &community, inboxes, false, context).await
101 #[async_trait::async_trait(?Send)]
102 impl ActivityHandler for CreateOrUpdateNote {
103 type DataType = LemmyContext;
104 type Error = LemmyError;
106 fn id(&self) -> &Url {
110 fn actor(&self) -> &Url {
114 #[tracing::instrument(skip_all)]
117 context: &Data<LemmyContext>,
118 request_counter: &mut i32,
119 ) -> Result<(), LemmyError> {
120 verify_is_public(&self.to, &self.cc)?;
121 let post = self.object.get_parents(context, request_counter).await?.0;
122 let community = self.community(context, request_counter).await?;
124 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
125 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
126 check_community_deleted_or_removed(&community)?;
127 check_post_deleted_or_removed(&post)?;
129 ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
133 #[tracing::instrument(skip_all)]
136 context: &Data<LemmyContext>,
137 request_counter: &mut i32,
138 ) -> Result<(), LemmyError> {
139 let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
141 // author likes their own comment by default
142 let like_form = CommentLikeForm {
143 comment_id: comment.id,
144 post_id: comment.post_id,
145 person_id: comment.creator_id,
148 CommentLike::like(context.pool(), &like_form).await?;
150 let do_send_email = self.kind == CreateOrUpdateType::Create;
151 let recipients = get_comment_notif_recipients(
159 let notif_type = match self.kind {
160 CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
161 CreateOrUpdateType::Update => UserOperationCrud::EditComment,
163 send_comment_ws_message(
164 comment.id, notif_type, None, None, None, recipients, context,