1 use activitystreams::{activity::kind::UndoType, public};
5 use lemmy_api_common::blocking;
8 traits::{ActivityHandler, ActorType},
10 use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
11 use lemmy_utils::LemmyError;
12 use lemmy_websocket::{
13 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
20 community::{announce::GetCommunity, send_to_community},
21 deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
26 activity_lists::AnnouncableActivities,
27 fetcher::object_id::ObjectId,
28 objects::{community::ApubCommunity, person::ApubPerson},
29 protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete},
32 #[async_trait::async_trait(?Send)]
33 impl ActivityHandler for UndoDelete {
34 type DataType = LemmyContext;
37 context: &Data<LemmyContext>,
38 request_counter: &mut i32,
39 ) -> Result<(), LemmyError> {
40 verify_is_public(&self.to)?;
41 verify_activity(self, &context.settings())?;
42 self.object.verify(context, request_counter).await?;
43 let community = self.get_community(context, request_counter).await?;
44 verify_delete_activity(
48 self.object.summary.is_some(),
58 context: &Data<LemmyContext>,
59 request_counter: &mut i32,
60 ) -> Result<(), LemmyError> {
61 if self.object.summary.is_some() {
62 UndoDelete::receive_undo_remove_action(&self.object.object, context).await
64 receive_delete_action(
77 pub(in crate::activities::deletion) async fn send(
79 community: &ApubCommunity,
81 summary: Option<String>,
82 context: &LemmyContext,
83 ) -> Result<(), LemmyError> {
84 let object = Delete::new(actor, community, object_id, summary, context)?;
86 let id = generate_activity_id(
88 &context.settings().get_protocol_and_hostname(),
90 let undo = UndoDelete {
91 actor: ObjectId::new(actor.actor_id()),
94 cc: vec![community.actor_id()],
97 unparsed: Default::default(),
100 let activity = AnnouncableActivities::UndoDelete(undo);
101 send_to_community(activity, &id, actor, community, vec![], context).await
104 pub(in crate::activities) async fn receive_undo_remove_action(
106 context: &LemmyContext,
107 ) -> Result<(), LemmyError> {
108 use UserOperationCrud::*;
109 match DeletableObjects::read_from_db(object, context).await? {
110 DeletableObjects::Community(community) => {
112 return Err(anyhow!("Only local admin can restore community").into());
114 let deleted_community = blocking(context.pool(), move |conn| {
115 Community::update_removed(conn, community.id, false)
118 send_community_ws_message(deleted_community.id, EditCommunity, None, None, context).await?;
120 DeletableObjects::Post(post) => {
121 let removed_post = blocking(context.pool(), move |conn| {
122 Post::update_removed(conn, post.id, false)
125 send_post_ws_message(removed_post.id, EditPost, None, None, context).await?;
127 DeletableObjects::Comment(comment) => {
128 let removed_comment = blocking(context.pool(), move |conn| {
129 Comment::update_removed(conn, comment.id, false)
132 send_comment_ws_message_simple(removed_comment.id, EditComment, context).await?;
139 #[async_trait::async_trait(?Send)]
140 impl GetCommunity for UndoDelete {
141 async fn get_community(
143 context: &LemmyContext,
144 request_counter: &mut i32,
145 ) -> Result<ApubCommunity, LemmyError> {
146 self.object.get_community(context, request_counter).await