]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/read.rs
Remove TypedBuilder from db_views and db_views_actor (#3637)
[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, LemmyErrorExt, LemmyErrorType};
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(&mut 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(&mut context.pool(), comment_id)
41         .await
42         .with_lemmy_type(LemmyErrorType::CouldntFindPost)?
43         .post_id
44     } else {
45       Err(LemmyErrorType::CouldntFindPost)?
46     };
47
48     // Check to see if the person is a mod or admin, to show deleted / removed
49     let community_id = Post::read(&mut context.pool(), post_id).await?.community_id;
50     let is_mod_or_admin = is_mod_or_admin_opt(
51       &mut context.pool(),
52       local_user_view.as_ref(),
53       Some(community_id),
54     )
55     .await
56     .is_ok();
57
58     let post_view = PostView::read(
59       &mut context.pool(),
60       post_id,
61       person_id,
62       Some(is_mod_or_admin),
63     )
64     .await
65     .with_lemmy_type(LemmyErrorType::CouldntFindPost)?;
66
67     // Mark the post as read
68     let post_id = post_view.post.id;
69     if let Some(person_id) = person_id {
70       mark_post_as_read(person_id, post_id, &mut context.pool()).await?;
71     }
72
73     // Necessary for the sidebar subscribed
74     let community_view = CommunityView::read(
75       &mut context.pool(),
76       community_id,
77       person_id,
78       Some(is_mod_or_admin),
79     )
80     .await
81     .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
82
83     // Insert into PersonPostAggregates
84     // to update the read_comments count
85     if let Some(person_id) = person_id {
86       let read_comments = post_view.counts.comments;
87       let person_post_agg_form = PersonPostAggregatesForm {
88         person_id,
89         post_id,
90         read_comments,
91         ..PersonPostAggregatesForm::default()
92       };
93       PersonPostAggregates::upsert(&mut context.pool(), &person_post_agg_form)
94         .await
95         .with_lemmy_type(LemmyErrorType::CouldntFindPost)?;
96     }
97
98     let moderators =
99       CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
100
101     // Fetch the cross_posts
102     let cross_posts = if let Some(url) = &post_view.post.url {
103       let mut x_posts = PostQuery {
104         url_search: Some(url.inner().as_str().into()),
105         ..Default::default()
106       }
107       .list(&mut context.pool())
108       .await?;
109
110       // Don't return this post as one of the cross_posts
111       x_posts.retain(|x| x.post.id != post_id);
112       x_posts
113     } else {
114       Vec::new()
115     };
116
117     // Return the jwt
118     Ok(GetPostResponse {
119       post_view,
120       community_view,
121       moderators,
122       cross_posts,
123     })
124   }
125 }