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_queries::{source::private_message::PrivateMessage_, Crud, DeleteableOrRemoveable};
13 use lemmy_db_schema::source::private_message::PrivateMessage;
14 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
15 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for DeletePrivateMessage {
19 type Response = PrivateMessageResponse;
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 = blocking(context.pool(), move |conn| {
33 PrivateMessage::read(conn, private_message_id)
36 if local_user_view.person.id != orig_private_message.creator_id {
37 return Err(ApiError::err_plain("no_private_message_edit_allowed").into());
41 let private_message_id = data.private_message_id;
42 let deleted = data.deleted;
43 let updated_private_message = blocking(context.pool(), move |conn| {
44 PrivateMessage::update_deleted(conn, private_message_id, deleted)
47 .map_err(|e| ApiError::err("couldnt_update_private_message", e))?;
49 // Send the apub update
51 DeletePrivateMessageApub::send(
52 &local_user_view.person,
53 &updated_private_message.blank_out_deleted_or_removed_info(),
58 UndoDeletePrivateMessage::send(&local_user_view.person, &updated_private_message, context)
62 let op = UserOperationCrud::DeletePrivateMessage;
63 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await