1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{ListCommunities, ListCommunitiesResponse},
6 utils::{check_private_instance, get_local_user_view_from_jwt_opt},
8 use lemmy_db_schema::{source::local_site::LocalSite, traits::DeleteableOrRemoveable};
9 use lemmy_db_views_actor::community_view::CommunityQuery;
10 use lemmy_utils::{error::LemmyError, ConnectionId};
12 #[async_trait::async_trait(?Send)]
13 impl PerformCrud for ListCommunities {
14 type Response = ListCommunitiesResponse;
16 #[tracing::instrument(skip(context, _websocket_id))]
19 context: &Data<LemmyContext>,
20 _websocket_id: Option<ConnectionId>,
21 ) -> Result<ListCommunitiesResponse, LemmyError> {
22 let data: &ListCommunities = self;
24 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
26 let local_site = LocalSite::read(context.pool()).await?;
28 check_private_instance(&local_user_view, &local_site)?;
30 let person_id = local_user_view.clone().map(|l| l.person.id);
33 let listing_type = data.type_;
35 let limit = data.limit;
36 let local_user = local_user_view.map(|l| l.local_user);
37 let mut communities = CommunityQuery::builder()
39 .listing_type(listing_type)
41 .local_user(local_user.as_ref())
48 // Blank out deleted or removed info for non-logged in users
49 if person_id.is_none() {
52 .filter(|cv| cv.community.deleted || cv.community.removed)
54 cv.community = cv.clone().community.blank_out_deleted_or_removed_info();
59 Ok(ListCommunitiesResponse { communities })