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