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