2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 private_message::{MarkPrivateMessageAsRead, PrivateMessageResponse},
6 utils::local_user_view_from_jwt,
7 websocket::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;
26 let local_user_view = local_user_view_from_jwt(&data.auth, context).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
49 &UserOperation::MarkPrivateMessageAsRead,
50 data.private_message_id,