2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 private_message::{MarkPrivateMessageAsRead, PrivateMessageResponse},
6 utils::get_local_user_view_from_jwt,
7 websocket::{send::send_pm_ws_message, UserOperation},
10 source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
13 use lemmy_utils::{error::LemmyError, ConnectionId};
15 #[async_trait::async_trait(?Send)]
16 impl Perform for MarkPrivateMessageAsRead {
17 type Response = PrivateMessageResponse;
19 #[tracing::instrument(skip(context, websocket_id))]
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<PrivateMessageResponse, LemmyError> {
25 let data: &MarkPrivateMessageAsRead = 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 = PrivateMessage::read(context.pool(), private_message_id).await?;
32 if local_user_view.person.id != orig_private_message.recipient_id {
33 return Err(LemmyError::from_message("couldnt_update_private_message"));
37 let private_message_id = data.private_message_id;
39 PrivateMessage::update(
42 &PrivateMessageUpdateForm::builder().read(Some(read)).build(),
45 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
47 // No need to send an apub update
48 let op = UserOperation::MarkPrivateMessageAsRead;
49 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await