1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 post::{GetPost, GetPostResponse},
5 utils::{check_private_instance, get_local_user_view_from_jwt_opt, mark_post_as_read},
8 aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
9 source::{comment::Comment, local_site::LocalSite},
10 traits::{Crud, DeleteableOrRemoveable},
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};
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for GetPost {
19 type Response = GetPostResponse;
21 #[tracing::instrument(skip(context, _websocket_id))]
24 context: &Data<LemmyContext>,
25 _websocket_id: Option<ConnectionId>,
26 ) -> Result<GetPostResponse, LemmyError> {
27 let data: &GetPost = self;
29 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
31 let local_site = LocalSite::read(context.pool()).await?;
33 check_private_instance(&local_user_view, &local_site)?;
35 let person_id = local_user_view.map(|u| u.person.id);
37 // I'd prefer fetching the post_view by a comment join, but it adds a lot of boilerplate
38 let post_id = if let Some(id) = data.id {
40 } else if let Some(comment_id) = data.comment_id {
41 Comment::read(context.pool(), comment_id)
43 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?
46 Err(LemmyError::from_message("couldnt_find_post"))?
49 let mut post_view = PostView::read(context.pool(), post_id, person_id)
51 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?;
53 // Mark the post as read
54 let post_id = post_view.post.id;
55 if let Some(person_id) = person_id {
56 mark_post_as_read(person_id, post_id, context.pool()).await?;
59 // Necessary for the sidebar subscribed
60 let community_id = post_view.community.id;
61 let mut community_view = CommunityView::read(context.pool(), community_id, person_id)
63 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
65 // Insert into PersonPostAggregates
66 // to update the read_comments count
67 if let Some(person_id) = person_id {
68 let read_comments = post_view.counts.comments;
69 let person_post_agg_form = PersonPostAggregatesForm {
73 ..PersonPostAggregatesForm::default()
75 PersonPostAggregates::upsert(context.pool(), &person_post_agg_form)
77 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?;
80 // Blank out deleted or removed info for non-logged in users
81 if person_id.is_none() {
82 if post_view.post.deleted || post_view.post.removed {
83 post_view.post = post_view.post.blank_out_deleted_or_removed_info();
86 if community_view.community.deleted || community_view.community.removed {
87 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
91 let moderators = CommunityModeratorView::for_community(context.pool(), community_id).await?;
95 .send(GetPostUsersOnline { post_id })