]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/comment/remove.rs
Fix changelog links
[lemmy.git] / crates / apub / src / activities / comment / remove.rs
1 use crate::activities::{comment::send_websocket_message, verify_mod_action};
2 use activitystreams::activity::kind::RemoveType;
3 use lemmy_api_common::blocking;
4 use crate::{check_is_apub_id_valid, fetcher::objects::get_or_fetch_and_insert_comment};
5 use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew, PublicUrl};
6 use lemmy_db_queries::source::comment::Comment_;
7 use lemmy_db_schema::source::comment::Comment;
8 use lemmy_utils::LemmyError;
9 use lemmy_websocket::{LemmyContext, UserOperationCrud};
10 use url::Url;
11
12 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
13 #[serde(rename_all = "camelCase")]
14 pub struct RemoveComment {
15   to: PublicUrl,
16   pub(in crate::activities::comment) object: Url,
17   cc: [Url; 1],
18   #[serde(rename = "type")]
19   kind: RemoveType,
20   #[serde(flatten)]
21   common: ActivityCommonFields,
22 }
23
24 #[async_trait::async_trait(?Send)]
25 impl ActivityHandlerNew for RemoveComment {
26   async fn verify(&self, context: &LemmyContext, _: &mut i32) -> Result<(), LemmyError> {
27     verify_domains_match(&self.common.actor, self.common.id_unchecked())?;
28     check_is_apub_id_valid(&self.common.actor, false)?;
29     verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await
30   }
31
32   async fn receive(
33     &self,
34     context: &LemmyContext,
35     request_counter: &mut i32,
36   ) -> Result<(), LemmyError> {
37     let comment = get_or_fetch_and_insert_comment(&self.object, context, request_counter).await?;
38
39     let removed_comment = blocking(context.pool(), move |conn| {
40       Comment::update_removed(conn, comment.id, true)
41     })
42     .await??;
43
44     send_websocket_message(
45       removed_comment.id,
46       vec![],
47       UserOperationCrud::EditComment,
48       context,
49     )
50     .await
51   }
52
53   fn common(&self) -> &ActivityCommonFields {
54     &self.common
55   }
56 }