]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/read.rs
Add default post listing type (fixes #2195) (#2209)
[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   blocking,
5   check_private_instance,
6   get_local_user_view_from_jwt_opt,
7   mark_post_as_read,
8   post::{GetPost, GetPostResponse},
9 };
10 use lemmy_db_schema::traits::DeleteableOrRemoveable;
11 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostView};
12 use lemmy_db_views_actor::{
13   community_moderator_view::CommunityModeratorView,
14   community_view::CommunityView,
15 };
16 use lemmy_utils::{ConnectionId, LemmyError};
17 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
18
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetPost {
21   type Response = GetPostResponse;
22
23   #[tracing::instrument(skip(context, _websocket_id))]
24   async fn perform(
25     &self,
26     context: &Data<LemmyContext>,
27     _websocket_id: Option<ConnectionId>,
28   ) -> Result<GetPostResponse, LemmyError> {
29     let data: &GetPost = self;
30     let local_user_view =
31       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
32         .await?;
33
34     check_private_instance(&local_user_view, context.pool()).await?;
35
36     let show_bot_accounts = local_user_view
37       .as_ref()
38       .map(|t| t.local_user.show_bot_accounts);
39     let person_id = local_user_view.map(|u| u.person.id);
40
41     let id = data.id;
42     let mut post_view = blocking(context.pool(), move |conn| {
43       PostView::read(conn, id, person_id)
44     })
45     .await?
46     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?;
47
48     // Mark the post as read
49     if let Some(person_id) = person_id {
50       mark_post_as_read(person_id, id, context.pool()).await?;
51     }
52
53     let id = data.id;
54     let mut comments = blocking(context.pool(), move |conn| {
55       CommentQueryBuilder::create(conn)
56         .my_person_id(person_id)
57         .show_bot_accounts(show_bot_accounts)
58         .post_id(id)
59         .limit(9999)
60         .list()
61     })
62     .await??;
63
64     // Necessary for the sidebar
65     let community_id = post_view.community.id;
66     let mut community_view = blocking(context.pool(), move |conn| {
67       CommunityView::read(conn, community_id, person_id)
68     })
69     .await?
70     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
71
72     // Blank out deleted or removed info for non-logged in users
73     if person_id.is_none() {
74       if post_view.post.deleted || post_view.post.removed {
75         post_view.post = post_view.post.blank_out_deleted_or_removed_info();
76       }
77
78       for cv in comments
79         .iter_mut()
80         .filter(|cv| cv.comment.deleted || cv.comment.removed)
81       {
82         cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
83       }
84       if community_view.community.deleted || community_view.community.removed {
85         community_view.community = community_view.community.blank_out_deleted_or_removed_info();
86       }
87     }
88
89     let moderators = blocking(context.pool(), move |conn| {
90       CommunityModeratorView::for_community(conn, community_id)
91     })
92     .await??;
93
94     let online = context
95       .chat_server()
96       .send(GetPostUsersOnline { post_id: data.id })
97       .await
98       .unwrap_or(1);
99
100     // Return the jwt
101     Ok(GetPostResponse {
102       post_view,
103       community_view,
104       comments,
105       moderators,
106       online,
107     })
108   }
109 }