3 community::send_activity_in_community,
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
14 community::ApubCommunity,
17 private_message::ApubPrivateMessage,
20 activities::deletion::{delete::Delete, undo_delete::UndoDelete},
25 use activitypub_federation::{
26 core::object_id::ObjectId,
27 traits::{Actor, ApubObject},
28 utils::verify_domains_match,
30 use activitystreams_kinds::public;
31 use lemmy_api_common::{
34 send_comment_ws_message_simple,
35 send_community_ws_message,
43 use lemmy_db_schema::{
45 comment::{Comment, CommentUpdateForm},
46 community::{Community, CommunityUpdateForm},
48 post::{Post, PostUpdateForm},
49 private_message::{PrivateMessage, PrivateMessageUpdateForm},
53 use lemmy_utils::error::LemmyError;
61 /// Parameter `reason` being set indicates that this is a removal by a mod. If its unset, this
62 /// action was done by a normal user.
63 #[tracing::instrument(skip_all)]
64 pub async fn send_apub_delete_in_community(
67 object: DeletableObjects,
68 reason: Option<String>,
70 context: &LemmyContext,
71 ) -> Result<(), LemmyError> {
72 let actor = ApubPerson::from(actor);
73 let is_mod_action = reason.is_some();
74 let activity = if deleted {
75 let delete = Delete::new(&actor, object, public(), Some(&community), reason, context)?;
76 AnnouncableActivities::Delete(delete)
78 let undo = UndoDelete::new(&actor, object, public(), Some(&community), reason, context)?;
79 AnnouncableActivities::UndoDelete(undo)
81 send_activity_in_community(
92 #[tracing::instrument(skip_all)]
93 pub async fn send_apub_delete_private_message(
97 context: &LemmyContext,
98 ) -> Result<(), LemmyError> {
99 let recipient_id = pm.recipient_id;
100 let recipient: ApubPerson = Person::read(context.pool(), recipient_id).await?.into();
102 let deletable = DeletableObjects::PrivateMessage(Box::new(pm.into()));
103 let inbox = vec![recipient.shared_inbox_or_inbox()];
105 let delete = Delete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
106 send_lemmy_activity(context, delete, actor, inbox, true).await?;
108 let undo = UndoDelete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
109 send_lemmy_activity(context, undo, actor, inbox, true).await?;
114 pub enum DeletableObjects {
115 Community(Box<ApubCommunity>),
116 Comment(Box<ApubComment>),
118 PrivateMessage(Box<ApubPrivateMessage>),
121 impl DeletableObjects {
122 #[tracing::instrument(skip_all)]
123 pub(crate) async fn read_from_db(
125 context: &LemmyContext,
126 ) -> Result<DeletableObjects, LemmyError> {
127 if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
128 return Ok(DeletableObjects::Community(Box::new(c)));
130 if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
131 return Ok(DeletableObjects::Post(Box::new(p)));
133 if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
134 return Ok(DeletableObjects::Comment(Box::new(c)));
136 if let Some(p) = ApubPrivateMessage::read_from_apub_id(ap_id.clone(), context).await? {
137 return Ok(DeletableObjects::PrivateMessage(Box::new(p)));
139 Err(diesel::NotFound.into())
142 pub(crate) fn id(&self) -> Url {
144 DeletableObjects::Community(c) => c.actor_id(),
145 DeletableObjects::Comment(c) => c.ap_id.clone().into(),
146 DeletableObjects::Post(p) => p.ap_id.clone().into(),
147 DeletableObjects::PrivateMessage(p) => p.ap_id.clone().into(),
152 #[tracing::instrument(skip_all)]
153 pub(in crate::activities) async fn verify_delete_activity(
156 context: &LemmyContext,
157 request_counter: &mut i32,
158 ) -> Result<(), LemmyError> {
159 let object = DeletableObjects::read_from_db(activity.object.id(), context).await?;
161 DeletableObjects::Community(community) => {
162 verify_is_public(&activity.to, &[])?;
164 // can only do this check for local community, in remote case it would try to fetch the
165 // deleted community (which fails)
166 verify_person_in_community(&activity.actor, &community, context, request_counter).await?;
168 // community deletion is always a mod (or admin) action
171 activity.object.id(),
178 DeletableObjects::Post(p) => {
179 verify_is_public(&activity.to, &[])?;
180 verify_delete_post_or_comment(
182 &p.ap_id.clone().into(),
183 &activity.community(context, request_counter).await?,
190 DeletableObjects::Comment(c) => {
191 verify_is_public(&activity.to, &[])?;
192 verify_delete_post_or_comment(
194 &c.ap_id.clone().into(),
195 &activity.community(context, request_counter).await?,
202 DeletableObjects::PrivateMessage(_) => {
203 verify_person(&activity.actor, context, request_counter).await?;
204 verify_domains_match(activity.actor.inner(), activity.object.id())?;
210 #[tracing::instrument(skip_all)]
211 async fn verify_delete_post_or_comment(
212 actor: &ObjectId<ApubPerson>,
214 community: &ApubCommunity,
216 context: &LemmyContext,
217 request_counter: &mut i32,
218 ) -> Result<(), LemmyError> {
219 verify_person_in_community(actor, community, context, request_counter).await?;
221 verify_mod_action(actor, object_id, community.id, context, request_counter).await?;
223 // domain of post ap_id and post.creator ap_id are identical, so we just check the former
224 verify_domains_match(actor.inner(), object_id)?;
229 /// Write deletion or restoring of an object to the database, and send websocket message.
230 #[tracing::instrument(skip_all)]
231 async fn receive_delete_action(
233 actor: &ObjectId<ApubPerson>,
235 context: &LemmyContext,
236 request_counter: &mut i32,
237 ) -> Result<(), LemmyError> {
238 match DeletableObjects::read_from_db(object, context).await? {
239 DeletableObjects::Community(community) => {
241 let mod_: Person = actor
242 .dereference(context, local_instance(context).await, request_counter)
246 let object = DeletableObjects::Community(community.clone());
247 let c: Community = community.deref().deref().clone();
248 send_apub_delete_in_community(mod_, c, object, None, true, context).await?;
251 let community = Community::update(
254 &CommunityUpdateForm::builder()
255 .deleted(Some(deleted))
259 send_community_ws_message(
261 UserOperationCrud::DeleteCommunity,
268 DeletableObjects::Post(post) => {
269 if deleted != post.deleted {
270 let deleted_post = Post::update(
273 &PostUpdateForm::builder().deleted(Some(deleted)).build(),
276 send_post_ws_message(
278 UserOperationCrud::DeletePost,
286 DeletableObjects::Comment(comment) => {
287 if deleted != comment.deleted {
288 let deleted_comment = Comment::update(
291 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
294 send_comment_ws_message_simple(
296 UserOperationCrud::DeleteComment,
302 DeletableObjects::PrivateMessage(pm) => {
303 let deleted_private_message = PrivateMessage::update(
306 &PrivateMessageUpdateForm::builder()
307 .deleted(Some(deleted))
313 deleted_private_message.id,
314 UserOperationCrud::DeletePrivateMessage,