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 #[tracing::instrument(skip_all)]
24 pub async fn send_apub_delete(
26 community: &ApubCommunity,
27 object: DeletableObjects,
29 context: &LemmyContext,
30 ) -> Result<(), LemmyError> {
32 Delete::send(actor, community, object, None, context).await
34 UndoDelete::send(actor, community, object, None, context).await
38 // TODO: remove reason is actually optional in lemmy. we set an empty string in that case, but its
40 #[tracing::instrument(skip_all)]
41 pub async fn send_apub_remove(
43 community: &ApubCommunity,
44 object: DeletableObjects,
47 context: &LemmyContext,
48 ) -> Result<(), LemmyError> {
50 Delete::send(actor, community, object, Some(reason), context).await
52 UndoDelete::send(actor, community, object, Some(reason), context).await
56 pub enum DeletableObjects {
57 Community(Box<ApubCommunity>),
58 Comment(Box<ApubComment>),
62 impl DeletableObjects {
63 #[tracing::instrument(skip_all)]
64 pub(crate) async fn read_from_db(
66 context: &LemmyContext,
67 ) -> Result<DeletableObjects, LemmyError> {
68 if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
69 return Ok(DeletableObjects::Community(Box::new(c)));
71 if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
72 return Ok(DeletableObjects::Post(Box::new(p)));
74 if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
75 return Ok(DeletableObjects::Comment(Box::new(c)));
77 Err(diesel::NotFound.into())
80 pub(crate) fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
82 DeletableObjects::Community(c) => c.to_tombstone(),
83 DeletableObjects::Comment(c) => c.to_tombstone(),
84 DeletableObjects::Post(p) => p.to_tombstone(),
89 #[tracing::instrument(skip_all)]
90 pub(in crate::activities) async fn verify_delete_activity(
92 actor: &ObjectId<ApubPerson>,
93 community: &ApubCommunity,
95 context: &LemmyContext,
96 request_counter: &mut i32,
97 ) -> Result<(), LemmyError> {
98 let object = DeletableObjects::read_from_db(object, context).await?;
100 DeletableObjects::Community(community) => {
102 // can only do this check for local community, in remote case it would try to fetch the
103 // deleted community (which fails)
104 verify_person_in_community(actor, &community, context, request_counter).await?;
106 // community deletion is always a mod (or admin) action
107 verify_mod_action(actor, &community, context, request_counter).await?;
109 DeletableObjects::Post(p) => {
110 verify_delete_activity_post_or_comment(
112 &p.ap_id.clone().into(),
120 DeletableObjects::Comment(c) => {
121 verify_delete_activity_post_or_comment(
123 &c.ap_id.clone().into(),
135 #[tracing::instrument(skip_all)]
136 async fn verify_delete_activity_post_or_comment(
137 actor: &ObjectId<ApubPerson>,
139 community: &ApubCommunity,
141 context: &LemmyContext,
142 request_counter: &mut i32,
143 ) -> Result<(), LemmyError> {
144 verify_person_in_community(actor, community, context, request_counter).await?;
146 verify_mod_action(actor, community, context, request_counter).await?;
148 // domain of post ap_id and post.creator ap_id are identical, so we just check the former
149 verify_domains_match(actor.inner(), object_id)?;
154 /// Write deletion or restoring of an object to the database, and send websocket message.
155 /// TODO: we should do something similar for receive_remove_action(), but its much more complicated
156 /// because of the mod log
157 #[tracing::instrument(skip_all)]
158 async fn receive_delete_action(
160 actor: &ObjectId<ApubPerson>,
162 context: &LemmyContext,
163 request_counter: &mut i32,
164 ) -> Result<(), LemmyError> {
165 match DeletableObjects::read_from_db(object, context).await? {
166 DeletableObjects::Community(community) => {
169 .dereference(context, context.client(), request_counter)
171 let object = DeletableObjects::Community(community.clone());
172 send_apub_delete(&mod_, &community.clone(), object, true, context).await?;
175 let community = blocking(context.pool(), move |conn| {
176 Community::update_deleted(conn, community.id, deleted)
179 send_community_ws_message(
181 UserOperationCrud::DeleteCommunity,
188 DeletableObjects::Post(post) => {
189 if deleted != post.deleted {
190 let deleted_post = blocking(context.pool(), move |conn| {
191 Post::update_deleted(conn, post.id, deleted)
194 send_post_ws_message(
196 UserOperationCrud::DeletePost,
204 DeletableObjects::Comment(comment) => {
205 if deleted != comment.deleted {
206 let deleted_comment = blocking(context.pool(), move |conn| {
207 Comment::update_deleted(conn, comment.id, deleted)
210 send_comment_ws_message_simple(
212 UserOperationCrud::DeleteComment,