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;
27 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
29 // Checking permissions
30 let private_message_id = data.private_message_id;
31 let orig_private_message = blocking(context.pool(), move |conn| {
32 PrivateMessage::read(conn, private_message_id)
35 if local_user_view.person.id != orig_private_message.creator_id {
36 return Err(ApiError::err("no_private_message_edit_allowed").into());
40 let private_message_id = data.private_message_id;
41 let deleted = data.deleted;
42 let updated_private_message = blocking(context.pool(), move |conn| {
43 PrivateMessage::update_deleted(conn, private_message_id, deleted)
46 .map_err(|_| ApiError::err("couldnt_update_private_message"))?;
48 // Send the apub update
50 DeletePrivateMessageApub::send(
51 &local_user_view.person,
52 &updated_private_message.blank_out_deleted_or_removed_info(),
57 UndoDeletePrivateMessage::send(&local_user_view.person, &updated_private_message, context)
61 let op = UserOperationCrud::DeletePrivateMessage;
62 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await