]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/comment/create_or_update.rs
Create and Note always need to tag parent creator, for mastodon notifications
[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::{link::LinkExt, 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())
69       .flatten()
70       .map(|t| ObjectId::new(t.clone()))
71       .collect();
72     let mut inboxes = vec![];
73     for t in tagged_users {
74       let person = t.dereference(context, request_counter).await?;
75       inboxes.push(person.shared_inbox_or_inbox_url());
76     }
77
78     let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
79     send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
80   }
81 }
82
83 #[async_trait::async_trait(?Send)]
84 impl ActivityHandler for CreateOrUpdateComment {
85   type DataType = LemmyContext;
86
87   async fn verify(
88     &self,
89     context: &Data<LemmyContext>,
90     request_counter: &mut i32,
91   ) -> Result<(), LemmyError> {
92     verify_is_public(&self.to, &self.cc)?;
93     let post = self.object.get_parents(context, request_counter).await?.0;
94     let community = self.get_community(context, request_counter).await?;
95
96     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
97     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
98     verify_domains_match(self.actor.inner(), self.object.id.inner())?;
99     check_community_deleted_or_removed(&community)?;
100     check_post_deleted_or_removed(&post)?;
101
102     ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
103     Ok(())
104   }
105
106   async fn receive(
107     self,
108     context: &Data<LemmyContext>,
109     request_counter: &mut i32,
110   ) -> Result<(), LemmyError> {
111     let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
112     let recipients = get_notif_recipients(&self.actor, &comment, context, request_counter).await?;
113     let notif_type = match self.kind {
114       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
115       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
116     };
117     send_comment_ws_message(
118       comment.id, notif_type, None, None, None, recipients, context,
119     )
120     .await?;
121     Ok(())
122   }
123 }
124
125 #[async_trait::async_trait(?Send)]
126 impl GetCommunity for CreateOrUpdateComment {
127   async fn get_community(
128     &self,
129     context: &LemmyContext,
130     request_counter: &mut i32,
131   ) -> Result<ApubCommunity, LemmyError> {
132     let post = self.object.get_parents(context, request_counter).await?.0;
133     let community = blocking(context.pool(), move |conn| {
134       Community::read(conn, post.community_id)
135     })
136     .await??;
137     Ok(community.into())
138   }
139 }