]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/notifications/mark_all_read.rs
First pass at adding comment trees. (#2362)
[lemmy.git] / crates / api / src / local_user / notifications / mark_all_read.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   person::{GetRepliesResponse, MarkAllAsRead},
5   utils::{blocking, get_local_user_view_from_jwt},
6 };
7 use lemmy_db_schema::source::{
8   comment_reply::CommentReply,
9   person_mention::PersonMention,
10   private_message::PrivateMessage,
11 };
12 use lemmy_utils::{error::LemmyError, ConnectionId};
13 use lemmy_websocket::LemmyContext;
14
15 #[async_trait::async_trait(?Send)]
16 impl Perform for MarkAllAsRead {
17   type Response = GetRepliesResponse;
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<GetRepliesResponse, LemmyError> {
25     let data: &MarkAllAsRead = self;
26     let local_user_view =
27       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
28     let person_id = local_user_view.person.id;
29
30     // Mark all comment_replies as read
31     blocking(context.pool(), move |conn| {
32       CommentReply::mark_all_as_read(conn, person_id)
33     })
34     .await?
35     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
36
37     // Mark all user mentions as read
38     blocking(context.pool(), move |conn| {
39       PersonMention::mark_all_as_read(conn, person_id)
40     })
41     .await?
42     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
43
44     // Mark all private_messages as read
45     blocking(context.pool(), move |conn| {
46       PrivateMessage::mark_all_as_read(conn, person_id)
47     })
48     .await?
49     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
50
51     Ok(GetRepliesResponse { replies: vec![] })
52   }
53 }