3 community::{announce::AnnouncableActivities, send_to_community},
6 verify_delete_activity,
13 context::lemmy_context,
14 fetcher::object_id::ObjectId,
15 objects::{community::ApubCommunity, person::ApubPerson},
17 use activitystreams::{
18 activity::kind::DeleteType,
20 primitives::OneOrMany,
24 use lemmy_api_common::blocking;
27 traits::{ActivityFields, ActivityHandler, ActorType},
30 use lemmy_db_schema::{
38 ModRemoveCommunityForm,
46 use lemmy_utils::LemmyError;
47 use lemmy_websocket::{
48 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
52 use serde::{Deserialize, Serialize};
53 use serde_with::skip_serializing_none;
56 /// This is very confusing, because there are four distinct cases to handle:
57 /// - user deletes their post
58 /// - user deletes their comment
59 /// - remote community mod deletes local community
60 /// - remote community deletes itself (triggered by a mod)
62 /// TODO: we should probably change how community deletions work to simplify this. Probably by
63 /// wrapping it in an announce just like other activities, instead of having the community send it.
64 #[skip_serializing_none]
65 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
66 #[serde(rename_all = "camelCase")]
68 actor: ObjectId<ApubPerson>,
70 pub(in crate::activities::deletion) object: Url,
71 pub(in crate::activities::deletion) cc: [ObjectId<ApubCommunity>; 1],
72 #[serde(rename = "type")]
74 /// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user
75 /// deleting their own content.
76 pub(in crate::activities::deletion) summary: Option<String>,
78 #[serde(rename = "@context")]
79 context: OneOrMany<AnyBase>,
84 #[async_trait::async_trait(?Send)]
85 impl ActivityHandler for Delete {
86 type DataType = LemmyContext;
89 context: &Data<LemmyContext>,
90 request_counter: &mut i32,
91 ) -> Result<(), LemmyError> {
92 verify_activity(self, &context.settings())?;
93 verify_delete_activity(
97 self.summary.is_some(),
107 context: &Data<LemmyContext>,
108 request_counter: &mut i32,
109 ) -> Result<(), LemmyError> {
110 if let Some(reason) = self.summary {
111 // We set reason to empty string if it doesn't exist, to distinguish between delete and
112 // remove. Here we change it back to option, so we don't write it to db.
113 let reason = if reason.is_empty() {
118 receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
120 receive_delete_action(
124 community: UserOperationCrud::DeleteCommunity,
125 post: UserOperationCrud::DeletePost,
126 comment: UserOperationCrud::DeleteComment,
138 pub(in crate::activities::deletion) fn new(
140 community: &ApubCommunity,
142 summary: Option<String>,
143 context: &LemmyContext,
144 ) -> Result<Delete, LemmyError> {
146 actor: ObjectId::new(actor.actor_id()),
147 to: [PublicUrl::Public],
149 cc: [ObjectId::new(community.actor_id())],
150 kind: DeleteType::Delete,
152 id: generate_activity_id(
154 &context.settings().get_protocol_and_hostname(),
156 context: lemmy_context(),
157 unparsed: Default::default(),
160 pub(in crate::activities::deletion) async fn send(
162 community: &ApubCommunity,
164 summary: Option<String>,
165 context: &LemmyContext,
166 ) -> Result<(), LemmyError> {
167 let delete = Delete::new(actor, community, object_id, summary, context)?;
168 let delete_id = delete.id.clone();
170 let activity = AnnouncableActivities::Delete(delete);
171 send_to_community(activity, &delete_id, actor, community, vec![], context).await
175 pub(in crate::activities) async fn receive_remove_action(
176 actor: &ObjectId<ApubPerson>,
178 reason: Option<String>,
179 context: &LemmyContext,
180 request_counter: &mut i32,
181 ) -> Result<(), LemmyError> {
182 let actor = actor.dereference(context, request_counter).await?;
183 use UserOperationCrud::*;
184 match DeletableObjects::read_from_db(object, context).await? {
185 DeletableObjects::Community(community) => {
187 return Err(anyhow!("Only local admin can remove community").into());
189 let form = ModRemoveCommunityForm {
190 mod_person_id: actor.id,
191 community_id: community.id,
196 blocking(context.pool(), move |conn| {
197 ModRemoveCommunity::create(conn, &form)
200 let deleted_community = blocking(context.pool(), move |conn| {
201 Community::update_removed(conn, community.id, true)
205 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
207 DeletableObjects::Post(post) => {
208 let form = ModRemovePostForm {
209 mod_person_id: actor.id,
214 blocking(context.pool(), move |conn| {
215 ModRemovePost::create(conn, &form)
218 let removed_post = blocking(context.pool(), move |conn| {
219 Post::update_removed(conn, post.id, true)
223 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
225 DeletableObjects::Comment(comment) => {
226 let form = ModRemoveCommentForm {
227 mod_person_id: actor.id,
228 comment_id: comment.id,
232 blocking(context.pool(), move |conn| {
233 ModRemoveComment::create(conn, &form)
236 let removed_comment = blocking(context.pool(), move |conn| {
237 Comment::update_removed(conn, comment.id, true)
241 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;