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