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::activity::kind::DeleteType;
20 use lemmy_api_common::blocking;
21 use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
22 use lemmy_db_queries::{
23 source::{comment::Comment_, community::Community_, post::Post_},
26 use lemmy_db_schema::source::{
33 ModRemoveCommunityForm,
40 use lemmy_utils::LemmyError;
41 use lemmy_websocket::{
42 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
48 /// This is very confusing, because there are four distinct cases to handle:
49 /// - user deletes their post
50 /// - user deletes their comment
51 /// - remote community mod deletes local community
52 /// - remote community deletes itself (triggered by a mod)
54 /// TODO: we should probably change how community deletions work to simplify this. Probably by
55 /// wrapping it in an announce just like other activities, instead of having the community send it.
56 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
57 #[serde(rename_all = "camelCase")]
59 pub(in crate::activities::deletion) to: PublicUrl,
60 pub(in crate::activities::deletion) object: Url,
61 pub(in crate::activities::deletion) cc: [Url; 1],
62 #[serde(rename = "type")]
63 pub(in crate::activities::deletion) kind: DeleteType,
64 /// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user
65 /// deleting their own content.
66 pub(in crate::activities::deletion) summary: Option<String>,
68 pub(in crate::activities::deletion) common: ActivityCommonFields,
71 #[async_trait::async_trait(?Send)]
72 impl ActivityHandler for Delete {
75 context: &LemmyContext,
76 request_counter: &mut i32,
77 ) -> Result<(), LemmyError> {
78 verify_activity(self.common())?;
79 verify_delete_activity(
83 self.summary.is_some(),
93 context: &LemmyContext,
94 request_counter: &mut i32,
95 ) -> Result<(), LemmyError> {
96 if let Some(reason) = self.summary {
97 // We set reason to empty string if it doesn't exist, to distinguish between delete and
98 // remove. Here we change it back to option, so we don't write it to db.
99 let reason = if reason.is_empty() {
104 receive_remove_action(
113 receive_delete_action(
117 community: UserOperationCrud::DeleteCommunity,
118 post: UserOperationCrud::DeletePost,
119 comment: UserOperationCrud::DeleteComment,
129 fn common(&self) -> &ActivityCommonFields {
135 pub(in crate::activities::deletion) async fn send(
137 community: &Community,
139 summary: Option<String>,
140 context: &LemmyContext,
141 ) -> Result<(), LemmyError> {
142 let id = generate_activity_id(DeleteType::Delete)?;
143 let delete = Delete {
144 to: PublicUrl::Public,
146 cc: [community.actor_id()],
147 kind: DeleteType::Delete,
149 common: ActivityCommonFields {
150 context: lemmy_context(),
152 actor: actor.actor_id(),
153 unparsed: Default::default(),
157 let activity = AnnouncableActivities::Delete(delete);
158 send_to_community_new(activity, &id, actor, community, vec![], context).await
162 pub(in crate::activities) async fn receive_remove_action(
165 reason: Option<String>,
166 context: &LemmyContext,
167 request_counter: &mut i32,
168 ) -> Result<(), LemmyError> {
169 let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?;
170 use UserOperationCrud::*;
171 match DeletableObjects::read_from_db(object, context).await? {
172 DeletableObjects::Community(community) => {
174 return Err(anyhow!("Only local admin can remove community").into());
176 let form = ModRemoveCommunityForm {
177 mod_person_id: actor.id,
178 community_id: community.id,
183 blocking(context.pool(), move |conn| {
184 ModRemoveCommunity::create(conn, &form)
187 let deleted_community = blocking(context.pool(), move |conn| {
188 Community::update_removed(conn, community.id, true)
192 send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
194 DeletableObjects::Post(post) => {
195 let form = ModRemovePostForm {
196 mod_person_id: actor.id,
201 blocking(context.pool(), move |conn| {
202 ModRemovePost::create(conn, &form)
205 let removed_post = blocking(context.pool(), move |conn| {
206 Post::update_removed(conn, post.id, true)
210 send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
212 DeletableObjects::Comment(comment) => {
213 let form = ModRemoveCommentForm {
214 mod_person_id: actor.id,
215 comment_id: comment.id,
219 blocking(context.pool(), move |conn| {
220 ModRemoveComment::create(conn, &form)
223 let removed_comment = blocking(context.pool(), move |conn| {
224 Comment::update_removed(conn, comment.id, true)
228 send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;