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