3 check_community_deleted_or_removed,
4 community::send_activity_in_community,
7 verify_person_in_community,
9 activity_lists::AnnouncableActivities,
11 mentions::MentionOrValue,
12 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
14 activities::{create_or_update::note::CreateOrUpdateNote, CreateOrUpdateType},
19 use activitypub_federation::{
21 fetch::object_id::ObjectId,
23 protocol::verification::verify_domains_match,
24 traits::{ActivityHandler, Actor, Object},
26 use lemmy_api_common::{
27 build_response::send_local_notifs,
28 comment::{CommentResponse, CreateComment, EditComment},
29 context::LemmyContext,
30 utils::{check_post_deleted_or_removed, is_mod_or_admin},
32 use lemmy_db_schema::{
35 comment::{Comment, CommentLike, CommentLikeForm},
40 traits::{Crud, Likeable},
42 use lemmy_utils::{error::LemmyError, utils::mention::scrape_text_for_mentions};
45 #[async_trait::async_trait]
46 impl SendActivity for CreateComment {
47 type Response = CommentResponse;
49 async fn send_activity(
51 response: &Self::Response,
52 context: &Data<LemmyContext>,
53 ) -> Result<(), LemmyError> {
54 CreateOrUpdateNote::send(
55 &response.comment_view.comment,
56 response.comment_view.creator.id,
57 CreateOrUpdateType::Create,
64 #[async_trait::async_trait]
65 impl SendActivity for EditComment {
66 type Response = CommentResponse;
68 async fn send_activity(
70 response: &Self::Response,
71 context: &Data<LemmyContext>,
72 ) -> Result<(), LemmyError> {
73 CreateOrUpdateNote::send(
74 &response.comment_view.comment,
75 response.comment_view.creator.id,
76 CreateOrUpdateType::Update,
83 impl CreateOrUpdateNote {
84 #[tracing::instrument(skip(comment, person_id, kind, context))]
88 kind: CreateOrUpdateType,
89 context: &Data<LemmyContext>,
90 ) -> Result<(), LemmyError> {
91 // TODO: might be helpful to add a comment method to retrieve community directly
92 let post_id = comment.post_id;
93 let post = Post::read(context.pool(), post_id).await?;
94 let community_id = post.community_id;
95 let person: ApubPerson = Person::read(context.pool(), person_id).await?.into();
96 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
98 let id = generate_activity_id(
100 &context.settings().get_protocol_and_hostname(),
102 let note = ApubComment(comment.clone()).into_json(context).await?;
104 let create_or_update = CreateOrUpdateNote {
105 actor: person.id().into(),
108 tag: note.tag.clone(),
112 audience: Some(community.id().into()),
115 let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
119 if let MentionOrValue::Mention(t) = t {
125 .map(|t| t.href.clone())
128 let mut inboxes = vec![];
129 for t in tagged_users {
130 let person = t.dereference(context).await?;
131 inboxes.push(person.shared_inbox_or_inbox());
134 let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
135 send_activity_in_community(activity, &person, &community, inboxes, false, context).await
139 #[async_trait::async_trait]
140 impl ActivityHandler for CreateOrUpdateNote {
141 type DataType = LemmyContext;
142 type Error = LemmyError;
144 fn id(&self) -> &Url {
148 fn actor(&self) -> &Url {
152 #[tracing::instrument(skip_all)]
153 async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
154 verify_is_public(&self.to, &self.cc)?;
155 let post = self.object.get_parents(context).await?.0;
156 let community = self.community(context).await?;
158 verify_person_in_community(&self.actor, &community, context).await?;
159 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
160 check_community_deleted_or_removed(&community)?;
161 check_post_deleted_or_removed(&post)?;
163 ApubComment::verify(&self.object, self.actor.inner(), context).await?;
167 #[tracing::instrument(skip_all)]
168 async fn receive(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
169 insert_activity(&self.id, &self, false, false, context).await?;
170 // Need to do this check here instead of Note::from_json because we need the person who
171 // send the activity, not the comment author.
172 let existing_comment = self.object.id.dereference_local(context).await.ok();
173 if let (Some(distinguished), Some(existing_comment)) =
174 (self.object.distinguished, existing_comment)
176 if distinguished != existing_comment.distinguished {
177 let creator = self.actor.dereference(context).await?;
178 let (post, _) = self.object.get_parents(context).await?;
179 is_mod_or_admin(context.pool(), creator.id, post.community_id).await?;
183 let comment = ApubComment::from_json(self.object, context).await?;
185 // author likes their own comment by default
186 let like_form = CommentLikeForm {
187 comment_id: comment.id,
188 post_id: comment.post_id,
189 person_id: comment.creator_id,
192 CommentLike::like(context.pool(), &like_form).await?;
194 let do_send_email = self.kind == CreateOrUpdateType::Create;
195 let post_id = comment.post_id;
196 let post = Post::read(context.pool(), post_id).await?;
197 let actor = self.actor.dereference(context).await?;
200 // Although mentions could be gotten from the post tags (they are included there), or the ccs,
201 // Its much easier to scrape them from the comment body, since the API has to do that
203 // TODO: for compatibility with other projects, it would be much better to read this from cc or tags
204 let mentions = scrape_text_for_mentions(&comment.content);
205 send_local_notifs(mentions, &comment.0, &actor, &post, do_send_email, context).await?;