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, fetcher::object_id::ObjectId, EndpointType};
5 use lemmy_db_queries::{from_opt_str_to_opt_enum, DeleteableOrRemoveable, ListingType, SortType};
6 use lemmy_db_schema::source::community::*;
7 use lemmy_db_views_actor::{
8 community_moderator_view::CommunityModeratorView,
9 community_view::{CommunityQueryBuilder, CommunityView},
11 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
12 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
14 #[async_trait::async_trait(?Send)]
15 impl PerformCrud for GetCommunity {
16 type Response = GetCommunityResponse;
20 context: &Data<LemmyContext>,
21 _websocket_id: Option<ConnectionId>,
22 ) -> Result<GetCommunityResponse, LemmyError> {
23 let data: &GetCommunity = self;
25 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
26 let person_id = local_user_view.map(|u| u.person.id);
28 let community_id = match data.id {
31 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
32 let community_actor_id =
33 build_actor_id_from_shortname(EndpointType::Community, &name, &context.settings())?;
35 ObjectId::<Community>::new(community_actor_id)
36 .dereference(context, &mut 0)
38 .map_err(|e| ApiError::err("couldnt_find_community", e))?
43 let mut community_view = blocking(context.pool(), move |conn| {
44 CommunityView::read(conn, community_id, person_id)
47 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
49 // Blank out deleted or removed info
50 if community_view.community.deleted || community_view.community.removed {
51 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
54 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
55 CommunityModeratorView::for_community(conn, community_id)
58 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
62 .send(GetCommunityUsersOnline { community_id })
66 let res = GetCommunityResponse {
77 #[async_trait::async_trait(?Send)]
78 impl PerformCrud for ListCommunities {
79 type Response = ListCommunitiesResponse;
83 context: &Data<LemmyContext>,
84 _websocket_id: Option<ConnectionId>,
85 ) -> Result<ListCommunitiesResponse, LemmyError> {
86 let data: &ListCommunities = self;
88 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
90 let person_id = local_user_view.to_owned().map(|l| l.person.id);
92 // Don't show NSFW by default
93 let show_nsfw = match &local_user_view {
94 Some(uv) => uv.local_user.show_nsfw,
98 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
99 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
101 let page = data.page;
102 let limit = data.limit;
103 let mut communities = blocking(context.pool(), move |conn| {
104 CommunityQueryBuilder::create(conn)
105 .listing_type(listing_type)
107 .show_nsfw(show_nsfw)
108 .my_person_id(person_id)
115 // Blank out deleted or removed info
116 for cv in communities
118 .filter(|cv| cv.community.deleted || cv.community.removed)
120 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
124 Ok(ListCommunitiesResponse { communities })