]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/comment/create_or_update.rs
3b1ee3932cc87d0f97cd5d0b97e4dac1f0e24e34
[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   #[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.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   #[tracing::instrument(skip_all)]
88   async fn verify(
89     &self,
90     context: &Data<LemmyContext>,
91     request_counter: &mut i32,
92   ) -> Result<(), LemmyError> {
93     verify_is_public(&self.to, &self.cc)?;
94     let post = self.object.get_parents(context, request_counter).await?.0;
95     let community = self.get_community(context, request_counter).await?;
96
97     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
98     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
99     verify_domains_match(self.actor.inner(), self.object.id.inner())?;
100     check_community_deleted_or_removed(&community)?;
101     check_post_deleted_or_removed(&post)?;
102
103     ApubComment::verify(&self.object, self.actor.inner(), context, request_counter).await?;
104     Ok(())
105   }
106
107   #[tracing::instrument(skip_all)]
108   async fn receive(
109     self,
110     context: &Data<LemmyContext>,
111     request_counter: &mut i32,
112   ) -> Result<(), LemmyError> {
113     let comment = ApubComment::from_apub(self.object, context, request_counter).await?;
114     let do_send_email = self.kind == CreateOrUpdateType::Create;
115     let recipients = get_notif_recipients(
116       &self.actor,
117       &comment,
118       do_send_email,
119       context,
120       request_counter,
121     )
122     .await?;
123     let notif_type = match self.kind {
124       CreateOrUpdateType::Create => UserOperationCrud::CreateComment,
125       CreateOrUpdateType::Update => UserOperationCrud::EditComment,
126     };
127     send_comment_ws_message(
128       comment.id, notif_type, None, None, None, recipients, context,
129     )
130     .await?;
131     Ok(())
132   }
133 }
134
135 #[async_trait::async_trait(?Send)]
136 impl GetCommunity for CreateOrUpdateComment {
137   #[tracing::instrument(skip_all)]
138   async fn get_community(
139     &self,
140     context: &LemmyContext,
141     request_counter: &mut i32,
142   ) -> Result<ApubCommunity, LemmyError> {
143     let post = self.object.get_parents(context, request_counter).await?.0;
144     let community = blocking(context.pool(), move |conn| {
145       Community::read(conn, post.community_id)
146     })
147     .await??;
148     Ok(community.into())
149   }
150 }