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