4 announce::{AnnouncableActivities, GetCommunity},
10 verify_delete_activity,
18 context::lemmy_context,
19 fetcher::object_id::ObjectId,
20 objects::{community::ApubCommunity, person::ApubPerson},
22 use activitystreams::{
23 activity::kind::UndoType,
25 primitives::OneOrMany,
30 use lemmy_api_common::blocking;
33 traits::{ActivityFields, ActivityHandler, ActorType},
35 use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
36 use lemmy_utils::LemmyError;
37 use lemmy_websocket::{
38 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
42 use serde::{Deserialize, Serialize};
45 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
46 #[serde(rename_all = "camelCase")]
47 pub struct UndoDelete {
48 actor: ObjectId<ApubPerson>,
51 cc: [ObjectId<ApubCommunity>; 1],
52 #[serde(rename = "type")]
55 #[serde(rename = "@context")]
56 context: OneOrMany<AnyBase>,
61 #[async_trait::async_trait(?Send)]
62 impl ActivityHandler for UndoDelete {
63 type DataType = LemmyContext;
66 context: &Data<LemmyContext>,
67 request_counter: &mut i32,
68 ) -> Result<(), LemmyError> {
69 verify_is_public(&self.to)?;
70 verify_activity(self, &context.settings())?;
71 self.object.verify(context, request_counter).await?;
72 verify_delete_activity(
76 self.object.summary.is_some(),
86 context: &Data<LemmyContext>,
87 request_counter: &mut i32,
88 ) -> Result<(), LemmyError> {
89 if self.object.summary.is_some() {
90 UndoDelete::receive_undo_remove_action(&self.object.object, context).await
92 receive_delete_action(
96 community: UserOperationCrud::EditCommunity,
97 post: UserOperationCrud::EditPost,
98 comment: UserOperationCrud::EditComment,
110 pub(in crate::activities::deletion) async fn send(
112 community: &ApubCommunity,
114 summary: Option<String>,
115 context: &LemmyContext,
116 ) -> Result<(), LemmyError> {
117 let object = Delete::new(actor, community, object_id, summary, context)?;
119 let id = generate_activity_id(
121 &context.settings().get_protocol_and_hostname(),
123 let undo = UndoDelete {
124 actor: ObjectId::new(actor.actor_id()),
127 cc: [ObjectId::new(community.actor_id())],
128 kind: UndoType::Undo,
130 context: lemmy_context(),
131 unparsed: Default::default(),
134 let activity = AnnouncableActivities::UndoDelete(undo);
135 send_to_community(activity, &id, actor, community, vec![], context).await
138 pub(in crate::activities) async fn receive_undo_remove_action(
140 context: &LemmyContext,
141 ) -> Result<(), LemmyError> {
142 use UserOperationCrud::*;
143 match DeletableObjects::read_from_db(object, context).await? {
144 DeletableObjects::Community(community) => {
146 return Err(anyhow!("Only local admin can restore community").into());
148 let deleted_community = blocking(context.pool(), move |conn| {
149 Community::update_removed(conn, community.id, false)
152 send_community_ws_message(deleted_community.id, EditCommunity, None, None, context).await?;
154 DeletableObjects::Post(post) => {
155 let removed_post = blocking(context.pool(), move |conn| {
156 Post::update_removed(conn, post.id, false)
159 send_post_ws_message(removed_post.id, EditPost, None, None, context).await?;
161 DeletableObjects::Comment(comment) => {
162 let removed_comment = blocking(context.pool(), move |conn| {
163 Comment::update_removed(conn, comment.id, false)
166 send_comment_ws_message_simple(removed_comment.id, EditComment, context).await?;
173 #[async_trait::async_trait(?Send)]
174 impl GetCommunity for UndoDelete {
175 async fn get_community(
177 context: &LemmyContext,
178 request_counter: &mut i32,
179 ) -> Result<ApubCommunity, LemmyError> {
180 self.object.get_community(context, request_counter).await