]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/notifications/mark_reply_read.rs
2e338367ebb343b91810383e59bd0193e7ebc805
[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   context::LemmyContext,
5   person::{CommentReplyResponse, MarkCommentReplyAsRead},
6   utils::local_user_view_from_jwt,
7 };
8 use lemmy_db_schema::{
9   source::comment_reply::{CommentReply, CommentReplyUpdateForm},
10   traits::Crud,
11 };
12 use lemmy_db_views_actor::structs::CommentReplyView;
13 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
14
15 #[async_trait::async_trait(?Send)]
16 impl Perform for MarkCommentReplyAsRead {
17   type Response = CommentReplyResponse;
18
19   #[tracing::instrument(skip(context))]
20   async fn perform(
21     &self,
22     context: &Data<LemmyContext>,
23   ) -> Result<CommentReplyResponse, LemmyError> {
24     let data = self;
25     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
26
27     let comment_reply_id = data.comment_reply_id;
28     let read_comment_reply = CommentReply::read(context.pool(), comment_reply_id).await?;
29
30     if local_user_view.person.id != read_comment_reply.recipient_id {
31       return Err(LemmyErrorType::CouldntUpdateComment)?;
32     }
33
34     let comment_reply_id = read_comment_reply.id;
35     let read = Some(data.read);
36
37     CommentReply::update(
38       context.pool(),
39       comment_reply_id,
40       &CommentReplyUpdateForm { read },
41     )
42     .await
43     .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
44
45     let comment_reply_id = read_comment_reply.id;
46     let person_id = local_user_view.person.id;
47     let comment_reply_view =
48       CommentReplyView::read(context.pool(), comment_reply_id, Some(person_id)).await?;
49
50     Ok(CommentReplyResponse { comment_reply_view })
51   }
52 }