3 community::{announce::AnnouncableActivities, send_to_community},
6 verify_delete_activity,
14 context::lemmy_context,
15 fetcher::object_id::ObjectId,
16 objects::{community::ApubCommunity, person::ApubPerson},
18 use activitystreams::{
19 activity::kind::DeleteType,
21 primitives::OneOrMany,
26 use lemmy_api_common::blocking;
29 traits::{ActivityFields, ActivityHandler, ActorType},
31 use lemmy_db_schema::{
39 ModRemoveCommunityForm,
47 use lemmy_utils::LemmyError;
48 use lemmy_websocket::{
49 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
53 use serde::{Deserialize, Serialize};
54 use serde_with::skip_serializing_none;
57 /// This is very confusing, because there are four distinct cases to handle:
58 /// - user deletes their post
59 /// - user deletes their comment
60 /// - remote community mod deletes local community
61 /// - remote community deletes itself (triggered by a mod)
63 /// TODO: we should probably change how community deletions work to simplify this. Probably by
64 /// wrapping it in an announce just like other activities, instead of having the community send it.
65 #[skip_serializing_none]
66 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
67 #[serde(rename_all = "camelCase")]
69 actor: ObjectId<ApubPerson>,
71 pub(in crate::activities::deletion) object: Url,
72 pub(in crate::activities::deletion) cc: [ObjectId<ApubCommunity>; 1],
73 #[serde(rename = "type")]
75 /// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user
76 /// deleting their own content.
77 pub(in crate::activities::deletion) summary: Option<String>,
79 #[serde(rename = "@context")]
80 context: OneOrMany<AnyBase>,
85 #[async_trait::async_trait(?Send)]
86 impl ActivityHandler for Delete {
87 type DataType = LemmyContext;
90 context: &Data<LemmyContext>,
91 request_counter: &mut i32,
92 ) -> Result<(), LemmyError> {
93 verify_is_public(&self.to)?;
94 verify_activity(self, &context.settings())?;
95 verify_delete_activity(
99 self.summary.is_some(),
109 context: &Data<LemmyContext>,
110 request_counter: &mut i32,
111 ) -> Result<(), LemmyError> {
112 if let Some(reason) = self.summary {
113 // We set reason to empty string if it doesn't exist, to distinguish between delete and
114 // remove. Here we change it back to option, so we don't write it to db.
115 let reason = if reason.is_empty() {
120 receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
122 receive_delete_action(
126 community: UserOperationCrud::DeleteCommunity,
127 post: UserOperationCrud::DeletePost,
128 comment: UserOperationCrud::DeleteComment,
140 pub(in crate::activities::deletion) fn new(
142 community: &ApubCommunity,
144 summary: Option<String>,
145 context: &LemmyContext,
146 ) -> Result<Delete, LemmyError> {
148 actor: ObjectId::new(actor.actor_id()),
151 cc: [ObjectId::new(community.actor_id())],
152 kind: DeleteType::Delete,
154 id: generate_activity_id(
156 &context.settings().get_protocol_and_hostname(),
158 context: lemmy_context(),
159 unparsed: Default::default(),
162 pub(in crate::activities::deletion) async fn send(
164 community: &ApubCommunity,
166 summary: Option<String>,
167 context: &LemmyContext,
168 ) -> Result<(), LemmyError> {
169 let delete = Delete::new(actor, community, object_id, summary, context)?;
170 let delete_id = delete.id.clone();
172 let activity = AnnouncableActivities::Delete(delete);
173 send_to_community(activity, &delete_id, actor, community, vec![], context).await
177 pub(in crate::activities) async fn receive_remove_action(
178 actor: &ObjectId<ApubPerson>,
180 reason: Option<String>,
181 context: &LemmyContext,
182 request_counter: &mut i32,
183 ) -> Result<(), LemmyError> {
184 let actor = actor.dereference(context, request_counter).await?;
185 use UserOperationCrud::*;
186 match DeletableObjects::read_from_db(object, context).await? {
187 DeletableObjects::Community(community) => {
189 return Err(anyhow!("Only local admin can remove community").into());
191 let form = ModRemoveCommunityForm {
192 mod_person_id: actor.id,
193 community_id: community.id,
198 blocking(context.pool(), move |conn| {
199 ModRemoveCommunity::create(conn, &form)
202 let deleted_community = blocking(context.pool(), move |conn| {
203 Community::update_removed(conn, community.id, true)
207 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
209 DeletableObjects::Post(post) => {
210 let form = ModRemovePostForm {
211 mod_person_id: actor.id,
216 blocking(context.pool(), move |conn| {
217 ModRemovePost::create(conn, &form)
220 let removed_post = blocking(context.pool(), move |conn| {
221 Post::update_removed(conn, post.id, true)
225 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
227 DeletableObjects::Comment(comment) => {
228 let form = ModRemoveCommentForm {
229 mod_person_id: actor.id,
230 comment_id: comment.id,
234 blocking(context.pool(), move |conn| {
235 ModRemoveComment::create(conn, &form)
238 let removed_comment = blocking(context.pool(), move |conn| {
239 Comment::update_removed(conn, comment.id, true)
243 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;