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, DeleteableOrRemoveable};
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 .blank_out_deleted_or_removed_info()
50 .send_delete(&local_user_view.person, context)
53 updated_private_message
54 .send_undo_delete(&local_user_view.person, context)
58 let private_message_id = data.private_message_id;
59 let mut private_message_view = blocking(context.pool(), move |conn| {
60 PrivateMessageView::read(conn, private_message_id)
64 // Blank out deleted or removed info
66 private_message_view.private_message = private_message_view
68 .blank_out_deleted_or_removed_info();
71 let res = PrivateMessageResponse {
75 // Send notifications to the local recipient, if one exists
76 let recipient_id = orig_private_message.recipient_id;
77 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
78 LocalUserView::read_person(conn, recipient_id)
82 let local_recipient_id = local_recipient.local_user.id;
83 context.chat_server().do_send(SendUserRoomMessage {
84 op: UserOperationCrud::DeletePrivateMessage,
85 response: res.clone(),