]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/create_or_update/comment.rs
Merge different delete activities for better compatibility (fixes #2066) (#2073)
[lemmy.git] / crates / apub / src / activities / create_or_update / comment.rs
1 use crate::{
2   activities::{
3     check_community_deleted_or_removed,
4     community::{announce::GetCommunity, send_activity_in_community},
5     create_or_update::get_comment_notif_recipients,
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_kinds::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   #[tracing::instrument(skip(comment, actor, kind, context))]
32   pub async fn send(
33     comment: ApubComment,
34     actor: &ApubPerson,
35     kind: CreateOrUpdateType,
36     context: &LemmyContext,
37     request_counter: &mut i32,
38   ) -> Result<(), LemmyError> {
39     // TODO: might be helpful to add a comment method to retrieve community directly
40     let post_id = comment.post_id;
41     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
42     let community_id = post.community_id;
43     let community: ApubCommunity = blocking(context.pool(), move |conn| {
44       Community::read(conn, community_id)
45     })
46     .await??
47     .into();
48
49     let id = generate_activity_id(
50       kind.clone(),
51       &context.settings().get_protocol_and_hostname(),
52     )?;
53     let note = comment.into_apub(context).await?;
54
55     let create_or_update = CreateOrUpdateComment {
56       actor: ObjectId::new(actor.actor_id()),
57       to: vec![public()],
58       cc: note.cc.clone(),
59       tag: note.tag.clone(),
60       object: note,
61       kind,
62       id: id.clone(),
63       unparsed: Default::default(),
64     };
65
66     let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
67       .tag
68       .iter()
69       .map(|t| t.href.clone())
70       .map(ObjectId::new)
71       .collect();
72     let mut inboxes = vec![];
73     for t in tagged_users {
74       let person = t
75         .dereference(context, context.client(), request_counter)
76         .await?;
77       inboxes.push(person.shared_inbox_or_inbox_url());
78     }
79
80     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
81     send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
82   }
83 }
84
85 #[async_trait::async_trait(?Send)]
86 impl ActivityHandler for CreateOrUpdateComment {
87   type DataType = LemmyContext;
88
89   #[tracing::instrument(skip_all)]
90   async fn verify(
91     &self,
92     context: &Data<LemmyContext>,
93     request_counter: &mut i32,
94   ) -> Result<(), LemmyError> {
95     verify_is_public(&self.to, &self.cc)?;
96     let post = self.object.get_parents(context, request_counter).await?.0;
97     let community = self.get_community(context, request_counter).await?;
98
99     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
100     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
101     verify_domains_match(self.actor.inner(), self.object.id.inner())?;
102     check_community_deleted_or_removed(&community)?;
103     check_post_deleted_or_removed(&post)?;
104
105     ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
106     Ok(())
107   }
108
109   #[tracing::instrument(skip_all)]
110   async fn receive(
111     self,
112     context: &Data<LemmyContext>,
113     request_counter: &mut i32,
114   ) -> Result<(), LemmyError> {
115     let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
116     let do_send_email = self.kind == CreateOrUpdateType::Create;
117     let recipients = get_comment_notif_recipients(
118       &self.actor,
119       &comment,
120       do_send_email,
121       context,
122       request_counter,
123     )
124     .await?;
125     let notif_type = match self.kind {
126       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
127       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
128     };
129     send_comment_ws_message(
130       comment.id, notif_type, None, None, None, recipients, context,
131     )
132     .await?;
133     Ok(())
134   }
135 }
136
137 #[async_trait::async_trait(?Send)]
138 impl GetCommunity for CreateOrUpdateComment {
139   #[tracing::instrument(skip_all)]
140   async fn get_community(
141     &self,
142     context: &LemmyContext,
143     request_counter: &mut i32,
144   ) -> Result<ApubCommunity, LemmyError> {
145     let post = self.object.get_parents(context, request_counter).await?.0;
146     let community = blocking(context.pool(), move |conn| {
147       Community::read(conn, post.community_id)
148     })
149     .await??;
150     Ok(community.into())
151   }
152 }