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::ApubObjectType;
9 use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud};
10 use lemmy_db_schema::source::private_message::PrivateMessage;
11 use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
12 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
13 use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation};
15 #[async_trait::async_trait(?Send)]
16 impl PerformCrud for DeletePrivateMessage {
17 type Response = PrivateMessageResponse;
21 context: &Data<LemmyContext>,
22 websocket_id: Option<ConnectionId>,
23 ) -> Result<PrivateMessageResponse, LemmyError> {
24 let data: &DeletePrivateMessage = &self;
25 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
27 // Checking permissions
28 let private_message_id = data.private_message_id;
29 let orig_private_message = blocking(context.pool(), move |conn| {
30 PrivateMessage::read(conn, private_message_id)
33 if local_user_view.person.id != orig_private_message.creator_id {
34 return Err(ApiError::err("no_private_message_edit_allowed").into());
38 let private_message_id = data.private_message_id;
39 let deleted = data.deleted;
40 let updated_private_message = match blocking(context.pool(), move |conn| {
41 PrivateMessage::update_deleted(conn, private_message_id, deleted)
45 Ok(private_message) => private_message,
46 Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
49 // Send the apub update
51 updated_private_message
52 .send_delete(&local_user_view.person, context)
55 updated_private_message
56 .send_undo_delete(&local_user_view.person, context)
60 let private_message_id = data.private_message_id;
61 let private_message_view = blocking(context.pool(), move |conn| {
62 PrivateMessageView::read(conn, private_message_id)
66 let res = PrivateMessageResponse {
70 // Send notifications to the local recipient, if one exists
71 let recipient_id = orig_private_message.recipient_id;
72 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
73 LocalUserView::read_person(conn, recipient_id)
77 let local_recipient_id = local_recipient.local_user.id;
78 context.chat_server().do_send(SendUserRoomMessage {
79 op: UserOperation::DeletePrivateMessage,
80 response: res.clone(),