]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/comment/create_or_update.rs
Rewrite fetcher (#1792)
[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(kind.clone())?;
61     let maa = collect_non_local_mentions(comment, &community, context).await?;
62
63     let create_or_update = CreateOrUpdateComment {
64       actor: ObjectId::new(actor.actor_id()),
65       to: [PublicUrl::Public],
66       object: comment.to_apub(context.pool()).await?,
67       cc: maa.ccs,
68       tag: maa.tags,
69       kind,
70       id: id.clone(),
71       context: lemmy_context(),
72       unparsed: Default::default(),
73     };
74
75     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
76     send_to_community_new(activity, &id, actor, &community, maa.inboxes, context).await
77   }
78 }
79
80 #[async_trait::async_trait(?Send)]
81 impl ActivityHandler for CreateOrUpdateComment {
82   async fn verify(
83     &self,
84     context: &LemmyContext,
85     request_counter: &mut i32,
86   ) -> Result<(), LemmyError> {
87     let community = extract_community(&self.cc, context, request_counter).await?;
88     let community_id = ObjectId::new(community.actor_id());
89
90     verify_activity(self)?;
91     verify_person_in_community(&self.actor, &community_id, context, request_counter).await?;
92     verify_domains_match(self.actor.inner(), self.object.id_unchecked())?;
93     // TODO: should add a check that the correct community is in cc (probably needs changes to
94     //       comment deserialization)
95     self.object.verify(context, request_counter).await?;
96     Ok(())
97   }
98
99   async fn receive(
100     self,
101     context: &LemmyContext,
102     request_counter: &mut i32,
103   ) -> Result<(), LemmyError> {
104     let comment =
105       Comment::from_apub(&self.object, context, self.actor.inner(), request_counter).await?;
106     let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?;
107     let notif_type = match self.kind {
108       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
109       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
110     };
111     send_comment_ws_message(
112       comment.id, notif_type, None, None, None, recipients, context,
113     )
114     .await?;
115     Ok(())
116   }
117 }