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,
8 DeleteableOrRemoveable,
12 use lemmy_db_schema::source::community::*;
13 use lemmy_db_views_actor::{
14 community_moderator_view::CommunityModeratorView,
15 community_view::{CommunityQueryBuilder, CommunityView},
17 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
18 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for GetCommunity {
22 type Response = GetCommunityResponse;
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<GetCommunityResponse, LemmyError> {
29 let data: &GetCommunity = self;
31 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
32 let person_id = local_user_view.map(|u| u.person.id);
34 let community_id = match data.id {
37 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
38 let community_actor_id =
39 build_actor_id_from_shortname(EndpointType::Community, &name, &context.settings())?;
41 blocking(context.pool(), move |conn| {
42 Community::read_from_apub_id(conn, &community_actor_id)
45 .map_err(|_| ApiError::err("couldnt_find_community"))?
50 let mut community_view = blocking(context.pool(), move |conn| {
51 CommunityView::read(conn, community_id, person_id)
54 .map_err(|_| ApiError::err("couldnt_find_community"))?;
56 // Blank out deleted or removed info
57 if community_view.community.deleted || community_view.community.removed {
58 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
61 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
62 CommunityModeratorView::for_community(conn, community_id)
65 .map_err(|_| ApiError::err("couldnt_find_community"))?;
69 .send(GetCommunityUsersOnline { community_id })
73 let res = GetCommunityResponse {
84 #[async_trait::async_trait(?Send)]
85 impl PerformCrud for ListCommunities {
86 type Response = ListCommunitiesResponse;
90 context: &Data<LemmyContext>,
91 _websocket_id: Option<ConnectionId>,
92 ) -> Result<ListCommunitiesResponse, LemmyError> {
93 let data: &ListCommunities = self;
95 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
97 let person_id = local_user_view.to_owned().map(|l| l.person.id);
99 // Don't show NSFW by default
100 let show_nsfw = match &local_user_view {
101 Some(uv) => uv.local_user.show_nsfw,
105 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
106 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
108 let page = data.page;
109 let limit = data.limit;
110 let mut communities = blocking(context.pool(), move |conn| {
111 CommunityQueryBuilder::create(conn)
112 .listing_type(listing_type)
114 .show_nsfw(show_nsfw)
115 .my_person_id(person_id)
122 // Blank out deleted or removed info
123 for cv in communities
125 .filter(|cv| cv.community.deleted || cv.community.removed)
127 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
131 Ok(ListCommunitiesResponse { communities })