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, UserOperationCrud};
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 = blocking(context.pool(), move |conn| {
41 PrivateMessage::update_deleted(conn, private_message_id, deleted)
44 .map_err(|_| ApiError::err("couldnt_update_private_message"))?;
46 // Send the apub update
48 updated_private_message
49 .send_delete(&local_user_view.person, context)
52 updated_private_message
53 .send_undo_delete(&local_user_view.person, context)
57 let private_message_id = data.private_message_id;
58 let private_message_view = blocking(context.pool(), move |conn| {
59 PrivateMessageView::read(conn, private_message_id)
63 let res = PrivateMessageResponse {
67 // Send notifications to the local recipient, if one exists
68 let recipient_id = orig_private_message.recipient_id;
69 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
70 LocalUserView::read_person(conn, recipient_id)
74 let local_recipient_id = local_recipient.local_user.id;
75 context.chat_server().do_send(SendUserRoomMessage {
76 op: UserOperationCrud::DeletePrivateMessage,
77 response: res.clone(),