2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 private_message::{MarkPrivateMessageAsRead, PrivateMessageResponse},
5 utils::get_local_user_view_from_jwt,
8 source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
11 use lemmy_utils::{error::LemmyError, ConnectionId};
12 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperation};
14 #[async_trait::async_trait(?Send)]
15 impl Perform for MarkPrivateMessageAsRead {
16 type Response = PrivateMessageResponse;
18 #[tracing::instrument(skip(context, websocket_id))]
21 context: &Data<LemmyContext>,
22 websocket_id: Option<ConnectionId>,
23 ) -> Result<PrivateMessageResponse, LemmyError> {
24 let data: &MarkPrivateMessageAsRead = self;
26 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
28 // Checking permissions
29 let private_message_id = data.private_message_id;
30 let orig_private_message = PrivateMessage::read(context.pool(), private_message_id).await?;
31 if local_user_view.person.id != orig_private_message.recipient_id {
32 return Err(LemmyError::from_message("couldnt_update_private_message"));
36 let private_message_id = data.private_message_id;
38 PrivateMessage::update(
41 &PrivateMessageUpdateForm::builder().read(Some(read)).build(),
44 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
46 // No need to send an apub update
47 let op = UserOperation::MarkPrivateMessageAsRead;
48 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await