]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/create_or_update/comment.rs
Make verify apub url function async (#2514)
[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::{blocking, 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 = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
49     let community_id = post.community_id;
50     let community: ApubCommunity = blocking(context.pool(), move |conn| {
51       Community::read(conn, community_id)
52     })
53     .await??
54     .into();
55
56     let id = generate_activity_id(
57       kind.clone(),
58       &context.settings().get_protocol_and_hostname(),
59     )?;
60     let note = comment.into_apub(context).await?;
61
62     let create_or_update = CreateOrUpdateComment {
63       actor: ObjectId::new(actor.actor_id()),
64       to: vec![public()],
65       cc: note.cc.clone(),
66       tag: note.tag.clone(),
67       object: note,
68       kind,
69       id: id.clone(),
70       unparsed: Default::default(),
71     };
72
73     let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
74       .tag
75       .iter()
76       .filter_map(|t| {
77         if let MentionOrValue::Mention(t) = t {
78           Some(t)
79         } else {
80           None
81         }
82       })
83       .map(|t| t.href.clone())
84       .map(ObjectId::new)
85       .collect();
86     let mut inboxes = vec![];
87     for t in tagged_users {
88       let person = t
89         .dereference(context, local_instance(context), request_counter)
90         .await?;
91       inboxes.push(person.shared_inbox_or_inbox());
92     }
93
94     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
95     send_activity_in_community(activity, actor, &community, inboxes, context).await
96   }
97 }
98
99 #[async_trait::async_trait(?Send)]
100 impl ActivityHandler for CreateOrUpdateComment {
101   type DataType = LemmyContext;
102   type Error = LemmyError;
103
104   fn id(&self) -> &Url {
105     &self.id
106   }
107
108   fn actor(&self) -> &Url {
109     self.actor.inner()
110   }
111
112   #[tracing::instrument(skip_all)]
113   async fn verify(
114     &self,
115     context: &Data<LemmyContext>,
116     request_counter: &mut i32,
117   ) -> Result<(), LemmyError> {
118     verify_is_public(&self.to, &self.cc)?;
119     let post = self.object.get_parents(context, request_counter).await?.0;
120     let community = self.get_community(context, request_counter).await?;
121
122     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
123     verify_domains_match(self.actor.inner(), self.object.id.inner())?;
124     check_community_deleted_or_removed(&community)?;
125     check_post_deleted_or_removed(&post)?;
126
127     ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
128     Ok(())
129   }
130
131   #[tracing::instrument(skip_all)]
132   async fn receive(
133     self,
134     context: &Data<LemmyContext>,
135     request_counter: &mut i32,
136   ) -> Result<(), LemmyError> {
137     let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
138
139     // author likes their own comment by default
140     let like_form = CommentLikeForm {
141       comment_id: comment.id,
142       post_id: comment.post_id,
143       person_id: comment.creator_id,
144       score: 1,
145     };
146     blocking(context.pool(), move |conn: &mut _| {
147       CommentLike::like(conn, &like_form)
148     })
149     .await??;
150
151     let do_send_email = self.kind == CreateOrUpdateType::Create;
152     let recipients = get_comment_notif_recipients(
153       &self.actor,
154       &comment,
155       do_send_email,
156       context,
157       request_counter,
158     )
159     .await?;
160     let notif_type = match self.kind {
161       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
162       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
163     };
164     send_comment_ws_message(
165       comment.id, notif_type, None, None, None, recipients, context,
166     )
167     .await?;
168     Ok(())
169   }
170 }
171
172 #[async_trait::async_trait(?Send)]
173 impl GetCommunity for CreateOrUpdateComment {
174   #[tracing::instrument(skip_all)]
175   async fn get_community(
176     &self,
177     context: &LemmyContext,
178     request_counter: &mut i32,
179   ) -> Result<ApubCommunity, LemmyError> {
180     let post = self.object.get_parents(context, request_counter).await?.0;
181     let community = blocking(context.pool(), move |conn| {
182       Community::read(conn, post.community_id)
183     })
184     .await??;
185     Ok(community.into())
186   }
187 }