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