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