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