3 community::{announce::AnnouncableActivities, send_to_community},
6 verify_delete_activity,
13 context::lemmy_context,
14 fetcher::object_id::ObjectId,
16 use activitystreams::{
17 activity::kind::DeleteType,
19 primitives::OneOrMany,
23 use lemmy_api_common::blocking;
26 traits::{ActivityFields, ActivityHandler, ActorType},
29 use lemmy_db_queries::{
30 source::{comment::Comment_, community::Community_, post::Post_},
33 use lemmy_db_schema::source::{
40 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<Person>,
71 pub(in crate::activities::deletion) object: Url,
72 pub(in crate::activities::deletion) cc: [ObjectId<Community>; 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_activity(self, &context.settings())?;
94 verify_delete_activity(
98 self.summary.is_some(),
108 context: &Data<LemmyContext>,
109 request_counter: &mut i32,
110 ) -> Result<(), LemmyError> {
111 if let Some(reason) = self.summary {
112 // We set reason to empty string if it doesn't exist, to distinguish between delete and
113 // remove. Here we change it back to option, so we don't write it to db.
114 let reason = if reason.is_empty() {
119 receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
121 receive_delete_action(
125 community: UserOperationCrud::DeleteCommunity,
126 post: UserOperationCrud::DeletePost,
127 comment: UserOperationCrud::DeleteComment,
139 pub(in crate::activities::deletion) fn new(
141 community: &Community,
143 summary: Option<String>,
144 context: &LemmyContext,
145 ) -> Result<Delete, LemmyError> {
147 actor: ObjectId::new(actor.actor_id()),
148 to: [PublicUrl::Public],
150 cc: [ObjectId::new(community.actor_id())],
151 kind: DeleteType::Delete,
153 id: generate_activity_id(
155 &context.settings().get_protocol_and_hostname(),
157 context: lemmy_context(),
158 unparsed: Default::default(),
161 pub(in crate::activities::deletion) async fn send(
163 community: &Community,
165 summary: Option<String>,
166 context: &LemmyContext,
167 ) -> Result<(), LemmyError> {
168 let delete = Delete::new(actor, community, object_id, summary, context)?;
169 let delete_id = delete.id.clone();
171 let activity = AnnouncableActivities::Delete(delete);
172 send_to_community(activity, &delete_id, actor, community, vec![], context).await
176 pub(in crate::activities) async fn receive_remove_action(
177 actor: &ObjectId<Person>,
179 reason: Option<String>,
180 context: &LemmyContext,
181 request_counter: &mut i32,
182 ) -> Result<(), LemmyError> {
183 let actor = actor.dereference(context, request_counter).await?;
184 use UserOperationCrud::*;
185 match DeletableObjects::read_from_db(object, context).await? {
186 DeletableObjects::Community(community) => {
188 return Err(anyhow!("Only local admin can remove community").into());
190 let form = ModRemoveCommunityForm {
191 mod_person_id: actor.id,
192 community_id: community.id,
197 blocking(context.pool(), move |conn| {
198 ModRemoveCommunity::create(conn, &form)
201 let deleted_community = blocking(context.pool(), move |conn| {
202 Community::update_removed(conn, community.id, true)
206 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
208 DeletableObjects::Post(post) => {
209 let form = ModRemovePostForm {
210 mod_person_id: actor.id,
215 blocking(context.pool(), move |conn| {
216 ModRemovePost::create(conn, &form)
219 let removed_post = blocking(context.pool(), move |conn| {
220 Post::update_removed(conn, post.id, true)
224 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
226 DeletableObjects::Comment(comment) => {
227 let form = ModRemoveCommentForm {
228 mod_person_id: actor.id,
229 comment_id: comment.id,
233 blocking(context.pool(), move |conn| {
234 ModRemoveComment::create(conn, &form)
237 let removed_comment = blocking(context.pool(), move |conn| {
238 Comment::update_removed(conn, comment.id, true)
242 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;