1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 get_local_user_view_from_jwt,
6 person::{EditPrivateMessage, 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::{utils::remove_slurs, ApiError, ConnectionId, LemmyError};
13 use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud};
15 #[async_trait::async_trait(?Send)]
16 impl PerformCrud for EditPrivateMessage {
17 type Response = PrivateMessageResponse;
21 context: &Data<LemmyContext>,
22 websocket_id: Option<ConnectionId>,
23 ) -> Result<PrivateMessageResponse, LemmyError> {
24 let data: &EditPrivateMessage = 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 content_slurs_removed = remove_slurs(&data.content);
39 let private_message_id = data.private_message_id;
40 let updated_private_message = blocking(context.pool(), move |conn| {
41 PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed)
44 .map_err(|_| ApiError::err("couldnt_update_private_message"))?;
46 // Send the apub update
47 updated_private_message
48 .send_update(&local_user_view.person, context)
51 let private_message_id = data.private_message_id;
52 let mut private_message_view = blocking(context.pool(), move |conn| {
53 PrivateMessageView::read(conn, private_message_id)
57 // Blank out deleted or removed info
58 if private_message_view.private_message.deleted {
59 private_message_view.private_message = private_message_view
61 .blank_out_deleted_or_removed_info();
64 let res = PrivateMessageResponse {
68 // Send notifications to the local recipient, if one exists
69 let recipient_id = orig_private_message.recipient_id;
70 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
71 LocalUserView::read_person(conn, recipient_id)
75 let local_recipient_id = local_recipient.local_user.id;
76 context.chat_server().do_send(SendUserRoomMessage {
77 op: UserOperationCrud::EditPrivateMessage,
78 response: res.clone(),