2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 private_message::{MarkPrivateMessageAsRead, PrivateMessageResponse},
6 utils::local_user_view_from_jwt,
9 source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
12 use lemmy_db_views::structs::PrivateMessageView;
13 use lemmy_utils::error::LemmyError;
15 #[async_trait::async_trait(?Send)]
16 impl Perform for MarkPrivateMessageAsRead {
17 type Response = PrivateMessageResponse;
19 #[tracing::instrument(skip(context))]
22 context: &Data<LemmyContext>,
23 ) -> Result<PrivateMessageResponse, LemmyError> {
24 let data: &MarkPrivateMessageAsRead = self;
25 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
27 // Checking permissions
28 let private_message_id = data.private_message_id;
29 let orig_private_message = PrivateMessage::read(context.pool(), private_message_id).await?;
30 if local_user_view.person.id != orig_private_message.recipient_id {
31 return Err(LemmyError::from_message("couldnt_update_private_message"));
35 let private_message_id = data.private_message_id;
37 PrivateMessage::update(
40 &PrivateMessageUpdateForm::builder().read(Some(read)).build(),
43 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
45 let view = PrivateMessageView::read(context.pool(), private_message_id).await?;
46 Ok(PrivateMessageResponse {
47 private_message_view: view,