1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt_opt};
5 fetcher::webfinger::webfinger_resolve,
6 objects::community::ApubCommunity,
9 use lemmy_apub_lib::object_id::ObjectId;
10 use lemmy_db_schema::{
11 from_opt_str_to_opt_enum,
12 traits::DeleteableOrRemoveable,
16 use lemmy_db_views_actor::{
17 community_moderator_view::CommunityModeratorView,
18 community_view::{CommunityQueryBuilder, CommunityView},
20 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
21 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
23 #[async_trait::async_trait(?Send)]
24 impl PerformCrud for GetCommunity {
25 type Response = GetCommunityResponse;
29 context: &Data<LemmyContext>,
30 _websocket_id: Option<ConnectionId>,
31 ) -> Result<GetCommunityResponse, LemmyError> {
32 let data: &GetCommunity = self;
34 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
35 let person_id = local_user_view.map(|u| u.person.id);
37 let community_id = match data.id {
40 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
41 let community_actor_id =
42 webfinger_resolve::<ApubCommunity>(&name, EndpointType::Community, context, &mut 0)
45 ObjectId::<ApubCommunity>::new(community_actor_id)
46 .dereference(context, &mut 0)
48 .map_err(|e| ApiError::err("couldnt_find_community", e))?
53 let mut community_view = blocking(context.pool(), move |conn| {
54 CommunityView::read(conn, community_id, person_id)
57 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
59 // Blank out deleted or removed info for non-logged in users
60 if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
62 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
65 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
66 CommunityModeratorView::for_community(conn, community_id)
69 .map_err(|e| ApiError::err("couldnt_find_community", e))?;
73 .send(GetCommunityUsersOnline { community_id })
77 let res = GetCommunityResponse {
88 #[async_trait::async_trait(?Send)]
89 impl PerformCrud for ListCommunities {
90 type Response = ListCommunitiesResponse;
94 context: &Data<LemmyContext>,
95 _websocket_id: Option<ConnectionId>,
96 ) -> Result<ListCommunitiesResponse, LemmyError> {
97 let data: &ListCommunities = self;
99 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
101 let person_id = local_user_view.to_owned().map(|l| l.person.id);
103 // Don't show NSFW by default
104 let show_nsfw = match &local_user_view {
105 Some(uv) => uv.local_user.show_nsfw,
109 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
110 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
112 let page = data.page;
113 let limit = data.limit;
114 let mut communities = blocking(context.pool(), move |conn| {
115 CommunityQueryBuilder::create(conn)
116 .listing_type(listing_type)
118 .show_nsfw(show_nsfw)
119 .my_person_id(person_id)
126 // Blank out deleted or removed info for non-logged in users
127 if person_id.is_none() {
128 for cv in communities
130 .filter(|cv| cv.community.deleted || cv.community.removed)
132 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
137 Ok(ListCommunitiesResponse { communities })