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