3 community::{announce::GetCommunity, send_activity_in_community},
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
13 community::ApubCommunity,
16 private_message::ApubPrivateMessage,
18 protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete},
20 use activitystreams_kinds::public;
21 use lemmy_api_common::blocking;
24 traits::{ActorType, ApubObject},
25 verify::verify_domains_match,
27 use lemmy_db_schema::{
33 private_message::PrivateMessage,
37 use lemmy_utils::LemmyError;
38 use lemmy_websocket::{
40 send_comment_ws_message_simple,
41 send_community_ws_message,
55 /// Parameter `reason` being set indicates that this is a removal by a mod. If its unset, this
56 /// action was done by a normal user.
57 #[tracing::instrument(skip_all)]
58 pub async fn send_apub_delete_in_community(
61 object: DeletableObjects,
62 reason: Option<String>,
64 context: &LemmyContext,
65 ) -> Result<(), LemmyError> {
66 let (id, activity) = if deleted {
67 let delete = Delete::new(&actor, object, public(), Some(&community), reason, context)?;
68 (delete.id.clone(), AnnouncableActivities::Delete(delete))
70 let undo = UndoDelete::new(&actor, object, public(), Some(&community), reason, context)?;
71 (undo.id.clone(), AnnouncableActivities::UndoDelete(undo))
73 send_activity_in_community(
76 &ApubPerson::from(actor),
84 #[tracing::instrument(skip_all)]
85 pub async fn send_apub_delete_private_message(
89 context: &LemmyContext,
90 ) -> Result<(), LemmyError> {
91 let recipient_id = pm.recipient_id;
92 let recipient: ApubPerson =
93 blocking(context.pool(), move |conn| Person::read(conn, recipient_id))
97 let deletable = DeletableObjects::PrivateMessage(Box::new(pm.into()));
98 let inbox = vec![recipient.shared_inbox_or_inbox_url()];
100 let delete = Delete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
101 let id = delete.id.clone();
102 send_lemmy_activity(context, &delete, &id, actor, inbox, true).await?;
104 let undo = UndoDelete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
105 let id = undo.id.clone();
106 send_lemmy_activity(context, &undo, &id, actor, inbox, true).await?;
111 pub enum DeletableObjects {
112 Community(Box<ApubCommunity>),
113 Comment(Box<ApubComment>),
115 PrivateMessage(Box<ApubPrivateMessage>),
118 impl DeletableObjects {
119 #[tracing::instrument(skip_all)]
120 pub(crate) async fn read_from_db(
122 context: &LemmyContext,
123 ) -> Result<DeletableObjects, LemmyError> {
124 if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
125 return Ok(DeletableObjects::Community(Box::new(c)));
127 if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
128 return Ok(DeletableObjects::Post(Box::new(p)));
130 if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
131 return Ok(DeletableObjects::Comment(Box::new(c)));
133 if let Some(p) = ApubPrivateMessage::read_from_apub_id(ap_id.clone(), context).await? {
134 return Ok(DeletableObjects::PrivateMessage(Box::new(p)));
136 Err(diesel::NotFound.into())
139 pub(crate) fn id(&self) -> Url {
141 DeletableObjects::Community(c) => c.actor_id(),
142 DeletableObjects::Comment(c) => c.ap_id.clone().into(),
143 DeletableObjects::Post(p) => p.ap_id.clone().into(),
144 DeletableObjects::PrivateMessage(p) => p.ap_id.clone().into(),
149 #[tracing::instrument(skip_all)]
150 pub(in crate::activities) async fn verify_delete_activity(
153 context: &LemmyContext,
154 request_counter: &mut i32,
155 ) -> Result<(), LemmyError> {
156 let object = DeletableObjects::read_from_db(activity.object.id(), context).await?;
158 DeletableObjects::Community(community) => {
159 verify_is_public(&activity.to, &[])?;
161 // can only do this check for local community, in remote case it would try to fetch the
162 // deleted community (which fails)
163 verify_person_in_community(&activity.actor, &community, context, request_counter).await?;
165 // community deletion is always a mod (or admin) action
168 activity.object.id(),
175 DeletableObjects::Post(p) => {
176 verify_is_public(&activity.to, &[])?;
177 verify_delete_post_or_comment(
179 &p.ap_id.clone().into(),
180 &activity.get_community(context, request_counter).await?,
187 DeletableObjects::Comment(c) => {
188 verify_is_public(&activity.to, &[])?;
189 verify_delete_post_or_comment(
191 &c.ap_id.clone().into(),
192 &activity.get_community(context, request_counter).await?,
199 DeletableObjects::PrivateMessage(_) => {
200 verify_person(&activity.actor, context, request_counter).await?;
201 verify_domains_match(activity.actor.inner(), activity.object.id())?;
207 #[tracing::instrument(skip_all)]
208 async fn verify_delete_post_or_comment(
209 actor: &ObjectId<ApubPerson>,
211 community: &ApubCommunity,
213 context: &LemmyContext,
214 request_counter: &mut i32,
215 ) -> Result<(), LemmyError> {
216 verify_person_in_community(actor, community, context, request_counter).await?;
218 verify_mod_action(actor, object_id, community, context, request_counter).await?;
220 // domain of post ap_id and post.creator ap_id are identical, so we just check the former
221 verify_domains_match(actor.inner(), object_id)?;
226 /// Write deletion or restoring of an object to the database, and send websocket message.
227 #[tracing::instrument(skip_all)]
228 async fn receive_delete_action(
230 actor: &ObjectId<ApubPerson>,
232 context: &LemmyContext,
233 request_counter: &mut i32,
234 ) -> Result<(), LemmyError> {
235 match DeletableObjects::read_from_db(object, context).await? {
236 DeletableObjects::Community(community) => {
238 let mod_: Person = actor
239 .dereference(context, context.client(), request_counter)
243 let object = DeletableObjects::Community(community.clone());
244 let c: Community = community.deref().deref().clone();
245 send_apub_delete_in_community(mod_, c, object, None, true, context).await?;
248 let community = blocking(context.pool(), move |conn| {
249 Community::update_deleted(conn, community.id, deleted)
252 send_community_ws_message(
254 UserOperationCrud::DeleteCommunity,
261 DeletableObjects::Post(post) => {
262 if deleted != post.deleted {
263 let deleted_post = blocking(context.pool(), move |conn| {
264 Post::update_deleted(conn, post.id, deleted)
267 send_post_ws_message(
269 UserOperationCrud::DeletePost,
277 DeletableObjects::Comment(comment) => {
278 if deleted != comment.deleted {
279 let deleted_comment = blocking(context.pool(), move |conn| {
280 Comment::update_deleted(conn, comment.id, deleted)
283 send_comment_ws_message_simple(
285 UserOperationCrud::DeleteComment,
291 DeletableObjects::PrivateMessage(pm) => {
292 let deleted_private_message = blocking(context.pool(), move |conn| {
293 PrivateMessage::update_deleted(conn, pm.id, deleted)
298 deleted_private_message.id,
299 UserOperationCrud::DeletePrivateMessage,