1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 private_message::{DeletePrivateMessage, PrivateMessageResponse},
5 utils::get_local_user_view_from_jwt,
6 websocket::{send::send_pm_ws_message, UserOperationCrud},
9 use lemmy_apub::activities::deletion::send_apub_delete_private_message;
10 use lemmy_db_schema::{
11 source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
14 use lemmy_utils::{error::LemmyError, ConnectionId};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for DeletePrivateMessage {
18 type Response = PrivateMessageResponse;
20 #[tracing::instrument(skip(self, context, websocket_id))]
23 context: &Data<LemmyContext>,
24 websocket_id: Option<ConnectionId>,
25 ) -> Result<PrivateMessageResponse, LemmyError> {
26 let data: &DeletePrivateMessage = self;
28 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
30 // Checking permissions
31 let private_message_id = data.private_message_id;
32 let orig_private_message = PrivateMessage::read(context.pool(), private_message_id).await?;
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 = PrivateMessage::update(
43 &PrivateMessageUpdateForm::builder()
44 .deleted(Some(deleted))
48 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
50 // Send the apub update
51 send_apub_delete_private_message(
52 &local_user_view.person.into(),
53 updated_private_message,
59 let op = UserOperationCrud::DeletePrivateMessage;
60 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await