1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 check_private_instance,
6 get_local_user_view_from_jwt_opt,
8 post::{GetPost, GetPostResponse},
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,
16 use lemmy_utils::{ConnectionId, LemmyError};
17 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetPost {
21 type Response = GetPostResponse;
23 #[tracing::instrument(skip(context, _websocket_id))]
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<GetPostResponse, LemmyError> {
29 let data: &GetPost = self;
31 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
34 check_private_instance(&local_user_view, context.pool()).await?;
36 let show_bot_accounts = local_user_view
38 .map(|t| t.local_user.show_bot_accounts);
39 let person_id = local_user_view.map(|u| u.person.id);
42 let mut post_view = blocking(context.pool(), move |conn| {
43 PostView::read(conn, id, person_id)
46 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?;
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?;
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)
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)
70 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
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();
80 .filter(|cv| cv.comment.deleted || cv.comment.removed)
82 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
84 if community_view.community.deleted || community_view.community.removed {
85 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
89 let moderators = blocking(context.pool(), move |conn| {
90 CommunityModeratorView::for_community(conn, community_id)
96 .send(GetPostUsersOnline { post_id: data.id })