]> Untitled Git - lemmy.git/blob - crates/api/src/post/mark_read.rs
8777ebced832a1f9b15be2046e03b1ce5907aa65
[lemmy.git] / crates / api / src / post / mark_read.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   post::{MarkPostAsRead, PostResponse},
5   utils::{blocking, get_local_user_view_from_jwt, mark_post_as_read, mark_post_as_unread},
6 };
7 use lemmy_db_views::structs::PostView;
8 use lemmy_utils::{ConnectionId, LemmyError};
9 use lemmy_websocket::LemmyContext;
10
11 #[async_trait::async_trait(?Send)]
12 impl Perform for MarkPostAsRead {
13   type Response = PostResponse;
14
15   #[tracing::instrument(skip(context, _websocket_id))]
16   async fn perform(
17     &self,
18     context: &Data<LemmyContext>,
19     _websocket_id: Option<ConnectionId>,
20   ) -> Result<Self::Response, LemmyError> {
21     let data = self;
22     let local_user_view =
23       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
24
25     let post_id = data.post_id;
26     let person_id = local_user_view.person.id;
27
28     // Mark the post as read / unread
29     if data.read {
30       mark_post_as_read(person_id, post_id, context.pool()).await?;
31     } else {
32       mark_post_as_unread(person_id, post_id, context.pool()).await?;
33     }
34
35     // Fetch it
36     let post_view = blocking(context.pool(), move |conn| {
37       PostView::read(conn, post_id, Some(person_id))
38     })
39     .await??;
40
41     let res = Self::Response { post_view };
42
43     Ok(res)
44   }
45 }