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