1 use crate::activities::{
2 comment::send_websocket_message as send_comment_message,
3 community::send_websocket_message as send_community_message,
4 post::send_websocket_message as send_post_message,
5 removal::remove::RemovePostCommentCommunityOrMod,
8 verify_person_in_community,
10 use activitystreams::activity::kind::UndoType;
12 use lemmy_api_common::blocking;
15 community::get_or_fetch_and_upsert_community,
16 objects::get_or_fetch_and_insert_post_or_comment,
20 use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
21 use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_};
22 use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
23 use lemmy_utils::LemmyError;
24 use lemmy_websocket::{LemmyContext, UserOperationCrud};
27 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
28 #[serde(rename_all = "camelCase")]
29 pub struct UndoRemovePostCommentOrCommunity {
31 object: RemovePostCommentCommunityOrMod,
33 #[serde(rename = "type")]
36 common: ActivityCommonFields,
39 #[async_trait::async_trait(?Send)]
40 impl ActivityHandler for UndoRemovePostCommentOrCommunity {
43 context: &LemmyContext,
44 request_counter: &mut i32,
45 ) -> Result<(), LemmyError> {
46 verify_activity(self.common())?;
47 let object_community =
48 get_or_fetch_and_upsert_community(&self.object.object, context, request_counter).await;
49 // removing a community
50 if object_community.is_ok() {
51 verify_mod_action(&self.common.actor, self.object.object.clone(), context).await?;
53 // removing a post or comment
55 verify_person_in_community(&self.common.actor, &self.cc, context, request_counter).await?;
56 verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await?;
58 self.object.verify(context, request_counter).await?;
59 // dont check that actor and object.actor are identical, so that one mod can
60 // undo the action of another
66 context: &LemmyContext,
67 request_counter: &mut i32,
68 ) -> Result<(), LemmyError> {
69 let object_community =
70 get_or_fetch_and_upsert_community(&self.object.object, context, request_counter).await;
71 // restoring a community
72 if let Ok(community) = object_community {
74 return Err(anyhow!("Only local admin can undo remove community").into());
76 let deleted_community = blocking(context.pool(), move |conn| {
77 Community::update_removed(conn, community.id, false)
81 send_community_message(
83 UserOperationCrud::EditCommunity,
88 // restoring a post or comment
90 match get_or_fetch_and_insert_post_or_comment(&self.object.object, context, request_counter)
93 PostOrComment::Post(post) => {
94 let removed_post = blocking(context.pool(), move |conn| {
95 Post::update_removed(conn, post.id, false)
98 send_post_message(removed_post.id, UserOperationCrud::EditPost, context).await
100 PostOrComment::Comment(comment) => {
101 let removed_comment = blocking(context.pool(), move |conn| {
102 Comment::update_removed(conn, comment.id, false)
105 send_comment_message(
108 UserOperationCrud::EditComment,
117 fn common(&self) -> &ActivityCommonFields {