]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/list.rs
Show deleted and removed posts for profile views. Fixes #2624 (#2729)
[lemmy.git] / crates / api_crud / src / community / list.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   community::{ListCommunities, ListCommunitiesResponse},
5   context::LemmyContext,
6   utils::{check_private_instance, get_local_user_view_from_jwt_opt, is_admin},
7 };
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};
11
12 #[async_trait::async_trait(?Send)]
13 impl PerformCrud for ListCommunities {
14   type Response = ListCommunitiesResponse;
15
16   #[tracing::instrument(skip(context, _websocket_id))]
17   async fn perform(
18     &self,
19     context: &Data<LemmyContext>,
20     _websocket_id: Option<ConnectionId>,
21   ) -> Result<ListCommunitiesResponse, LemmyError> {
22     let data: &ListCommunities = self;
23     let local_user_view =
24       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
25         .await?;
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());
28
29     check_private_instance(&local_user_view, &local_site)?;
30
31     let sort = data.sort;
32     let listing_type = data.type_;
33     let page = data.page;
34     let limit = data.limit;
35     let local_user = local_user_view.map(|l| l.local_user);
36     let communities = CommunityQuery::builder()
37       .pool(context.pool())
38       .listing_type(listing_type)
39       .sort(sort)
40       .local_user(local_user.as_ref())
41       .page(page)
42       .limit(limit)
43       .is_mod_or_admin(is_admin)
44       .build()
45       .list()
46       .await?;
47
48     // Return the jwt
49     Ok(ListCommunitiesResponse { communities })
50   }
51 }