2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 get_local_user_view_from_jwt,
6 person::{MarkPrivateMessageAsRead, PrivateMessageResponse},
8 use lemmy_db_queries::{source::private_message::PrivateMessage_, Crud};
9 use lemmy_db_schema::source::private_message::PrivateMessage;
10 use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
11 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
12 use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation};
14 #[async_trait::async_trait(?Send)]
15 impl Perform for MarkPrivateMessageAsRead {
16 type Response = PrivateMessageResponse;
20 context: &Data<LemmyContext>,
21 websocket_id: Option<ConnectionId>,
22 ) -> Result<PrivateMessageResponse, LemmyError> {
23 let data: &MarkPrivateMessageAsRead = &self;
24 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
26 // Checking permissions
27 let private_message_id = data.private_message_id;
28 let orig_private_message = blocking(context.pool(), move |conn| {
29 PrivateMessage::read(conn, private_message_id)
32 if local_user_view.person.id != orig_private_message.recipient_id {
33 return Err(ApiError::err("couldnt_update_private_message").into());
37 let private_message_id = data.private_message_id;
39 match blocking(context.pool(), move |conn| {
40 PrivateMessage::update_read(conn, private_message_id, read)
44 Ok(private_message) => private_message,
45 Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
48 // No need to send an apub update
49 let private_message_id = data.private_message_id;
50 let private_message_view = blocking(context.pool(), move |conn| {
51 PrivateMessageView::read(conn, private_message_id)
55 let res = PrivateMessageResponse {
59 // Send notifications to the local recipient, if one exists
60 let recipient_id = orig_private_message.recipient_id;
61 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
62 LocalUserView::read_person(conn, recipient_id)
66 let local_recipient_id = local_recipient.local_user.id;
67 context.chat_server().do_send(SendUserRoomMessage {
68 op: UserOperation::MarkPrivateMessageAsRead,
69 response: res.clone(),