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