2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 get_local_user_view_from_jwt,
6 person::{GetRepliesResponse, MarkAllAsRead},
8 use lemmy_db_schema::source::{
10 person_mention::PersonMention,
11 private_message::PrivateMessage,
13 use lemmy_db_views::comment_view::CommentQueryBuilder;
14 use lemmy_utils::{ConnectionId, LemmyError};
15 use lemmy_websocket::LemmyContext;
17 #[async_trait::async_trait(?Send)]
18 impl Perform for MarkAllAsRead {
19 type Response = GetRepliesResponse;
21 #[tracing::instrument(skip(context, _websocket_id))]
24 context: &Data<LemmyContext>,
25 _websocket_id: Option<ConnectionId>,
26 ) -> Result<GetRepliesResponse, LemmyError> {
27 let data: &MarkAllAsRead = self;
29 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
31 let person_id = local_user_view.person.id;
32 let replies = blocking(context.pool(), move |conn| {
33 CommentQueryBuilder::create(conn)
34 .my_person_id(person_id)
35 .recipient_id(person_id)
43 // TODO: this should probably be a bulk operation
44 // Not easy to do as a bulk operation,
45 // because recipient_id isn't in the comment table
46 for comment_view in &replies {
47 let reply_id = comment_view.comment.id;
48 let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
49 blocking(context.pool(), mark_as_read)
51 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
54 // Mark all user mentions as read
55 let update_person_mentions =
56 move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
57 blocking(context.pool(), update_person_mentions)
59 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
61 // Mark all private_messages as read
62 let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
63 blocking(context.pool(), update_pm)
65 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
67 Ok(GetRepliesResponse { replies: vec![] })