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::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::{ApiError, 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;
25 context: &Data<LemmyContext>,
26 websocket_id: Option<ConnectionId>,
27 ) -> Result<PrivateMessageResponse, LemmyError> {
28 let data: &DeletePrivateMessage = self;
30 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
32 // Checking permissions
33 let private_message_id = data.private_message_id;
34 let orig_private_message = blocking(context.pool(), move |conn| {
35 PrivateMessage::read(conn, private_message_id)
38 if local_user_view.person.id != orig_private_message.creator_id {
39 return Err(ApiError::err_plain("no_private_message_edit_allowed").into());
43 let private_message_id = data.private_message_id;
44 let deleted = data.deleted;
45 let updated_private_message = blocking(context.pool(), move |conn| {
46 PrivateMessage::update_deleted(conn, private_message_id, deleted)
49 .map_err(|e| ApiError::err("couldnt_update_private_message", e))?;
51 // Send the apub update
53 DeletePrivateMessageApub::send(
54 &local_user_view.person.into(),
55 &updated_private_message
56 .blank_out_deleted_or_removed_info()
62 UndoDeletePrivateMessage::send(
63 &local_user_view.person.into(),
64 &updated_private_message.into(),
70 let op = UserOperationCrud::DeletePrivateMessage;
71 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await