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