2 activities::{verify_mod_action, verify_person_in_community},
3 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
5 activities::deletion::{delete::Delete, undo_delete::UndoDelete},
6 objects::tombstone::Tombstone,
9 use lemmy_api_common::blocking;
10 use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject, verify::verify_domains_match};
11 use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
12 use lemmy_utils::LemmyError;
13 use lemmy_websocket::{
14 send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
23 pub async fn send_apub_delete(
25 community: &ApubCommunity,
26 object: DeletableObjects,
28 context: &LemmyContext,
29 ) -> Result<(), LemmyError> {
31 Delete::send(actor, community, object, None, context).await
33 UndoDelete::send(actor, community, object, None, context).await
37 // TODO: remove reason is actually optional in lemmy. we set an empty string in that case, but its
39 pub async fn send_apub_remove(
41 community: &ApubCommunity,
42 object: DeletableObjects,
45 context: &LemmyContext,
46 ) -> Result<(), LemmyError> {
48 Delete::send(actor, community, object, Some(reason), context).await
50 UndoDelete::send(actor, community, object, Some(reason), context).await
54 pub enum DeletableObjects {
55 Community(Box<ApubCommunity>),
56 Comment(Box<ApubComment>),
60 impl DeletableObjects {
61 pub(crate) async fn read_from_db(
63 context: &LemmyContext,
64 ) -> Result<DeletableObjects, LemmyError> {
65 if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
66 return Ok(DeletableObjects::Community(Box::new(c)));
68 if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
69 return Ok(DeletableObjects::Post(Box::new(p)));
71 if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
72 return Ok(DeletableObjects::Comment(Box::new(c)));
74 Err(diesel::NotFound.into())
77 pub(crate) fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
79 DeletableObjects::Community(c) => c.to_tombstone(),
80 DeletableObjects::Comment(c) => c.to_tombstone(),
81 DeletableObjects::Post(p) => p.to_tombstone(),
86 pub(in crate::activities) async fn verify_delete_activity(
88 actor: &ObjectId<ApubPerson>,
89 community: &ApubCommunity,
91 context: &LemmyContext,
92 request_counter: &mut i32,
93 ) -> Result<(), LemmyError> {
94 let object = DeletableObjects::read_from_db(object, context).await?;
96 DeletableObjects::Community(community) => {
98 // can only do this check for local community, in remote case it would try to fetch the
99 // deleted community (which fails)
100 verify_person_in_community(actor, &community, context, request_counter).await?;
102 // community deletion is always a mod (or admin) action
103 verify_mod_action(actor, &community, context, request_counter).await?;
105 DeletableObjects::Post(p) => {
106 verify_delete_activity_post_or_comment(
108 &p.ap_id.clone().into(),
116 DeletableObjects::Comment(c) => {
117 verify_delete_activity_post_or_comment(
119 &c.ap_id.clone().into(),
131 async fn verify_delete_activity_post_or_comment(
132 actor: &ObjectId<ApubPerson>,
134 community: &ApubCommunity,
136 context: &LemmyContext,
137 request_counter: &mut i32,
138 ) -> Result<(), LemmyError> {
139 verify_person_in_community(actor, community, context, request_counter).await?;
141 verify_mod_action(actor, community, context, request_counter).await?;
143 // domain of post ap_id and post.creator ap_id are identical, so we just check the former
144 verify_domains_match(actor.inner(), object_id)?;
149 /// Write deletion or restoring of an object to the database, and send websocket message.
150 /// TODO: we should do something similar for receive_remove_action(), but its much more complicated
151 /// because of the mod log
152 async fn receive_delete_action(
154 actor: &ObjectId<ApubPerson>,
156 context: &LemmyContext,
157 request_counter: &mut i32,
158 ) -> Result<(), LemmyError> {
159 match DeletableObjects::read_from_db(object, context).await? {
160 DeletableObjects::Community(community) => {
162 let mod_ = actor.dereference(context, request_counter).await?;
163 let object = DeletableObjects::Community(community.clone());
164 send_apub_delete(&mod_, &community.clone(), object, true, context).await?;
167 let community = blocking(context.pool(), move |conn| {
168 Community::update_deleted(conn, community.id, deleted)
171 send_community_ws_message(
173 UserOperationCrud::DeleteCommunity,
180 DeletableObjects::Post(post) => {
181 if deleted != post.deleted {
182 let deleted_post = blocking(context.pool(), move |conn| {
183 Post::update_deleted(conn, post.id, deleted)
186 send_post_ws_message(
188 UserOperationCrud::DeletePost,
196 DeletableObjects::Comment(comment) => {
197 if deleted != comment.deleted {
198 let deleted_comment = blocking(context.pool(), move |conn| {
199 Comment::update_deleted(conn, comment.id, deleted)
202 send_comment_ws_message_simple(
204 UserOperationCrud::DeleteComment,