]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/comment/create_or_update.rs
eab2a5771a4b7a04c0c231d7380faa52f37fcc49
[lemmy.git] / crates / apub / src / activities / comment / create_or_update.rs
1 use crate::{
2   activities::{
3     check_community_deleted_or_removed,
4     comment::{collect_non_local_mentions, get_notif_recipients},
5     community::{announce::GetCommunity, send_activity_in_community},
6     generate_activity_id,
7     verify_activity,
8     verify_is_public,
9     verify_person_in_community,
10   },
11   activity_lists::AnnouncableActivities,
12   objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
13   protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
14 };
15 use activitystreams::public;
16 use lemmy_api_common::{blocking, check_post_deleted_or_removed};
17 use lemmy_apub_lib::{
18   data::Data,
19   object_id::ObjectId,
20   traits::{ActivityHandler, ActorType, ApubObject},
21   verify::verify_domains_match,
22 };
23 use lemmy_db_schema::{
24   source::{community::Community, post::Post},
25   traits::Crud,
26 };
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperationCrud};
29
30 impl CreateOrUpdateComment {
31   pub async fn send(
32     comment: ApubComment,
33     actor: &ApubPerson,
34     kind: CreateOrUpdateType,
35     context: &LemmyContext,
36   ) -> Result<(), LemmyError> {
37     // TODO: might be helpful to add a comment method to retrieve community directly
38     let post_id = comment.post_id;
39     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
40     let community_id = post.community_id;
41     let community: ApubCommunity = blocking(context.pool(), move |conn| {
42       Community::read(conn, community_id)
43     })
44     .await??
45     .into();
46
47     let id = generate_activity_id(
48       kind.clone(),
49       &context.settings().get_protocol_and_hostname(),
50     )?;
51     let maa = collect_non_local_mentions(&comment, &community, context).await?;
52
53     let create_or_update = CreateOrUpdateComment {
54       actor: ObjectId::new(actor.actor_id()),
55       to: vec![public()],
56       object: comment.into_apub(context).await?,
57       cc: maa.ccs,
58       tag: maa.tags,
59       kind,
60       id: id.clone(),
61       unparsed: Default::default(),
62     };
63
64     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
65     send_activity_in_community(activity, &id, actor, &community, maa.inboxes, context).await
66   }
67 }
68
69 #[async_trait::async_trait(?Send)]
70 impl ActivityHandler for CreateOrUpdateComment {
71   type DataType = LemmyContext;
72
73   async fn verify(
74     &self,
75     context: &Data<LemmyContext>,
76     request_counter: &mut i32,
77   ) -> Result<(), LemmyError> {
78     verify_is_public(&self.to, &self.cc)?;
79     let post = self.object.get_parents(context, request_counter).await?.0;
80     let community = self.get_community(context, request_counter).await?;
81
82     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
83     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
84     verify_domains_match(self.actor.inner(), self.object.id.inner())?;
85     check_community_deleted_or_removed(&community)?;
86     check_post_deleted_or_removed(&post)?;
87
88     ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
89     Ok(())
90   }
91
92   async fn receive(
93     self,
94     context: &Data<LemmyContext>,
95     request_counter: &mut i32,
96   ) -> Result<(), LemmyError> {
97     let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
98     let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?;
99     let notif_type = match self.kind {
100       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
101       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
102     };
103     send_comment_ws_message(
104       comment.id, notif_type, None, None, None, recipients, context,
105     )
106     .await?;
107     Ok(())
108   }
109 }
110
111 #[async_trait::async_trait(?Send)]
112 impl GetCommunity for CreateOrUpdateComment {
113   async fn get_community(
114     &self,
115     context: &LemmyContext,
116     request_counter: &mut i32,
117   ) -> Result<ApubCommunity, LemmyError> {
118     let post = self.object.get_parents(context, request_counter).await?.0;
119     let community = blocking(context.pool(), move |conn| {
120       Community::read(conn, post.community_id)
121     })
122     .await??;
123     Ok(community.into())
124   }
125 }