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