]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/create_or_update/comment.rs
Use audience field to federate items in groups (fixes #2464) (#2584)
[lemmy.git] / crates / apub / src / activities / create_or_update / comment.rs
1 use crate::{
2   activities::{
3     check_community_deleted_or_removed,
4     community::send_activity_in_community,
5     create_or_update::get_comment_notif_recipients,
6     generate_activity_id,
7     verify_is_public,
8     verify_person_in_community,
9   },
10   activity_lists::AnnouncableActivities,
11   local_instance,
12   mentions::MentionOrValue,
13   objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
14   protocol::{
15     activities::{create_or_update::note::CreateOrUpdateNote, CreateOrUpdateType},
16     InCommunity,
17   },
18   ActorType,
19 };
20 use activitypub_federation::{
21   core::object_id::ObjectId,
22   data::Data,
23   traits::{ActivityHandler, Actor, ApubObject},
24   utils::verify_domains_match,
25 };
26 use activitystreams_kinds::public;
27 use lemmy_api_common::utils::check_post_deleted_or_removed;
28 use lemmy_db_schema::{
29   source::{
30     comment::{CommentLike, CommentLikeForm},
31     community::Community,
32     post::Post,
33   },
34   traits::{Crud, Likeable},
35 };
36 use lemmy_utils::error::LemmyError;
37 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
38 use url::Url;
39
40 impl CreateOrUpdateNote {
41   #[tracing::instrument(skip(comment, actor, kind, context))]
42   pub async fn send(
43     comment: ApubComment,
44     actor: &ApubPerson,
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();
54
55     let id = generate_activity_id(
56       kind.clone(),
57       &context.settings().get_protocol_and_hostname(),
58     )?;
59     let note = comment.into_apub(context).await?;
60
61     let create_or_update = CreateOrUpdateNote {
62       actor: ObjectId::new(actor.actor_id()),
63       to: vec![public()],
64       cc: note.cc.clone(),
65       tag: note.tag.clone(),
66       object: note,
67       kind,
68       id: id.clone(),
69       audience: Some(ObjectId::new(community.actor_id())),
70     };
71
72     let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
73       .tag
74       .iter()
75       .filter_map(|t| {
76         if let MentionOrValue::Mention(t) = t {
77           Some(t)
78         } else {
79           None
80         }
81       })
82       .map(|t| t.href.clone())
83       .map(ObjectId::new)
84       .collect();
85     let mut inboxes = vec![];
86     for t in tagged_users {
87       let person = t
88         .dereference(context, local_instance(context).await, request_counter)
89         .await?;
90       inboxes.push(person.shared_inbox_or_inbox());
91     }
92
93     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
94     send_activity_in_community(activity, actor, &community, inboxes, false, context).await
95   }
96 }
97
98 #[async_trait::async_trait(?Send)]
99 impl ActivityHandler for CreateOrUpdateNote {
100   type DataType = LemmyContext;
101   type Error = LemmyError;
102
103   fn id(&self) -> &Url {
104     &self.id
105   }
106
107   fn actor(&self) -> &Url {
108     self.actor.inner()
109   }
110
111   #[tracing::instrument(skip_all)]
112   async fn verify(
113     &self,
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?;
120
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)?;
125
126     ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
127     Ok(())
128   }
129
130   #[tracing::instrument(skip_all)]
131   async fn receive(
132     self,
133     context: &Data<LemmyContext>,
134     request_counter: &mut i32,
135   ) -> Result<(), LemmyError> {
136     let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
137
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,
143       score: 1,
144     };
145     CommentLike::like(context.pool(), &like_form).await?;
146
147     let do_send_email = self.kind == CreateOrUpdateType::Create;
148     let recipients = get_comment_notif_recipients(
149       &self.actor,
150       &comment,
151       do_send_email,
152       context,
153       request_counter,
154     )
155     .await?;
156     let notif_type = match self.kind {
157       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
158       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
159     };
160     send_comment_ws_message(
161       comment.id, notif_type, None, None, None, recipients, context,
162     )
163     .await?;
164     Ok(())
165   }
166 }