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_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
15 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
16 use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud};
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for DeletePrivateMessage {
20 type Response = PrivateMessageResponse;
24 context: &Data<LemmyContext>,
25 websocket_id: Option<ConnectionId>,
26 ) -> Result<PrivateMessageResponse, LemmyError> {
27 let data: &DeletePrivateMessage = self;
28 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).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("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(|_| ApiError::err("couldnt_update_private_message"))?;
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 private_message_id = data.private_message_id;
63 let mut private_message_view = blocking(context.pool(), move |conn| {
64 PrivateMessageView::read(conn, private_message_id)
68 // Blank out deleted or removed info
70 private_message_view.private_message = private_message_view
72 .blank_out_deleted_or_removed_info();
75 let res = PrivateMessageResponse {
79 // Send notifications to the local recipient, if one exists
80 let recipient_id = orig_private_message.recipient_id;
81 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
82 LocalUserView::read_person(conn, recipient_id)
86 let local_recipient_id = local_recipient.local_user.id;
87 context.chat_server().do_send(SendUserRoomMessage {
88 op: UserOperationCrud::DeletePrivateMessage,
89 response: res.clone(),