]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/create_or_update/comment.rs
54df09ce616c4d49ebe5f328138cc09e077494ab
[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   SendActivity,
20 };
21 use activitypub_federation::{
22   core::object_id::ObjectId,
23   data::Data,
24   traits::{ActivityHandler, Actor, ApubObject},
25   utils::verify_domains_match,
26 };
27 use activitystreams_kinds::public;
28 use lemmy_api_common::{
29   comment::{CommentResponse, CreateComment, EditComment},
30   context::LemmyContext,
31   utils::check_post_deleted_or_removed,
32   websocket::{send::send_comment_ws_message, UserOperationCrud},
33 };
34 use lemmy_db_schema::{
35   newtypes::PersonId,
36   source::{
37     comment::{Comment, CommentLike, CommentLikeForm},
38     community::Community,
39     person::Person,
40     post::Post,
41   },
42   traits::{Crud, Likeable},
43 };
44 use lemmy_utils::error::LemmyError;
45 use url::Url;
46
47 #[async_trait::async_trait(?Send)]
48 impl SendActivity for CreateComment {
49   type Response = CommentResponse;
50
51   async fn send_activity(
52     _request: &Self,
53     response: &Self::Response,
54     context: &LemmyContext,
55   ) -> Result<(), LemmyError> {
56     CreateOrUpdateNote::send(
57       &response.comment_view.comment,
58       response.comment_view.creator.id,
59       CreateOrUpdateType::Create,
60       context,
61     )
62     .await
63   }
64 }
65
66 #[async_trait::async_trait(?Send)]
67 impl SendActivity for EditComment {
68   type Response = CommentResponse;
69
70   async fn send_activity(
71     _request: &Self,
72     response: &Self::Response,
73     context: &LemmyContext,
74   ) -> Result<(), LemmyError> {
75     CreateOrUpdateNote::send(
76       &response.comment_view.comment,
77       response.comment_view.creator.id,
78       CreateOrUpdateType::Update,
79       context,
80     )
81     .await
82   }
83 }
84
85 impl CreateOrUpdateNote {
86   #[tracing::instrument(skip(comment, person_id, kind, context))]
87   async fn send(
88     comment: &Comment,
89     person_id: PersonId,
90     kind: CreateOrUpdateType,
91     context: &LemmyContext,
92   ) -> Result<(), LemmyError> {
93     // TODO: might be helpful to add a comment method to retrieve community directly
94     let post_id = comment.post_id;
95     let post = Post::read(context.pool(), post_id).await?;
96     let community_id = post.community_id;
97     let person: ApubPerson = Person::read(context.pool(), person_id).await?.into();
98     let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
99
100     let id = generate_activity_id(
101       kind.clone(),
102       &context.settings().get_protocol_and_hostname(),
103     )?;
104     let note = ApubComment(comment.clone()).into_apub(context).await?;
105
106     let create_or_update = CreateOrUpdateNote {
107       actor: ObjectId::new(person.actor_id()),
108       to: vec![public()],
109       cc: note.cc.clone(),
110       tag: note.tag.clone(),
111       object: note,
112       kind,
113       id: id.clone(),
114       audience: Some(ObjectId::new(community.actor_id())),
115     };
116
117     let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
118       .tag
119       .iter()
120       .filter_map(|t| {
121         if let MentionOrValue::Mention(t) = t {
122           Some(t)
123         } else {
124           None
125         }
126       })
127       .map(|t| t.href.clone())
128       .map(ObjectId::new)
129       .collect();
130     let mut inboxes = vec![];
131     for t in tagged_users {
132       let person = t
133         .dereference(context, local_instance(context).await, &mut 0)
134         .await?;
135       inboxes.push(person.shared_inbox_or_inbox());
136     }
137
138     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
139     send_activity_in_community(activity, &person, &community, inboxes, false, context).await
140   }
141 }
142
143 #[async_trait::async_trait(?Send)]
144 impl ActivityHandler for CreateOrUpdateNote {
145   type DataType = LemmyContext;
146   type Error = LemmyError;
147
148   fn id(&self) -> &Url {
149     &self.id
150   }
151
152   fn actor(&self) -> &Url {
153     self.actor.inner()
154   }
155
156   #[tracing::instrument(skip_all)]
157   async fn verify(
158     &self,
159     context: &Data<LemmyContext>,
160     request_counter: &mut i32,
161   ) -> Result<(), LemmyError> {
162     verify_is_public(&self.to, &self.cc)?;
163     let post = self.object.get_parents(context, request_counter).await?.0;
164     let community = self.community(context, request_counter).await?;
165
166     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
167     verify_domains_match(self.actor.inner(), self.object.id.inner())?;
168     check_community_deleted_or_removed(&community)?;
169     check_post_deleted_or_removed(&post)?;
170
171     ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
172     Ok(())
173   }
174
175   #[tracing::instrument(skip_all)]
176   async fn receive(
177     self,
178     context: &Data<LemmyContext>,
179     request_counter: &mut i32,
180   ) -> Result<(), LemmyError> {
181     let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
182
183     // author likes their own comment by default
184     let like_form = CommentLikeForm {
185       comment_id: comment.id,
186       post_id: comment.post_id,
187       person_id: comment.creator_id,
188       score: 1,
189     };
190     CommentLike::like(context.pool(), &like_form).await?;
191
192     let do_send_email = self.kind == CreateOrUpdateType::Create;
193     let recipients = get_comment_notif_recipients(
194       &self.actor,
195       &comment,
196       do_send_email,
197       context,
198       request_counter,
199     )
200     .await?;
201     let notif_type = match self.kind {
202       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
203       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
204     };
205     send_comment_ws_message(
206       comment.id, notif_type, None, None, None, recipients, context,
207     )
208     .await?;
209     Ok(())
210   }
211 }