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_api_common::utils::blocking;
29 use lemmy_db_schema::{
35 private_message::PrivateMessage,
39 use lemmy_utils::error::LemmyError;
40 use lemmy_websocket::{
42 send_comment_ws_message_simple,
43 send_community_ws_message,
57 /// Parameter `reason` being set indicates that this is a removal by a mod. If its unset, this
58 /// action was done by a normal user.
59 #[tracing::instrument(skip_all)]
60 pub async fn send_apub_delete_in_community(
63 object: DeletableObjects,
64 reason: Option<String>,
66 context: &LemmyContext,
67 ) -> Result<(), LemmyError> {
68 let actor = ApubPerson::from(actor);
69 let activity = if deleted {
70 let delete = Delete::new(&actor, object, public(), Some(&community), reason, context)?;
71 AnnouncableActivities::Delete(delete)
73 let undo = UndoDelete::new(&actor, object, public(), Some(&community), reason, context)?;
74 AnnouncableActivities::UndoDelete(undo)
76 send_activity_in_community(activity, &actor, &community.into(), vec![], context).await
79 #[tracing::instrument(skip_all)]
80 pub async fn send_apub_delete_private_message(
84 context: &LemmyContext,
85 ) -> Result<(), LemmyError> {
86 let recipient_id = pm.recipient_id;
87 let recipient: ApubPerson =
88 blocking(context.pool(), move |conn| Person::read(conn, recipient_id))
92 let deletable = DeletableObjects::PrivateMessage(Box::new(pm.into()));
93 let inbox = vec![recipient.shared_inbox_or_inbox()];
95 let delete = Delete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
96 send_lemmy_activity(context, delete, actor, inbox, true).await?;
98 let undo = UndoDelete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
99 send_lemmy_activity(context, undo, actor, inbox, true).await?;
104 pub enum DeletableObjects {
105 Community(Box<ApubCommunity>),
106 Comment(Box<ApubComment>),
108 PrivateMessage(Box<ApubPrivateMessage>),
111 impl DeletableObjects {
112 #[tracing::instrument(skip_all)]
113 pub(crate) async fn read_from_db(
115 context: &LemmyContext,
116 ) -> Result<DeletableObjects, LemmyError> {
117 if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
118 return Ok(DeletableObjects::Community(Box::new(c)));
120 if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
121 return Ok(DeletableObjects::Post(Box::new(p)));
123 if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
124 return Ok(DeletableObjects::Comment(Box::new(c)));
126 if let Some(p) = ApubPrivateMessage::read_from_apub_id(ap_id.clone(), context).await? {
127 return Ok(DeletableObjects::PrivateMessage(Box::new(p)));
129 Err(diesel::NotFound.into())
132 pub(crate) fn id(&self) -> Url {
134 DeletableObjects::Community(c) => c.actor_id(),
135 DeletableObjects::Comment(c) => c.ap_id.clone().into(),
136 DeletableObjects::Post(p) => p.ap_id.clone().into(),
137 DeletableObjects::PrivateMessage(p) => p.ap_id.clone().into(),
142 #[tracing::instrument(skip_all)]
143 pub(in crate::activities) async fn verify_delete_activity(
146 context: &LemmyContext,
147 request_counter: &mut i32,
148 ) -> Result<(), LemmyError> {
149 let object = DeletableObjects::read_from_db(activity.object.id(), context).await?;
151 DeletableObjects::Community(community) => {
152 verify_is_public(&activity.to, &[])?;
154 // can only do this check for local community, in remote case it would try to fetch the
155 // deleted community (which fails)
156 verify_person_in_community(&activity.actor, &community, context, request_counter).await?;
158 // community deletion is always a mod (or admin) action
161 activity.object.id(),
168 DeletableObjects::Post(p) => {
169 verify_is_public(&activity.to, &[])?;
170 verify_delete_post_or_comment(
172 &p.ap_id.clone().into(),
173 &activity.get_community(context, request_counter).await?,
180 DeletableObjects::Comment(c) => {
181 verify_is_public(&activity.to, &[])?;
182 verify_delete_post_or_comment(
184 &c.ap_id.clone().into(),
185 &activity.get_community(context, request_counter).await?,
192 DeletableObjects::PrivateMessage(_) => {
193 verify_person(&activity.actor, context, request_counter).await?;
194 verify_domains_match(activity.actor.inner(), activity.object.id())?;
200 #[tracing::instrument(skip_all)]
201 async fn verify_delete_post_or_comment(
202 actor: &ObjectId<ApubPerson>,
204 community: &ApubCommunity,
206 context: &LemmyContext,
207 request_counter: &mut i32,
208 ) -> Result<(), LemmyError> {
209 verify_person_in_community(actor, community, context, request_counter).await?;
211 verify_mod_action(actor, object_id, community.id, context, request_counter).await?;
213 // domain of post ap_id and post.creator ap_id are identical, so we just check the former
214 verify_domains_match(actor.inner(), object_id)?;
219 /// Write deletion or restoring of an object to the database, and send websocket message.
220 #[tracing::instrument(skip_all)]
221 async fn receive_delete_action(
223 actor: &ObjectId<ApubPerson>,
225 context: &LemmyContext,
226 request_counter: &mut i32,
227 ) -> Result<(), LemmyError> {
228 match DeletableObjects::read_from_db(object, context).await? {
229 DeletableObjects::Community(community) => {
231 let mod_: Person = actor
232 .dereference(context, local_instance(context), request_counter)
236 let object = DeletableObjects::Community(community.clone());
237 let c: Community = community.deref().deref().clone();
238 send_apub_delete_in_community(mod_, c, object, None, true, context).await?;
241 let community = blocking(context.pool(), move |conn| {
242 Community::update_deleted(conn, community.id, deleted)
245 send_community_ws_message(
247 UserOperationCrud::DeleteCommunity,
254 DeletableObjects::Post(post) => {
255 if deleted != post.deleted {
256 let deleted_post = blocking(context.pool(), move |conn| {
257 Post::update_deleted(conn, post.id, deleted)
260 send_post_ws_message(
262 UserOperationCrud::DeletePost,
270 DeletableObjects::Comment(comment) => {
271 if deleted != comment.deleted {
272 let deleted_comment = blocking(context.pool(), move |conn| {
273 Comment::update_deleted(conn, comment.id, deleted)
276 send_comment_ws_message_simple(
278 UserOperationCrud::DeleteComment,
284 DeletableObjects::PrivateMessage(pm) => {
285 let deleted_private_message = blocking(context.pool(), move |conn| {
286 PrivateMessage::update_deleted(conn, pm.id, deleted)
291 deleted_private_message.id,
292 UserOperationCrud::DeletePrivateMessage,