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,
54 /// Parameter `reason` being set indicates that this is a removal by a mod. If its unset, this
55 /// action was done by a normal user.
56 #[tracing::instrument(skip_all)]
57 pub async fn send_apub_delete_in_community(
60 object: DeletableObjects,
61 reason: Option<String>,
63 context: &LemmyContext,
64 ) -> Result<(), LemmyError> {
65 let (id, activity) = if deleted {
66 let delete = Delete::new(&actor, object, public(), Some(&community), reason, context)?;
67 (delete.id.clone(), AnnouncableActivities::Delete(delete))
69 let undo = UndoDelete::new(&actor, object, public(), Some(&community), reason, context)?;
70 (undo.id.clone(), AnnouncableActivities::UndoDelete(undo))
72 send_activity_in_community(
75 &ApubPerson::from(actor),
83 #[tracing::instrument(skip_all)]
84 pub async fn send_apub_delete_private_message(
88 context: &LemmyContext,
89 ) -> Result<(), LemmyError> {
90 let recipient_id = pm.recipient_id;
91 let recipient: ApubPerson =
92 blocking(context.pool(), move |conn| Person::read(conn, recipient_id))
96 let deletable = DeletableObjects::PrivateMessage(Box::new(pm.into()));
97 let inbox = vec![recipient.shared_inbox_or_inbox_url()];
99 let delete = Delete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
100 let id = delete.id.clone();
101 send_lemmy_activity(context, &delete, &id, actor, inbox, true).await?;
103 let undo = UndoDelete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
104 let id = undo.id.clone();
105 send_lemmy_activity(context, &undo, &id, actor, inbox, true).await?;
110 pub enum DeletableObjects {
111 Community(Box<ApubCommunity>),
112 Comment(Box<ApubComment>),
114 PrivateMessage(Box<ApubPrivateMessage>),
117 impl DeletableObjects {
118 #[tracing::instrument(skip_all)]
119 pub(crate) async fn read_from_db(
121 context: &LemmyContext,
122 ) -> Result<DeletableObjects, LemmyError> {
123 if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
124 return Ok(DeletableObjects::Community(Box::new(c)));
126 if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
127 return Ok(DeletableObjects::Post(Box::new(p)));
129 if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
130 return Ok(DeletableObjects::Comment(Box::new(c)));
132 if let Some(p) = ApubPrivateMessage::read_from_apub_id(ap_id.clone(), context).await? {
133 return Ok(DeletableObjects::PrivateMessage(Box::new(p)));
135 Err(diesel::NotFound.into())
138 pub(crate) fn id(&self) -> Url {
140 DeletableObjects::Community(c) => c.actor_id(),
141 DeletableObjects::Comment(c) => c.ap_id.clone().into(),
142 DeletableObjects::Post(p) => p.ap_id.clone().into(),
143 DeletableObjects::PrivateMessage(p) => p.ap_id.clone().into(),
148 #[tracing::instrument(skip_all)]
149 pub(in crate::activities) async fn verify_delete_activity(
152 context: &LemmyContext,
153 request_counter: &mut i32,
154 ) -> Result<(), LemmyError> {
155 let object = DeletableObjects::read_from_db(activity.object.id(), context).await?;
157 DeletableObjects::Community(community) => {
158 verify_is_public(&activity.to, &[])?;
160 // can only do this check for local community, in remote case it would try to fetch the
161 // deleted community (which fails)
162 verify_person_in_community(&activity.actor, &community, context, request_counter).await?;
164 // community deletion is always a mod (or admin) action
165 verify_mod_action(&activity.actor, &community, context, request_counter).await?;
167 DeletableObjects::Post(p) => {
168 verify_is_public(&activity.to, &[])?;
169 verify_delete_post_or_comment(
171 &p.ap_id.clone().into(),
172 &activity.get_community(context, request_counter).await?,
179 DeletableObjects::Comment(c) => {
180 verify_is_public(&activity.to, &[])?;
181 verify_delete_post_or_comment(
183 &c.ap_id.clone().into(),
184 &activity.get_community(context, request_counter).await?,
191 DeletableObjects::PrivateMessage(_) => {
192 verify_person(&activity.actor, context, request_counter).await?;
193 verify_domains_match(activity.actor.inner(), activity.object.id())?;
199 #[tracing::instrument(skip_all)]
200 async fn verify_delete_post_or_comment(
201 actor: &ObjectId<ApubPerson>,
203 community: &ApubCommunity,
205 context: &LemmyContext,
206 request_counter: &mut i32,
207 ) -> Result<(), LemmyError> {
208 verify_person_in_community(actor, community, context, request_counter).await?;
210 verify_mod_action(actor, community, context, request_counter).await?;
212 // domain of post ap_id and post.creator ap_id are identical, so we just check the former
213 verify_domains_match(actor.inner(), object_id)?;
218 /// Write deletion or restoring of an object to the database, and send websocket message.
219 #[tracing::instrument(skip_all)]
220 async fn receive_delete_action(
222 actor: &ObjectId<ApubPerson>,
224 context: &LemmyContext,
225 request_counter: &mut i32,
226 ) -> Result<(), LemmyError> {
227 match DeletableObjects::read_from_db(object, context).await? {
228 DeletableObjects::Community(community) => {
230 let mod_: Person = actor
231 .dereference(context, context.client(), request_counter)
235 let object = DeletableObjects::Community(community.clone());
236 let c: Community = community.deref().deref().clone();
237 send_apub_delete_in_community(mod_, c, object, None, true, context).await?;
240 let community = blocking(context.pool(), move |conn| {
241 Community::update_deleted(conn, community.id, deleted)
244 send_community_ws_message(
246 UserOperationCrud::DeleteCommunity,
253 DeletableObjects::Post(post) => {
254 if deleted != post.deleted {
255 let deleted_post = blocking(context.pool(), move |conn| {
256 Post::update_deleted(conn, post.id, deleted)
259 send_post_ws_message(
261 UserOperationCrud::DeletePost,
269 DeletableObjects::Comment(comment) => {
270 if deleted != comment.deleted {
271 let deleted_comment = blocking(context.pool(), move |conn| {
272 Comment::update_deleted(conn, comment.id, deleted)
275 send_comment_ws_message_simple(
277 UserOperationCrud::DeleteComment,
283 DeletableObjects::PrivateMessage(pm) => {
284 let deleted_private_message = blocking(context.pool(), move |conn| {
285 PrivateMessage::update_deleted(conn, pm.id, deleted)
290 deleted_private_message.id,
291 UserOperationCrud::DeletePrivateMessage,