3 community::{announce::GetCommunity, send_activity_in_community},
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
14 community::ApubCommunity,
17 private_message::ApubPrivateMessage,
19 protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete},
22 use activitypub_federation::{
23 core::object_id::ObjectId,
24 traits::{Actor, ApubObject},
25 utils::verify_domains_match,
27 use activitystreams_kinds::public;
28 use lemmy_db_schema::{
30 comment::{Comment, CommentUpdateForm},
31 community::{Community, CommunityUpdateForm},
33 post::{Post, PostUpdateForm},
34 private_message::{PrivateMessage, PrivateMessageUpdateForm},
38 use lemmy_utils::error::LemmyError;
39 use lemmy_websocket::{
41 send_comment_ws_message_simple,
42 send_community_ws_message,
56 /// Parameter `reason` being set indicates that this is a removal by a mod. If its unset, this
57 /// action was done by a normal user.
58 #[tracing::instrument(skip_all)]
59 pub async fn send_apub_delete_in_community(
62 object: DeletableObjects,
63 reason: Option<String>,
65 context: &LemmyContext,
66 ) -> Result<(), LemmyError> {
67 let actor = ApubPerson::from(actor);
68 let activity = if deleted {
69 let delete = Delete::new(&actor, object, public(), Some(&community), reason, context)?;
70 AnnouncableActivities::Delete(delete)
72 let undo = UndoDelete::new(&actor, object, public(), Some(&community), reason, context)?;
73 AnnouncableActivities::UndoDelete(undo)
75 send_activity_in_community(activity, &actor, &community.into(), vec![], context).await
78 #[tracing::instrument(skip_all)]
79 pub async fn send_apub_delete_private_message(
83 context: &LemmyContext,
84 ) -> Result<(), LemmyError> {
85 let recipient_id = pm.recipient_id;
86 let recipient: ApubPerson = Person::read(context.pool(), recipient_id).await?.into();
88 let deletable = DeletableObjects::PrivateMessage(Box::new(pm.into()));
89 let inbox = vec![recipient.shared_inbox_or_inbox()];
91 let delete = Delete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
92 send_lemmy_activity(context, delete, actor, inbox, true).await?;
94 let undo = UndoDelete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
95 send_lemmy_activity(context, undo, actor, inbox, true).await?;
100 pub enum DeletableObjects {
101 Community(Box<ApubCommunity>),
102 Comment(Box<ApubComment>),
104 PrivateMessage(Box<ApubPrivateMessage>),
107 impl DeletableObjects {
108 #[tracing::instrument(skip_all)]
109 pub(crate) async fn read_from_db(
111 context: &LemmyContext,
112 ) -> Result<DeletableObjects, LemmyError> {
113 if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
114 return Ok(DeletableObjects::Community(Box::new(c)));
116 if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
117 return Ok(DeletableObjects::Post(Box::new(p)));
119 if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
120 return Ok(DeletableObjects::Comment(Box::new(c)));
122 if let Some(p) = ApubPrivateMessage::read_from_apub_id(ap_id.clone(), context).await? {
123 return Ok(DeletableObjects::PrivateMessage(Box::new(p)));
125 Err(diesel::NotFound.into())
128 pub(crate) fn id(&self) -> Url {
130 DeletableObjects::Community(c) => c.actor_id(),
131 DeletableObjects::Comment(c) => c.ap_id.clone().into(),
132 DeletableObjects::Post(p) => p.ap_id.clone().into(),
133 DeletableObjects::PrivateMessage(p) => p.ap_id.clone().into(),
138 #[tracing::instrument(skip_all)]
139 pub(in crate::activities) async fn verify_delete_activity(
142 context: &LemmyContext,
143 request_counter: &mut i32,
144 ) -> Result<(), LemmyError> {
145 let object = DeletableObjects::read_from_db(activity.object.id(), context).await?;
147 DeletableObjects::Community(community) => {
148 verify_is_public(&activity.to, &[])?;
150 // can only do this check for local community, in remote case it would try to fetch the
151 // deleted community (which fails)
152 verify_person_in_community(&activity.actor, &community, context, request_counter).await?;
154 // community deletion is always a mod (or admin) action
157 activity.object.id(),
164 DeletableObjects::Post(p) => {
165 verify_is_public(&activity.to, &[])?;
166 verify_delete_post_or_comment(
168 &p.ap_id.clone().into(),
169 &activity.get_community(context, request_counter).await?,
176 DeletableObjects::Comment(c) => {
177 verify_is_public(&activity.to, &[])?;
178 verify_delete_post_or_comment(
180 &c.ap_id.clone().into(),
181 &activity.get_community(context, request_counter).await?,
188 DeletableObjects::PrivateMessage(_) => {
189 verify_person(&activity.actor, context, request_counter).await?;
190 verify_domains_match(activity.actor.inner(), activity.object.id())?;
196 #[tracing::instrument(skip_all)]
197 async fn verify_delete_post_or_comment(
198 actor: &ObjectId<ApubPerson>,
200 community: &ApubCommunity,
202 context: &LemmyContext,
203 request_counter: &mut i32,
204 ) -> Result<(), LemmyError> {
205 verify_person_in_community(actor, community, context, request_counter).await?;
207 verify_mod_action(actor, object_id, community.id, context, request_counter).await?;
209 // domain of post ap_id and post.creator ap_id are identical, so we just check the former
210 verify_domains_match(actor.inner(), object_id)?;
215 /// Write deletion or restoring of an object to the database, and send websocket message.
216 #[tracing::instrument(skip_all)]
217 async fn receive_delete_action(
219 actor: &ObjectId<ApubPerson>,
221 context: &LemmyContext,
222 request_counter: &mut i32,
223 ) -> Result<(), LemmyError> {
224 match DeletableObjects::read_from_db(object, context).await? {
225 DeletableObjects::Community(community) => {
227 let mod_: Person = actor
228 .dereference(context, local_instance(context).await, request_counter)
232 let object = DeletableObjects::Community(community.clone());
233 let c: Community = community.deref().deref().clone();
234 send_apub_delete_in_community(mod_, c, object, None, true, context).await?;
237 let community = Community::update(
240 &CommunityUpdateForm::builder()
241 .deleted(Some(deleted))
245 send_community_ws_message(
247 UserOperationCrud::DeleteCommunity,
254 DeletableObjects::Post(post) => {
255 if deleted != post.deleted {
256 let deleted_post = Post::update(
259 &PostUpdateForm::builder().deleted(Some(deleted)).build(),
262 send_post_ws_message(
264 UserOperationCrud::DeletePost,
272 DeletableObjects::Comment(comment) => {
273 if deleted != comment.deleted {
274 let deleted_comment = Comment::update(
277 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
280 send_comment_ws_message_simple(
282 UserOperationCrud::DeleteComment,
288 DeletableObjects::PrivateMessage(pm) => {
289 let deleted_private_message = PrivateMessage::update(
292 &PrivateMessageUpdateForm::builder()
293 .deleted(Some(deleted))
299 deleted_private_message.id,
300 UserOperationCrud::DeletePrivateMessage,