4 announce::{AnnouncableActivities, GetCommunity},
9 verify_delete_activity,
17 fetcher::object_id::ObjectId,
18 objects::{community::ApubCommunity, person::ApubPerson},
20 use activitystreams::{activity::kind::DeleteType, public, unparsed::Unparsed};
22 use lemmy_api_common::blocking;
25 traits::{ActivityFields, ActivityHandler, ActorType},
27 use lemmy_db_schema::{
35 ModRemoveCommunityForm,
43 use lemmy_utils::LemmyError;
44 use lemmy_websocket::{
45 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
49 use serde::{Deserialize, Serialize};
50 use serde_with::skip_serializing_none;
53 /// This is very confusing, because there are four distinct cases to handle:
54 /// - user deletes their post
55 /// - user deletes their comment
56 /// - remote community mod deletes local community
57 /// - remote community deletes itself (triggered by a mod)
59 /// TODO: we should probably change how community deletions work to simplify this. Probably by
60 /// wrapping it in an announce just like other activities, instead of having the community send it.
61 #[skip_serializing_none]
62 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
63 #[serde(rename_all = "camelCase")]
65 actor: ObjectId<ApubPerson>,
67 pub(in crate::activities::deletion) object: Url,
68 pub(in crate::activities::deletion) cc: Vec<Url>,
69 #[serde(rename = "type")]
71 /// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user
72 /// deleting their own content.
73 pub(in crate::activities::deletion) summary: Option<String>,
79 #[async_trait::async_trait(?Send)]
80 impl ActivityHandler for Delete {
81 type DataType = LemmyContext;
84 context: &Data<LemmyContext>,
85 request_counter: &mut i32,
86 ) -> Result<(), LemmyError> {
87 verify_is_public(&self.to)?;
88 verify_activity(self, &context.settings())?;
89 let community = self.get_community(context, request_counter).await?;
90 verify_delete_activity(
94 self.summary.is_some(),
104 context: &Data<LemmyContext>,
105 request_counter: &mut i32,
106 ) -> Result<(), LemmyError> {
107 if let Some(reason) = self.summary {
108 // We set reason to empty string if it doesn't exist, to distinguish between delete and
109 // remove. Here we change it back to option, so we don't write it to db.
110 let reason = if reason.is_empty() {
115 receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
117 receive_delete_action(
121 community: UserOperationCrud::DeleteCommunity,
122 post: UserOperationCrud::DeletePost,
123 comment: UserOperationCrud::DeleteComment,
135 pub(in crate::activities::deletion) fn new(
137 community: &ApubCommunity,
139 summary: Option<String>,
140 context: &LemmyContext,
141 ) -> Result<Delete, LemmyError> {
143 actor: ObjectId::new(actor.actor_id()),
146 cc: vec![community.actor_id()],
147 kind: DeleteType::Delete,
149 id: generate_activity_id(
151 &context.settings().get_protocol_and_hostname(),
153 unparsed: Default::default(),
156 pub(in crate::activities::deletion) async fn send(
158 community: &ApubCommunity,
160 summary: Option<String>,
161 context: &LemmyContext,
162 ) -> Result<(), LemmyError> {
163 let delete = Delete::new(actor, community, object_id, summary, context)?;
164 let delete_id = delete.id.clone();
166 let activity = AnnouncableActivities::Delete(delete);
167 send_to_community(activity, &delete_id, actor, community, vec![], context).await
171 pub(in crate::activities) async fn receive_remove_action(
172 actor: &ObjectId<ApubPerson>,
174 reason: Option<String>,
175 context: &LemmyContext,
176 request_counter: &mut i32,
177 ) -> Result<(), LemmyError> {
178 let actor = actor.dereference(context, request_counter).await?;
179 use UserOperationCrud::*;
180 match DeletableObjects::read_from_db(object, context).await? {
181 DeletableObjects::Community(community) => {
183 return Err(anyhow!("Only local admin can remove community").into());
185 let form = ModRemoveCommunityForm {
186 mod_person_id: actor.id,
187 community_id: community.id,
192 blocking(context.pool(), move |conn| {
193 ModRemoveCommunity::create(conn, &form)
196 let deleted_community = blocking(context.pool(), move |conn| {
197 Community::update_removed(conn, community.id, true)
201 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
203 DeletableObjects::Post(post) => {
204 let form = ModRemovePostForm {
205 mod_person_id: actor.id,
210 blocking(context.pool(), move |conn| {
211 ModRemovePost::create(conn, &form)
214 let removed_post = blocking(context.pool(), move |conn| {
215 Post::update_removed(conn, post.id, true)
219 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
221 DeletableObjects::Comment(comment) => {
222 let form = ModRemoveCommentForm {
223 mod_person_id: actor.id,
224 comment_id: comment.id,
228 blocking(context.pool(), move |conn| {
229 ModRemoveComment::create(conn, &form)
232 let removed_comment = blocking(context.pool(), move |conn| {
233 Comment::update_removed(conn, comment.id, true)
237 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
243 #[async_trait::async_trait(?Send)]
244 impl GetCommunity for Delete {
245 async fn get_community(
247 context: &LemmyContext,
248 _request_counter: &mut i32,
249 ) -> Result<ApubCommunity, LemmyError> {
250 let community_id = match DeletableObjects::read_from_db(&self.object, context).await? {
251 DeletableObjects::Community(c) => c.id,
252 DeletableObjects::Comment(c) => {
253 let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;
256 DeletableObjects::Post(p) => p.community_id,
258 let community = blocking(context.pool(), move |conn| {
259 Community::read(conn, community_id)