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::{ConnectionId, LemmyError};
21 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
23 #[async_trait::async_trait(?Send)]
24 impl PerformCrud for GetCommunity {
25 type Response = GetCommunityResponse;
27 #[tracing::instrument(skip(context, _websocket_id))]
30 context: &Data<LemmyContext>,
31 _websocket_id: Option<ConnectionId>,
32 ) -> Result<GetCommunityResponse, LemmyError> {
33 let data: &GetCommunity = self;
35 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
37 let person_id = local_user_view.map(|u| u.person.id);
39 let community_id = match data.id {
42 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
43 let community_actor_id =
44 webfinger_resolve::<ApubCommunity>(&name, EndpointType::Community, context, &mut 0)
47 ObjectId::<ApubCommunity>::new(community_actor_id)
48 .dereference(context, context.client(), &mut 0)
50 .map_err(LemmyError::from)
51 .map_err(|e| e.with_message("couldnt_find_community"))?
56 let mut community_view = blocking(context.pool(), move |conn| {
57 CommunityView::read(conn, community_id, person_id)
60 .map_err(LemmyError::from)
61 .map_err(|e| e.with_message("couldnt_find_community"))?;
63 // Blank out deleted or removed info for non-logged in users
64 if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
66 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
69 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
70 CommunityModeratorView::for_community(conn, community_id)
73 .map_err(LemmyError::from)
74 .map_err(|e| e.with_message("couldnt_find_community"))?;
78 .send(GetCommunityUsersOnline { community_id })
82 let res = GetCommunityResponse {
93 #[async_trait::async_trait(?Send)]
94 impl PerformCrud for ListCommunities {
95 type Response = ListCommunitiesResponse;
97 #[tracing::instrument(skip(context, _websocket_id))]
100 context: &Data<LemmyContext>,
101 _websocket_id: Option<ConnectionId>,
102 ) -> Result<ListCommunitiesResponse, LemmyError> {
103 let data: &ListCommunities = self;
104 let local_user_view =
105 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
108 let person_id = local_user_view.to_owned().map(|l| l.person.id);
110 // Don't show NSFW by default
111 let show_nsfw = match &local_user_view {
112 Some(uv) => uv.local_user.show_nsfw,
116 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
117 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
119 let page = data.page;
120 let limit = data.limit;
121 let mut communities = blocking(context.pool(), move |conn| {
122 CommunityQueryBuilder::create(conn)
123 .listing_type(listing_type)
125 .show_nsfw(show_nsfw)
126 .my_person_id(person_id)
133 // Blank out deleted or removed info for non-logged in users
134 if person_id.is_none() {
135 for cv in communities
137 .filter(|cv| cv.community.deleted || cv.community.removed)
139 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
144 Ok(ListCommunitiesResponse { communities })