]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/read.rs
Adding shortname fetching for users and communities. Fixes #1662 (#1663)
[lemmy.git] / crates / api_crud / src / post / read.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, mark_post_as_read, post::*};
4 use lemmy_apub::{build_actor_id_from_shortname, EndpointType};
5 use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType};
6 use lemmy_db_views::{
7   comment_view::CommentQueryBuilder,
8   post_view::{PostQueryBuilder, PostView},
9 };
10 use lemmy_db_views_actor::{
11   community_moderator_view::CommunityModeratorView,
12   community_view::CommunityView,
13 };
14 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
15 use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
16
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for GetPost {
19   type Response = GetPostResponse;
20
21   async fn perform(
22     &self,
23     context: &Data<LemmyContext>,
24     _websocket_id: Option<ConnectionId>,
25   ) -> Result<GetPostResponse, LemmyError> {
26     let data: &GetPost = self;
27     let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
28
29     let show_bot_accounts = local_user_view
30       .as_ref()
31       .map(|t| t.local_user.show_bot_accounts);
32     let person_id = local_user_view.map(|u| u.person.id);
33
34     let id = data.id;
35     let post_view = blocking(context.pool(), move |conn| {
36       PostView::read(conn, id, person_id)
37     })
38     .await?
39     .map_err(|_| ApiError::err("couldnt_find_post"))?;
40
41     // Mark the post as read
42     if let Some(person_id) = person_id {
43       mark_post_as_read(person_id, id, context.pool()).await?;
44     }
45
46     let id = data.id;
47     let comments = blocking(context.pool(), move |conn| {
48       CommentQueryBuilder::create(conn)
49         .my_person_id(person_id)
50         .show_bot_accounts(show_bot_accounts)
51         .post_id(id)
52         .limit(9999)
53         .list()
54     })
55     .await??;
56
57     let community_id = post_view.community.id;
58     let moderators = blocking(context.pool(), move |conn| {
59       CommunityModeratorView::for_community(conn, community_id)
60     })
61     .await??;
62
63     // Necessary for the sidebar
64     let community_view = blocking(context.pool(), move |conn| {
65       CommunityView::read(conn, community_id, person_id)
66     })
67     .await?
68     .map_err(|_| ApiError::err("couldnt_find_community"))?;
69
70     let online = context
71       .chat_server()
72       .send(GetPostUsersOnline { post_id: data.id })
73       .await
74       .unwrap_or(1);
75
76     // Return the jwt
77     Ok(GetPostResponse {
78       post_view,
79       community_view,
80       comments,
81       moderators,
82       online,
83     })
84   }
85 }
86
87 #[async_trait::async_trait(?Send)]
88 impl PerformCrud for GetPosts {
89   type Response = GetPostsResponse;
90
91   async fn perform(
92     &self,
93     context: &Data<LemmyContext>,
94     _websocket_id: Option<ConnectionId>,
95   ) -> Result<GetPostsResponse, LemmyError> {
96     let data: &GetPosts = self;
97     let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
98
99     let person_id = local_user_view.to_owned().map(|l| l.person.id);
100
101     let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
102     let show_bot_accounts = local_user_view
103       .as_ref()
104       .map(|t| t.local_user.show_bot_accounts);
105     let show_read_posts = local_user_view
106       .as_ref()
107       .map(|t| t.local_user.show_read_posts);
108
109     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
110     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
111
112     let page = data.page;
113     let limit = data.limit;
114     let community_id = data.community_id;
115     let community_actor_id = data
116       .community_name
117       .as_ref()
118       .map(|t| build_actor_id_from_shortname(EndpointType::Community, t).ok())
119       .unwrap_or(None);
120     let saved_only = data.saved_only;
121
122     let posts = blocking(context.pool(), move |conn| {
123       PostQueryBuilder::create(conn)
124         .listing_type(listing_type)
125         .sort(sort)
126         .show_nsfw(show_nsfw)
127         .show_bot_accounts(show_bot_accounts)
128         .show_read_posts(show_read_posts)
129         .community_id(community_id)
130         .community_actor_id(community_actor_id)
131         .saved_only(saved_only)
132         .my_person_id(person_id)
133         .page(page)
134         .limit(limit)
135         .list()
136     })
137     .await?
138     .map_err(|_| ApiError::err("couldnt_get_posts"))?;
139
140     Ok(GetPostsResponse { posts })
141   }
142 }