1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 person::{DeletePrivateMessage, PrivateMessageResponse},
5 utils::{blocking, get_local_user_view_from_jwt},
7 use lemmy_apub::activities::deletion::send_apub_delete_private_message;
8 use lemmy_db_schema::{source::private_message::PrivateMessage, traits::Crud};
9 use lemmy_utils::{ConnectionId, LemmyError};
10 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
12 #[async_trait::async_trait(?Send)]
13 impl PerformCrud for DeletePrivateMessage {
14 type Response = PrivateMessageResponse;
16 #[tracing::instrument(skip(self, context, websocket_id))]
19 context: &Data<LemmyContext>,
20 websocket_id: Option<ConnectionId>,
21 ) -> Result<PrivateMessageResponse, LemmyError> {
22 let data: &DeletePrivateMessage = self;
24 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
26 // Checking permissions
27 let private_message_id = data.private_message_id;
28 let orig_private_message = blocking(context.pool(), move |conn| {
29 PrivateMessage::read(conn, private_message_id)
32 if local_user_view.person.id != orig_private_message.creator_id {
33 return Err(LemmyError::from_message("no_private_message_edit_allowed"));
37 let private_message_id = data.private_message_id;
38 let deleted = data.deleted;
39 let updated_private_message = blocking(context.pool(), move |conn| {
40 PrivateMessage::update_deleted(conn, private_message_id, deleted)
43 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
45 // Send the apub update
46 send_apub_delete_private_message(
47 &local_user_view.person.into(),
48 updated_private_message,
54 let op = UserOperationCrud::DeletePrivateMessage;
55 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await