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