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