]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/comment/create_or_update.rs
Rewrite remaining activities (#1712)
[lemmy.git] / crates / apub / src / activities / comment / create_or_update.rs
1 use crate::{
2   activities::{
3     comment::{collect_non_local_mentions, get_notif_recipients},
4     community::announce::AnnouncableActivities,
5     extract_community,
6     generate_activity_id,
7     verify_activity,
8     verify_person_in_community,
9     CreateOrUpdateType,
10   },
11   activity_queue::send_to_community_new,
12   extensions::context::lemmy_context,
13   objects::{comment::Note, FromApub, ToApub},
14   ActorType,
15 };
16 use activitystreams::{base::AnyBase, link::Mention, primitives::OneOrMany, unparsed::Unparsed};
17 use lemmy_api_common::blocking;
18 use lemmy_apub_lib::{values::PublicUrl, verify_domains_match, ActivityFields, ActivityHandler};
19 use lemmy_db_queries::Crud;
20 use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
21 use lemmy_utils::LemmyError;
22 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
23 use serde::{Deserialize, Serialize};
24 use url::Url;
25
26 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
27 #[serde(rename_all = "camelCase")]
28 pub struct CreateOrUpdateComment {
29   actor: Url,
30   to: PublicUrl,
31   object: Note,
32   cc: Vec<Url>,
33   tag: Vec<Mention>,
34   #[serde(rename = "type")]
35   kind: CreateOrUpdateType,
36   id: Url,
37   #[serde(rename = "@context")]
38   context: OneOrMany<AnyBase>,
39   #[serde(flatten)]
40   unparsed: Unparsed,
41 }
42
43 impl CreateOrUpdateComment {
44   pub async fn send(
45     comment: &Comment,
46     actor: &Person,
47     kind: CreateOrUpdateType,
48     context: &LemmyContext,
49   ) -> Result<(), LemmyError> {
50     // TODO: might be helpful to add a comment method to retrieve community directly
51     let post_id = comment.post_id;
52     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
53     let community_id = post.community_id;
54     let community = blocking(context.pool(), move |conn| {
55       Community::read(conn, community_id)
56     })
57     .await??;
58
59     let id = generate_activity_id(kind.clone())?;
60     let maa = collect_non_local_mentions(comment, &community, context).await?;
61
62     let create_or_update = CreateOrUpdateComment {
63       actor: actor.actor_id(),
64       to: PublicUrl::Public,
65       object: comment.to_apub(context.pool()).await?,
66       cc: maa.ccs,
67       tag: maa.tags,
68       kind,
69       id: id.clone(),
70       context: lemmy_context(),
71       unparsed: Default::default(),
72     };
73
74     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
75     send_to_community_new(activity, &id, actor, &community, maa.inboxes, context).await
76   }
77 }
78
79 #[async_trait::async_trait(?Send)]
80 impl ActivityHandler for CreateOrUpdateComment {
81   async fn verify(
82     &self,
83     context: &LemmyContext,
84     request_counter: &mut i32,
85   ) -> Result<(), LemmyError> {
86     let community = extract_community(&self.cc, context, request_counter).await?;
87
88     verify_activity(self)?;
89     verify_person_in_community(&self.actor, &community.actor_id(), context, request_counter)
90       .await?;
91     verify_domains_match(&self.actor, self.object.id_unchecked())?;
92     // TODO: should add a check that the correct community is in cc (probably needs changes to
93     //       comment deserialization)
94     self.object.verify(context, request_counter).await?;
95     Ok(())
96   }
97
98   async fn receive(
99     self,
100     context: &LemmyContext,
101     request_counter: &mut i32,
102   ) -> Result<(), LemmyError> {
103     let comment = Comment::from_apub(&self.object, context, &self.actor, request_counter).await?;
104     let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?;
105     let notif_type = match self.kind {
106       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
107       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
108     };
109     send_comment_ws_message(
110       comment.id, notif_type, None, None, None, recipients, context,
111     )
112     .await?;
113     Ok(())
114   }
115 }