]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/deletion/undo_delete.rs
Remove chatserver (#2919)
[lemmy.git] / crates / apub / src / activities / deletion / undo_delete.rs
1 use crate::{
2   activities::{
3     deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
4     generate_activity_id,
5   },
6   insert_activity,
7   objects::person::ApubPerson,
8   protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete},
9 };
10 use activitypub_federation::{config::Data, kinds::activity::UndoType, traits::ActivityHandler};
11 use lemmy_api_common::context::LemmyContext;
12 use lemmy_db_schema::{
13   source::{
14     comment::{Comment, CommentUpdateForm},
15     community::{Community, CommunityUpdateForm},
16     moderator::{
17       ModRemoveComment,
18       ModRemoveCommentForm,
19       ModRemoveCommunity,
20       ModRemoveCommunityForm,
21       ModRemovePost,
22       ModRemovePostForm,
23     },
24     post::{Post, PostUpdateForm},
25   },
26   traits::Crud,
27 };
28 use lemmy_utils::error::LemmyError;
29 use url::Url;
30
31 #[async_trait::async_trait]
32 impl ActivityHandler for UndoDelete {
33   type DataType = LemmyContext;
34   type Error = LemmyError;
35
36   fn id(&self) -> &Url {
37     &self.id
38   }
39
40   fn actor(&self) -> &Url {
41     self.actor.inner()
42   }
43
44   async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
45     self.object.verify(data).await?;
46     verify_delete_activity(&self.object, self.object.summary.is_some(), data).await?;
47     Ok(())
48   }
49
50   #[tracing::instrument(skip_all)]
51   async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
52     insert_activity(&self.id, &self, false, false, context).await?;
53     if self.object.summary.is_some() {
54       UndoDelete::receive_undo_remove_action(
55         &self.actor.dereference(context).await?,
56         self.object.object.id(),
57         context,
58       )
59       .await
60     } else {
61       receive_delete_action(self.object.object.id(), &self.actor, false, context).await
62     }
63   }
64 }
65
66 impl UndoDelete {
67   #[tracing::instrument(skip_all)]
68   pub(in crate::activities::deletion) fn new(
69     actor: &ApubPerson,
70     object: DeletableObjects,
71     to: Url,
72     community: Option<&Community>,
73     summary: Option<String>,
74     context: &Data<LemmyContext>,
75   ) -> Result<UndoDelete, LemmyError> {
76     let object = Delete::new(actor, object, to.clone(), community, summary, context)?;
77
78     let id = generate_activity_id(
79       UndoType::Undo,
80       &context.settings().get_protocol_and_hostname(),
81     )?;
82     let cc: Option<Url> = community.map(|c| c.actor_id.clone().into());
83     Ok(UndoDelete {
84       actor: actor.actor_id.clone().into(),
85       to: vec![to],
86       object,
87       cc: cc.into_iter().collect(),
88       kind: UndoType::Undo,
89       id,
90       audience: community.map(|c| c.actor_id.clone().into()),
91     })
92   }
93
94   #[tracing::instrument(skip_all)]
95   pub(in crate::activities) async fn receive_undo_remove_action(
96     actor: &ApubPerson,
97     object: &Url,
98     context: &Data<LemmyContext>,
99   ) -> Result<(), LemmyError> {
100     match DeletableObjects::read_from_db(object, context).await? {
101       DeletableObjects::Community(community) => {
102         if community.local {
103           return Err(LemmyError::from_message(
104             "Only local admin can restore community",
105           ));
106         }
107         let form = ModRemoveCommunityForm {
108           mod_person_id: actor.id,
109           community_id: community.id,
110           removed: Some(false),
111           reason: None,
112           expires: None,
113         };
114         ModRemoveCommunity::create(context.pool(), &form).await?;
115         Community::update(
116           context.pool(),
117           community.id,
118           &CommunityUpdateForm::builder().removed(Some(false)).build(),
119         )
120         .await?;
121       }
122       DeletableObjects::Post(post) => {
123         let form = ModRemovePostForm {
124           mod_person_id: actor.id,
125           post_id: post.id,
126           removed: Some(false),
127           reason: None,
128         };
129         ModRemovePost::create(context.pool(), &form).await?;
130         Post::update(
131           context.pool(),
132           post.id,
133           &PostUpdateForm::builder().removed(Some(false)).build(),
134         )
135         .await?;
136       }
137       DeletableObjects::Comment(comment) => {
138         let form = ModRemoveCommentForm {
139           mod_person_id: actor.id,
140           comment_id: comment.id,
141           removed: Some(false),
142           reason: None,
143         };
144         ModRemoveComment::create(context.pool(), &form).await?;
145         Comment::update(
146           context.pool(),
147           comment.id,
148           &CommentUpdateForm::builder().removed(Some(false)).build(),
149         )
150         .await?;
151       }
152       DeletableObjects::PrivateMessage(_) => unimplemented!(),
153     }
154     Ok(())
155   }
156 }