3 community::announce::AnnouncableActivities,
6 verify_delete_activity,
13 activity_queue::send_to_community_new,
14 extensions::context::lemmy_context,
15 fetcher::person::get_or_fetch_and_upsert_person,
18 use activitystreams::{
19 activity::kind::DeleteType,
21 primitives::OneOrMany,
25 use lemmy_api_common::blocking;
26 use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler};
27 use lemmy_db_queries::{
28 source::{comment::Comment_, community::Community_, post::Post_},
31 use lemmy_db_schema::source::{
38 ModRemoveCommunityForm,
45 use lemmy_utils::LemmyError;
46 use lemmy_websocket::{
47 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
51 use serde::{Deserialize, Serialize};
54 /// This is very confusing, because there are four distinct cases to handle:
55 /// - user deletes their post
56 /// - user deletes their comment
57 /// - remote community mod deletes local community
58 /// - remote community deletes itself (triggered by a mod)
60 /// TODO: we should probably change how community deletions work to simplify this. Probably by
61 /// wrapping it in an announce just like other activities, instead of having the community send it.
62 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
63 #[serde(rename_all = "camelCase")]
67 pub(in crate::activities::deletion) object: Url,
68 pub(in crate::activities::deletion) cc: [Url; 1],
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>,
75 #[serde(rename = "@context")]
76 context: OneOrMany<AnyBase>,
81 #[async_trait::async_trait(?Send)]
82 impl ActivityHandler for Delete {
85 context: &LemmyContext,
86 request_counter: &mut i32,
87 ) -> Result<(), LemmyError> {
88 verify_activity(self)?;
89 verify_delete_activity(
93 self.summary.is_some(),
103 context: &LemmyContext,
104 request_counter: &mut i32,
105 ) -> Result<(), LemmyError> {
106 if let Some(reason) = self.summary {
107 // We set reason to empty string if it doesn't exist, to distinguish between delete and
108 // remove. Here we change it back to option, so we don't write it to db.
109 let reason = if reason.is_empty() {
114 receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
116 receive_delete_action(
120 community: UserOperationCrud::DeleteCommunity,
121 post: UserOperationCrud::DeletePost,
122 comment: UserOperationCrud::DeleteComment,
134 pub(in crate::activities::deletion) fn new(
136 community: &Community,
138 summary: Option<String>,
139 ) -> Result<Delete, LemmyError> {
141 actor: actor.actor_id(),
142 to: [PublicUrl::Public],
144 cc: [community.actor_id()],
145 kind: DeleteType::Delete,
147 id: generate_activity_id(DeleteType::Delete)?,
148 context: lemmy_context(),
149 unparsed: Default::default(),
152 pub(in crate::activities::deletion) async fn send(
154 community: &Community,
156 summary: Option<String>,
157 context: &LemmyContext,
158 ) -> Result<(), LemmyError> {
159 let delete = Delete::new(actor, community, object_id, summary)?;
160 let delete_id = delete.id.clone();
162 let activity = AnnouncableActivities::Delete(delete);
163 send_to_community_new(activity, &delete_id, actor, community, vec![], context).await
167 pub(in crate::activities) async fn receive_remove_action(
170 reason: Option<String>,
171 context: &LemmyContext,
172 request_counter: &mut i32,
173 ) -> Result<(), LemmyError> {
174 let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?;
175 use UserOperationCrud::*;
176 match DeletableObjects::read_from_db(object, context).await? {
177 DeletableObjects::Community(community) => {
179 return Err(anyhow!("Only local admin can remove community").into());
181 let form = ModRemoveCommunityForm {
182 mod_person_id: actor.id,
183 community_id: community.id,
188 blocking(context.pool(), move |conn| {
189 ModRemoveCommunity::create(conn, &form)
192 let deleted_community = blocking(context.pool(), move |conn| {
193 Community::update_removed(conn, community.id, true)
197 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
199 DeletableObjects::Post(post) => {
200 let form = ModRemovePostForm {
201 mod_person_id: actor.id,
206 blocking(context.pool(), move |conn| {
207 ModRemovePost::create(conn, &form)
210 let removed_post = blocking(context.pool(), move |conn| {
211 Post::update_removed(conn, post.id, true)
215 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
217 DeletableObjects::Comment(comment) => {
218 let form = ModRemoveCommentForm {
219 mod_person_id: actor.id,
220 comment_id: comment.id,
224 blocking(context.pool(), move |conn| {
225 ModRemoveComment::create(conn, &form)
228 let removed_comment = blocking(context.pool(), move |conn| {
229 Comment::update_removed(conn, comment.id, true)
233 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;