2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 person::{MarkPrivateMessageAsRead, PrivateMessageResponse},
5 utils::{blocking, get_local_user_view_from_jwt},
7 use lemmy_db_schema::{source::private_message::PrivateMessage, traits::Crud};
8 use lemmy_utils::{ConnectionId, LemmyError};
9 use lemmy_websocket::{send::send_pm_ws_message, LemmyContext, UserOperation};
11 #[async_trait::async_trait(?Send)]
12 impl Perform for MarkPrivateMessageAsRead {
13 type Response = PrivateMessageResponse;
15 #[tracing::instrument(skip(context, websocket_id))]
18 context: &Data<LemmyContext>,
19 websocket_id: Option<ConnectionId>,
20 ) -> Result<PrivateMessageResponse, LemmyError> {
21 let data: &MarkPrivateMessageAsRead = self;
23 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
25 // Checking permissions
26 let private_message_id = data.private_message_id;
27 let orig_private_message = blocking(context.pool(), move |conn| {
28 PrivateMessage::read(conn, private_message_id)
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 blocking(context.pool(), move |conn| {
39 PrivateMessage::update_read(conn, private_message_id, read)
42 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
44 // No need to send an apub update
45 let op = UserOperation::MarkPrivateMessageAsRead;
46 send_pm_ws_message(data.private_message_id, op, websocket_id, context).await