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