]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/notifications/mark_reply_read.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / api / src / local_user / notifications / mark_reply_read.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   person::{CommentReplyResponse, MarkCommentReplyAsRead},
5   utils::{blocking, get_local_user_view_from_jwt},
6 };
7 use lemmy_db_schema::{source::comment_reply::CommentReply, traits::Crud};
8 use lemmy_db_views_actor::structs::CommentReplyView;
9 use lemmy_utils::{error::LemmyError, ConnectionId};
10 use lemmy_websocket::LemmyContext;
11
12 #[async_trait::async_trait(?Send)]
13 impl Perform for MarkCommentReplyAsRead {
14   type Response = CommentReplyResponse;
15
16   #[tracing::instrument(skip(context, _websocket_id))]
17   async fn perform(
18     &self,
19     context: &Data<LemmyContext>,
20     _websocket_id: Option<ConnectionId>,
21   ) -> Result<CommentReplyResponse, LemmyError> {
22     let data = self;
23     let local_user_view =
24       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
25
26     let comment_reply_id = data.comment_reply_id;
27     let read_comment_reply = blocking(context.pool(), move |conn| {
28       CommentReply::read(conn, comment_reply_id)
29     })
30     .await??;
31
32     if local_user_view.person.id != read_comment_reply.recipient_id {
33       return Err(LemmyError::from_message("couldnt_update_comment"));
34     }
35
36     let comment_reply_id = read_comment_reply.id;
37     let read = data.read;
38     let update_reply = move |conn: &mut _| CommentReply::update_read(conn, comment_reply_id, read);
39     blocking(context.pool(), update_reply)
40       .await?
41       .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
42
43     let comment_reply_id = read_comment_reply.id;
44     let person_id = local_user_view.person.id;
45     let comment_reply_view = blocking(context.pool(), move |conn| {
46       CommentReplyView::read(conn, comment_reply_id, Some(person_id))
47     })
48     .await??;
49
50     Ok(CommentReplyResponse { comment_reply_view })
51   }
52 }