]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/read.rs
Dont swallow API errors (fixes #1834) (#1837)
[lemmy.git] / crates / api_crud / src / community / read.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt_opt};
4 use lemmy_apub::{build_actor_id_from_shortname, fetcher::object_id::ObjectId, EndpointType};
5 use lemmy_db_queries::{from_opt_str_to_opt_enum, DeleteableOrRemoveable, ListingType, SortType};
6 use lemmy_db_schema::source::community::*;
7 use lemmy_db_views_actor::{
8   community_moderator_view::CommunityModeratorView,
9   community_view::{CommunityQueryBuilder, CommunityView},
10 };
11 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
12 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
13
14 #[async_trait::async_trait(?Send)]
15 impl PerformCrud for GetCommunity {
16   type Response = GetCommunityResponse;
17
18   async fn perform(
19     &self,
20     context: &Data<LemmyContext>,
21     _websocket_id: Option<ConnectionId>,
22   ) -> Result<GetCommunityResponse, LemmyError> {
23     let data: &GetCommunity = self;
24     let local_user_view =
25       get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
26     let person_id = local_user_view.map(|u| u.person.id);
27
28     let community_id = match data.id {
29       Some(id) => id,
30       None => {
31         let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
32         let community_actor_id =
33           build_actor_id_from_shortname(EndpointType::Community, &name, &context.settings())?;
34
35         ObjectId::<Community>::new(community_actor_id)
36           .dereference(context, &mut 0)
37           .await
38           .map_err(|e| ApiError::err("couldnt_find_community", e))?
39           .id
40       }
41     };
42
43     let mut community_view = blocking(context.pool(), move |conn| {
44       CommunityView::read(conn, community_id, person_id)
45     })
46     .await?
47     .map_err(|e| ApiError::err("couldnt_find_community", e))?;
48
49     // Blank out deleted or removed info
50     if community_view.community.deleted || community_view.community.removed {
51       community_view.community = community_view.community.blank_out_deleted_or_removed_info();
52     }
53
54     let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
55       CommunityModeratorView::for_community(conn, community_id)
56     })
57     .await?
58     .map_err(|e| ApiError::err("couldnt_find_community", e))?;
59
60     let online = context
61       .chat_server()
62       .send(GetCommunityUsersOnline { community_id })
63       .await
64       .unwrap_or(1);
65
66     let res = GetCommunityResponse {
67       community_view,
68       moderators,
69       online,
70     };
71
72     // Return the jwt
73     Ok(res)
74   }
75 }
76
77 #[async_trait::async_trait(?Send)]
78 impl PerformCrud for ListCommunities {
79   type Response = ListCommunitiesResponse;
80
81   async fn perform(
82     &self,
83     context: &Data<LemmyContext>,
84     _websocket_id: Option<ConnectionId>,
85   ) -> Result<ListCommunitiesResponse, LemmyError> {
86     let data: &ListCommunities = self;
87     let local_user_view =
88       get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
89
90     let person_id = local_user_view.to_owned().map(|l| l.person.id);
91
92     // Don't show NSFW by default
93     let show_nsfw = match &local_user_view {
94       Some(uv) => uv.local_user.show_nsfw,
95       None => false,
96     };
97
98     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
99     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
100
101     let page = data.page;
102     let limit = data.limit;
103     let mut communities = blocking(context.pool(), move |conn| {
104       CommunityQueryBuilder::create(conn)
105         .listing_type(listing_type)
106         .sort(sort)
107         .show_nsfw(show_nsfw)
108         .my_person_id(person_id)
109         .page(page)
110         .limit(limit)
111         .list()
112     })
113     .await??;
114
115     // Blank out deleted or removed info
116     for cv in communities
117       .iter_mut()
118       .filter(|cv| cv.community.deleted || cv.community.removed)
119     {
120       cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
121     }
122
123     // Return the jwt
124     Ok(ListCommunitiesResponse { communities })
125   }
126 }