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