]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/read.rs
Showing # of unread comments for posts. Fixes #2134 (#2393)
[lemmy.git] / crates / api_crud / src / post / read.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   post::{GetPost, GetPostResponse},
5   utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt, mark_post_as_read},
6 };
7 use lemmy_db_schema::{
8   aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
9   source::comment::Comment,
10   traits::{Crud, DeleteableOrRemoveable},
11 };
12 use lemmy_db_views::structs::PostView;
13 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
14 use lemmy_utils::{error::LemmyError, ConnectionId};
15 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
16
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for GetPost {
19   type Response = GetPostResponse;
20
21   #[tracing::instrument(skip(context, _websocket_id))]
22   async fn perform(
23     &self,
24     context: &Data<LemmyContext>,
25     _websocket_id: Option<ConnectionId>,
26   ) -> Result<GetPostResponse, LemmyError> {
27     let data: &GetPost = self;
28     let local_user_view =
29       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
30         .await?;
31
32     check_private_instance(&local_user_view, context.pool()).await?;
33
34     let person_id = local_user_view.map(|u| u.person.id);
35
36     // I'd prefer fetching the post_view by a comment join, but it adds a lot of boilerplate
37     let post_id = if let Some(id) = data.id {
38       id
39     } else if let Some(comment_id) = data.comment_id {
40       blocking(context.pool(), move |conn| Comment::read(conn, comment_id))
41         .await?
42         .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?
43         .post_id
44     } else {
45       Err(LemmyError::from_message("couldnt_find_post"))?
46     };
47
48     let mut post_view = blocking(context.pool(), move |conn| {
49       PostView::read(conn, post_id, person_id)
50     })
51     .await?
52     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?;
53
54     // Mark the post as read
55     let post_id = post_view.post.id;
56     if let Some(person_id) = person_id {
57       mark_post_as_read(person_id, post_id, context.pool()).await?;
58     }
59
60     // Necessary for the sidebar subscribed
61     let community_id = post_view.community.id;
62     let mut community_view = blocking(context.pool(), move |conn| {
63       CommunityView::read(conn, community_id, person_id)
64     })
65     .await?
66     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
67
68     // Insert into PersonPostAggregates
69     // to update the read_comments count
70     if let Some(person_id) = person_id {
71       let read_comments = post_view.counts.comments;
72       let person_post_agg_form = PersonPostAggregatesForm {
73         person_id,
74         post_id,
75         read_comments,
76         ..PersonPostAggregatesForm::default()
77       };
78       blocking(context.pool(), move |conn| {
79         PersonPostAggregates::upsert(conn, &person_post_agg_form)
80       })
81       .await?
82       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?;
83     }
84
85     // Blank out deleted or removed info for non-logged in users
86     if person_id.is_none() {
87       if post_view.post.deleted || post_view.post.removed {
88         post_view.post = post_view.post.blank_out_deleted_or_removed_info();
89       }
90
91       if community_view.community.deleted || community_view.community.removed {
92         community_view.community = community_view.community.blank_out_deleted_or_removed_info();
93       }
94     }
95
96     let moderators = blocking(context.pool(), move |conn| {
97       CommunityModeratorView::for_community(conn, community_id)
98     })
99     .await??;
100
101     let online = context
102       .chat_server()
103       .send(GetPostUsersOnline { post_id })
104       .await
105       .unwrap_or(1);
106
107     // Return the jwt
108     Ok(GetPostResponse {
109       post_view,
110       community_view,
111       moderators,
112       online,
113     })
114   }
115 }