]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/read.rs
Replace Option<bool> with bool for PostQuery and CommentQuery (#3819) (#3857)
[lemmy.git] / crates / api_crud / src / post / read.rs
1 use actix_web::web::{Data, Json, Query};
2 use lemmy_api_common::{
3   context::LemmyContext,
4   post::{GetPost, GetPostResponse},
5   utils::{
6     check_private_instance,
7     is_mod_or_admin_opt,
8     local_user_view_from_jwt_opt,
9     mark_post_as_read,
10   },
11 };
12 use lemmy_db_schema::{
13   aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
14   source::{comment::Comment, local_site::LocalSite, post::Post},
15   traits::Crud,
16 };
17 use lemmy_db_views::{post_view::PostQuery, structs::PostView};
18 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
19 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
20
21 #[tracing::instrument(skip(context))]
22 pub async fn get_post(
23   data: Query<GetPost>,
24   context: Data<LemmyContext>,
25 ) -> Result<Json<GetPostResponse>, LemmyError> {
26   let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
27   let local_site = LocalSite::read(&mut context.pool()).await?;
28
29   check_private_instance(&local_user_view, &local_site)?;
30
31   let person_id = local_user_view.as_ref().map(|u| u.person.id);
32
33   // I'd prefer fetching the post_view by a comment join, but it adds a lot of boilerplate
34   let post_id = if let Some(id) = data.id {
35     id
36   } else if let Some(comment_id) = data.comment_id {
37     Comment::read(&mut context.pool(), comment_id)
38       .await
39       .with_lemmy_type(LemmyErrorType::CouldntFindPost)?
40       .post_id
41   } else {
42     Err(LemmyErrorType::CouldntFindPost)?
43   };
44
45   // Check to see if the person is a mod or admin, to show deleted / removed
46   let community_id = Post::read(&mut context.pool(), post_id).await?.community_id;
47   let is_mod_or_admin = is_mod_or_admin_opt(
48     &mut context.pool(),
49     local_user_view.as_ref(),
50     Some(community_id),
51   )
52   .await
53   .is_ok();
54
55   let post_view = PostView::read(&mut context.pool(), post_id, person_id, is_mod_or_admin)
56     .await
57     .with_lemmy_type(LemmyErrorType::CouldntFindPost)?;
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, &mut context.pool()).await?;
63   }
64
65   // Necessary for the sidebar subscribed
66   let community_view = CommunityView::read(
67     &mut context.pool(),
68     community_id,
69     person_id,
70     is_mod_or_admin,
71   )
72   .await
73   .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
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(&mut context.pool(), &person_post_agg_form)
86       .await
87       .with_lemmy_type(LemmyErrorType::CouldntFindPost)?;
88   }
89
90   let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
91
92   // Fetch the cross_posts
93   let cross_posts = if let Some(url) = &post_view.post.url {
94     let mut x_posts = PostQuery {
95       url_search: Some(url.inner().as_str().into()),
96       ..Default::default()
97     }
98     .list(&mut context.pool())
99     .await?;
100
101     // Don't return this post as one of the cross_posts
102     x_posts.retain(|x| x.post.id != post_id);
103     x_posts
104   } else {
105     Vec::new()
106   };
107
108   // Return the jwt
109   Ok(Json(GetPostResponse {
110     post_view,
111     community_view,
112     moderators,
113     cross_posts,
114   }))
115 }