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},
7 use lemmy_db_schema::traits::DeleteableOrRemoveable;
8 use lemmy_db_views::{comment_view::CommentQueryBuilder, structs::PostView};
9 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
10 use lemmy_utils::{error::LemmyError, ConnectionId};
11 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
13 #[async_trait::async_trait(?Send)]
14 impl PerformCrud for GetPost {
15 type Response = GetPostResponse;
17 #[tracing::instrument(skip(context, _websocket_id))]
20 context: &Data<LemmyContext>,
21 _websocket_id: Option<ConnectionId>,
22 ) -> Result<GetPostResponse, LemmyError> {
23 let data: &GetPost = self;
25 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
28 check_private_instance(&local_user_view, context.pool()).await?;
30 let show_bot_accounts = local_user_view
32 .map(|t| t.local_user.show_bot_accounts);
33 let person_id = local_user_view.map(|u| u.person.id);
36 let mut post_view = blocking(context.pool(), move |conn| {
37 PostView::read(conn, id, person_id)
40 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?;
42 // Mark the post as read
43 if let Some(person_id) = person_id {
44 mark_post_as_read(person_id, id, context.pool()).await?;
48 let mut comments = blocking(context.pool(), move |conn| {
49 CommentQueryBuilder::create(conn)
50 .my_person_id(person_id)
51 .show_bot_accounts(show_bot_accounts)
58 // Necessary for the sidebar
59 let community_id = post_view.community.id;
60 let mut community_view = blocking(context.pool(), move |conn| {
61 CommunityView::read(conn, community_id, person_id)
64 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
66 // Blank out deleted or removed info for non-logged in users
67 if person_id.is_none() {
68 if post_view.post.deleted || post_view.post.removed {
69 post_view.post = post_view.post.blank_out_deleted_or_removed_info();
74 .filter(|cv| cv.comment.deleted || cv.comment.removed)
76 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
78 if community_view.community.deleted || community_view.community.removed {
79 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
83 let moderators = blocking(context.pool(), move |conn| {
84 CommunityModeratorView::for_community(conn, community_id)
90 .send(GetPostUsersOnline { post_id: data.id })