3 activities::{generate_activity_id, send_activity_to_community},
6 create_apub_tombstone_response,
10 get_or_fetch_and_insert_comment,
11 get_or_fetch_and_insert_post,
12 get_or_fetch_and_upsert_user,
23 use activitystreams::{
25 kind::{CreateType, DeleteType, DislikeType, LikeType, RemoveType, UndoType, UpdateType},
36 object::{kind::NoteType, Note, Tombstone},
40 use actix_web::{body::Body, web, web::Path, HttpResponse};
42 use itertools::Itertools;
43 use lemmy_api_structs::blocking;
45 comment::{Comment, CommentForm},
53 utils::{convert_datetime, remove_slurs, scrape_text_for_mentions, MentionData},
57 use serde::Deserialize;
58 use serde_json::Error;
61 #[derive(Deserialize)]
62 pub struct CommentQuery {
66 /// Return the post json over HTTP.
67 pub async fn get_apub_comment(
68 info: Path<CommentQuery>,
69 context: web::Data<LemmyContext>,
70 ) -> Result<HttpResponse<Body>, LemmyError> {
71 let id = info.comment_id.parse::<i32>()?;
72 let comment = blocking(context.pool(), move |conn| Comment::read(conn, id)).await??;
75 Ok(create_apub_response(
76 &comment.to_apub(context.pool()).await?,
79 Ok(create_apub_tombstone_response(&comment.to_tombstone()?))
83 #[async_trait::async_trait(?Send)]
84 impl ToApub for Comment {
87 async fn to_apub(&self, pool: &DbPool) -> Result<Note, LemmyError> {
88 let mut comment = Note::new();
90 let creator_id = self.creator_id;
91 let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
93 let post_id = self.post_id;
94 let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
96 let community_id = post.community_id;
97 let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
99 // Add a vector containing some important info to the "in_reply_to" field
100 // [post_ap_id, Option(parent_comment_ap_id)]
101 let mut in_reply_to_vec = vec![post.ap_id];
103 if let Some(parent_id) = self.parent_id {
104 let parent_comment = blocking(pool, move |conn| Comment::read(conn, parent_id)).await??;
106 in_reply_to_vec.push(parent_comment.ap_id);
110 // Not needed when the Post is embedded in a collection (like for community outbox)
111 .set_context(activitystreams::context())
112 .set_id(Url::parse(&self.ap_id)?)
113 .set_published(convert_datetime(self.published))
114 .set_to(community.actor_id)
115 .set_many_in_reply_tos(in_reply_to_vec)
116 .set_content(self.content.to_owned())
117 .set_attributed_to(creator.actor_id);
119 if let Some(u) = self.updated {
120 comment.set_updated(convert_datetime(u));
126 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
127 create_tombstone(self.deleted, &self.ap_id, self.updated, NoteType::Note)
131 #[async_trait::async_trait(?Send)]
132 impl FromApub for CommentForm {
133 type ApubType = Note;
135 /// Parse an ActivityPub note received from another instance into a Lemmy comment
138 context: &LemmyContext,
139 expected_domain: Option<Url>,
140 ) -> Result<CommentForm, LemmyError> {
141 let creator_actor_id = ¬e
143 .context(location_info!())?
144 .as_single_xsd_any_uri()
145 .context(location_info!())?;
147 let creator = get_or_fetch_and_upsert_user(creator_actor_id, context).await?;
149 let mut in_reply_tos = note
152 .context(location_info!())?
154 .context(location_info!())?
156 .map(|i| i.as_xsd_any_uri().context(""));
157 let post_ap_id = in_reply_tos.next().context(location_info!())??;
159 // This post, or the parent comment might not yet exist on this server yet, fetch them.
160 let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
162 // The 2nd item, if it exists, is the parent comment apub_id
163 // For deeply nested comments, FromApub automatically gets called recursively
164 let parent_id: Option<i32> = match in_reply_tos.next() {
165 Some(parent_comment_uri) => {
166 let parent_comment_ap_id = &parent_comment_uri?;
168 get_or_fetch_and_insert_comment(&parent_comment_ap_id, context).await?;
170 Some(parent_comment.id)
176 .context(location_info!())?
177 .as_single_xsd_string()
178 .context(location_info!())?
180 let content_slurs_removed = remove_slurs(&content);
183 creator_id: creator.id,
186 content: content_slurs_removed,
189 published: note.published().map(|u| u.to_owned().naive_local()),
190 updated: note.updated().map(|u| u.to_owned().naive_local()),
192 ap_id: Some(check_actor_domain(note, expected_domain)?),
198 #[async_trait::async_trait(?Send)]
199 impl ApubObjectType for Comment {
200 /// Send out information about a newly created comment, to the followers of the community.
201 async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
202 let note = self.to_apub(context.pool()).await?;
204 let post_id = self.post_id;
205 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
207 let community_id = post.community_id;
208 let community = blocking(context.pool(), move |conn| {
209 Community::read(conn, community_id)
213 let maa = collect_non_local_mentions_and_addresses(&self.content, &community, context).await?;
215 let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?);
217 .set_context(activitystreams::context())
218 .set_id(generate_activity_id(CreateType::Create)?)
220 .set_many_ccs(maa.addressed_ccs.to_owned())
221 // Set the mention tags
222 .set_many_tags(maa.get_tags()?);
224 send_activity_to_community(&creator, &community, maa.inboxes, create, context).await?;
228 /// Send out information about an edited post, to the followers of the community.
229 async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
230 let note = self.to_apub(context.pool()).await?;
232 let post_id = self.post_id;
233 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
235 let community_id = post.community_id;
236 let community = blocking(context.pool(), move |conn| {
237 Community::read(conn, community_id)
241 let maa = collect_non_local_mentions_and_addresses(&self.content, &community, context).await?;
243 let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?);
245 .set_context(activitystreams::context())
246 .set_id(generate_activity_id(UpdateType::Update)?)
248 .set_many_ccs(maa.addressed_ccs.to_owned())
249 // Set the mention tags
250 .set_many_tags(maa.get_tags()?);
252 send_activity_to_community(&creator, &community, maa.inboxes, update, context).await?;
256 async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
257 let note = self.to_apub(context.pool()).await?;
259 let post_id = self.post_id;
260 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
262 let community_id = post.community_id;
263 let community = blocking(context.pool(), move |conn| {
264 Community::read(conn, community_id)
268 let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
270 .set_context(activitystreams::context())
271 .set_id(generate_activity_id(DeleteType::Delete)?)
273 .set_many_ccs(vec![community.get_followers_url()?]);
275 send_activity_to_community(
278 vec![community.get_shared_inbox_url()?],
286 async fn send_undo_delete(
289 context: &LemmyContext,
290 ) -> Result<(), LemmyError> {
291 let note = self.to_apub(context.pool()).await?;
293 let post_id = self.post_id;
294 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
296 let community_id = post.community_id;
297 let community = blocking(context.pool(), move |conn| {
298 Community::read(conn, community_id)
302 // Generate a fake delete activity, with the correct object
303 let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?);
305 .set_context(activitystreams::context())
306 .set_id(generate_activity_id(DeleteType::Delete)?)
308 .set_many_ccs(vec![community.get_followers_url()?]);
310 // Undo that fake activity
311 let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?);
313 .set_context(activitystreams::context())
314 .set_id(generate_activity_id(UndoType::Undo)?)
316 .set_many_ccs(vec![community.get_followers_url()?]);
318 send_activity_to_community(
321 vec![community.get_shared_inbox_url()?],
329 async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
330 let note = self.to_apub(context.pool()).await?;
332 let post_id = self.post_id;
333 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
335 let community_id = post.community_id;
336 let community = blocking(context.pool(), move |conn| {
337 Community::read(conn, community_id)
341 let mut remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?);
343 .set_context(activitystreams::context())
344 .set_id(generate_activity_id(RemoveType::Remove)?)
346 .set_many_ccs(vec![community.get_followers_url()?]);
348 send_activity_to_community(
351 vec![community.get_shared_inbox_url()?],
359 async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
360 let note = self.to_apub(context.pool()).await?;
362 let post_id = self.post_id;
363 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
365 let community_id = post.community_id;
366 let community = blocking(context.pool(), move |conn| {
367 Community::read(conn, community_id)
371 // Generate a fake delete activity, with the correct object
372 let mut remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?);
374 .set_context(activitystreams::context())
375 .set_id(generate_activity_id(RemoveType::Remove)?)
377 .set_many_ccs(vec![community.get_followers_url()?]);
379 // Undo that fake activity
380 let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?);
382 .set_context(activitystreams::context())
383 .set_id(generate_activity_id(UndoType::Undo)?)
385 .set_many_ccs(vec![community.get_followers_url()?]);
387 send_activity_to_community(
390 vec![community.get_shared_inbox_url()?],
399 #[async_trait::async_trait(?Send)]
400 impl ApubLikeableType for Comment {
401 async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
402 let note = self.to_apub(context.pool()).await?;
404 let post_id = self.post_id;
405 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
407 let community_id = post.community_id;
408 let community = blocking(context.pool(), move |conn| {
409 Community::read(conn, community_id)
413 let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?);
415 .set_context(activitystreams::context())
416 .set_id(generate_activity_id(LikeType::Like)?)
418 .set_many_ccs(vec![community.get_followers_url()?]);
420 send_activity_to_community(
423 vec![community.get_shared_inbox_url()?],
431 async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
432 let note = self.to_apub(context.pool()).await?;
434 let post_id = self.post_id;
435 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
437 let community_id = post.community_id;
438 let community = blocking(context.pool(), move |conn| {
439 Community::read(conn, community_id)
443 let mut dislike = Dislike::new(creator.actor_id.to_owned(), note.into_any_base()?);
445 .set_context(activitystreams::context())
446 .set_id(generate_activity_id(DislikeType::Dislike)?)
448 .set_many_ccs(vec![community.get_followers_url()?]);
450 send_activity_to_community(
453 vec![community.get_shared_inbox_url()?],
461 async fn send_undo_like(
464 context: &LemmyContext,
465 ) -> Result<(), LemmyError> {
466 let note = self.to_apub(context.pool()).await?;
468 let post_id = self.post_id;
469 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
471 let community_id = post.community_id;
472 let community = blocking(context.pool(), move |conn| {
473 Community::read(conn, community_id)
477 let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?);
479 .set_context(activitystreams::context())
480 .set_id(generate_activity_id(DislikeType::Dislike)?)
482 .set_many_ccs(vec![community.get_followers_url()?]);
484 // Undo that fake activity
485 let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?);
487 .set_context(activitystreams::context())
488 .set_id(generate_activity_id(UndoType::Undo)?)
490 .set_many_ccs(vec![community.get_followers_url()?]);
492 send_activity_to_community(
495 vec![community.get_shared_inbox_url()?],
504 struct MentionsAndAddresses {
505 addressed_ccs: Vec<Url>,
510 impl MentionsAndAddresses {
511 fn get_tags(&self) -> Result<Vec<AnyBase>, Error> {
515 .map(|t| t.to_owned().into_any_base())
516 .collect::<Result<Vec<AnyBase>, Error>>()
520 /// This takes a comment, and builds a list of to_addresses, inboxes,
521 /// and mention tags, so they know where to be sent to.
522 /// Addresses are the users / addresses that go in the cc field.
523 async fn collect_non_local_mentions_and_addresses(
525 community: &Community,
526 context: &LemmyContext,
527 ) -> Result<MentionsAndAddresses, LemmyError> {
528 let mut addressed_ccs = vec![community.get_followers_url()?];
530 // Add the mention tag
531 let mut tags = Vec::new();
533 // Get the inboxes for any mentions
534 let mentions = scrape_text_for_mentions(&content)
536 // Filter only the non-local ones
537 .filter(|m| !m.is_local())
538 .collect::<Vec<MentionData>>();
540 let mut mention_inboxes: Vec<Url> = Vec::new();
541 for mention in &mentions {
542 // TODO should it be fetching it every time?
543 if let Ok(actor_id) = fetch_webfinger_url(mention, context.client()).await {
544 debug!("mention actor_id: {}", actor_id);
545 addressed_ccs.push(actor_id.to_owned().to_string().parse()?);
547 let mention_user = get_or_fetch_and_upsert_user(&actor_id, context).await?;
548 let shared_inbox = mention_user.get_shared_inbox_url()?;
550 mention_inboxes.push(shared_inbox);
551 let mut mention_tag = Mention::new();
552 mention_tag.set_href(actor_id).set_name(mention.full_name());
553 tags.push(mention_tag);
557 let mut inboxes = vec![community.get_shared_inbox_url()?];
558 inboxes.extend(mention_inboxes);
559 inboxes = inboxes.into_iter().unique().collect();
561 Ok(MentionsAndAddresses {