1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 person::{EditPrivateMessage, PrivateMessageResponse},
5 utils::{blocking, get_local_user_view_from_jwt},
7 use lemmy_apub::protocol::activities::{
8 create_or_update::private_message::CreateOrUpdatePrivateMessage,
11 use lemmy_db_schema::{source::private_message::PrivateMessage, traits::Crud};
12 use lemmy_utils::{utils::remove_slurs, ConnectionId, LemmyError};
13 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperationCrud};
15 #[async_trait::async_trait(?Send)]
16 impl PerformCrud for EditPrivateMessage {
17 type Response = PrivateMessageResponse;
19 #[tracing::instrument(skip(self, context, websocket_id))]
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<PrivateMessageResponse, LemmyError> {
25 let data: &EditPrivateMessage = self;
27 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
29 // Checking permissions
30 let private_message_id = data.private_message_id;
31 let orig_private_message = blocking(context.pool(), move |conn| {
32 PrivateMessage::read(conn, private_message_id)
35 if local_user_view.person.id != orig_private_message.creator_id {
36 return Err(LemmyError::from_message("no_private_message_edit_allowed"));
40 let content_slurs_removed = remove_slurs(&data.content, &context.settings().slur_regex());
41 let private_message_id = data.private_message_id;
42 let updated_private_message = blocking(context.pool(), move |conn| {
43 PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed)
46 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
48 // Send the apub update
49 CreateOrUpdatePrivateMessage::send(
50 updated_private_message.into(),
51 &local_user_view.person.into(),
52 CreateOrUpdateType::Update,
57 let op = UserOperationCrud::EditPrivateMessage;
58 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await