1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 private_message::{EditPrivateMessage, PrivateMessageResponse},
5 utils::{blocking, get_local_user_view_from_jwt, local_site_to_slur_regex},
7 use lemmy_apub::protocol::activities::{
8 create_or_update::private_message::CreateOrUpdatePrivateMessage,
11 use lemmy_db_schema::{
13 local_site::LocalSite,
14 private_message::{PrivateMessage, PrivateMessageUpdateForm},
19 use lemmy_utils::{error::LemmyError, utils::remove_slurs, ConnectionId};
20 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
22 #[async_trait::async_trait(?Send)]
23 impl PerformCrud for EditPrivateMessage {
24 type Response = PrivateMessageResponse;
26 #[tracing::instrument(skip(self, context, websocket_id))]
29 context: &Data<LemmyContext>,
30 websocket_id: Option<ConnectionId>,
31 ) -> Result<PrivateMessageResponse, LemmyError> {
32 let data: &EditPrivateMessage = self;
34 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
35 let local_site = blocking(context.pool(), LocalSite::read).await??;
37 // Checking permissions
38 let private_message_id = data.private_message_id;
39 let orig_private_message = blocking(context.pool(), move |conn| {
40 PrivateMessage::read(conn, private_message_id)
43 if local_user_view.person.id != orig_private_message.creator_id {
44 return Err(LemmyError::from_message("no_private_message_edit_allowed"));
48 let content_slurs_removed = remove_slurs(&data.content, &local_site_to_slur_regex(&local_site));
49 let private_message_id = data.private_message_id;
50 let updated_private_message = blocking(context.pool(), move |conn| {
51 PrivateMessage::update(
54 &PrivateMessageUpdateForm::builder()
55 .content(Some(content_slurs_removed))
56 .updated(Some(Some(naive_now())))
61 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
63 // Send the apub update
64 CreateOrUpdatePrivateMessage::send(
65 updated_private_message.into(),
66 &local_user_view.person.into(),
67 CreateOrUpdateType::Update,
72 let op = UserOperationCrud::EditPrivateMessage;
73 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await