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, is_admin},
8 use lemmy_db_schema::source::local_site::LocalSite;
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?;
27 let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok());
29 check_private_instance(&local_user_view, &local_site)?;
32 let listing_type = data.type_;
34 let limit = data.limit;
35 let local_user = local_user_view.map(|l| l.local_user);
36 let communities = CommunityQuery::builder()
38 .listing_type(listing_type)
40 .local_user(local_user.as_ref())
43 .is_mod_or_admin(is_admin)
49 Ok(ListCommunitiesResponse { communities })