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::protocol::activities::private_message::{
9 delete::DeletePrivateMessage as DeletePrivateMessageApub,
10 undo_delete::UndoDeletePrivateMessage,
12 use lemmy_db_schema::{
13 source::private_message::PrivateMessage,
14 traits::{Crud, DeleteableOrRemoveable},
16 use lemmy_utils::{ConnectionId, LemmyError};
17 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for DeletePrivateMessage {
21 type Response = PrivateMessageResponse;
23 #[tracing::instrument(skip(self, context, websocket_id))]
26 context: &Data<LemmyContext>,
27 websocket_id: Option<ConnectionId>,
28 ) -> Result<PrivateMessageResponse, LemmyError> {
29 let data: &DeletePrivateMessage = self;
31 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33 // Checking permissions
34 let private_message_id = data.private_message_id;
35 let orig_private_message = blocking(context.pool(), move |conn| {
36 PrivateMessage::read(conn, private_message_id)
39 if local_user_view.person.id != orig_private_message.creator_id {
40 return Err(LemmyError::from_message("no_private_message_edit_allowed"));
44 let private_message_id = data.private_message_id;
45 let deleted = data.deleted;
46 let updated_private_message = blocking(context.pool(), move |conn| {
47 PrivateMessage::update_deleted(conn, private_message_id, deleted)
50 .map_err(LemmyError::from)
51 .map_err(|e| e.with_message("couldnt_update_private_message"))?;
53 // Send the apub update
55 DeletePrivateMessageApub::send(
56 &local_user_view.person.into(),
57 &updated_private_message
58 .blank_out_deleted_or_removed_info()
64 UndoDeletePrivateMessage::send(
65 &local_user_view.person.into(),
66 &updated_private_message.into(),
72 let op = UserOperationCrud::DeletePrivateMessage;
73 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await