1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 check_private_instance,
7 get_local_user_view_from_jwt_opt,
10 fetcher::webfinger::webfinger_resolve,
11 objects::community::ApubCommunity,
14 use lemmy_apub_lib::object_id::ObjectId;
15 use lemmy_db_schema::{
16 from_opt_str_to_opt_enum,
17 traits::DeleteableOrRemoveable,
21 use lemmy_db_views_actor::{
22 community_moderator_view::CommunityModeratorView,
23 community_view::{CommunityQueryBuilder, CommunityView},
25 use lemmy_utils::{ConnectionId, LemmyError};
26 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for GetCommunity {
30 type Response = GetCommunityResponse;
32 #[tracing::instrument(skip(context, _websocket_id))]
35 context: &Data<LemmyContext>,
36 _websocket_id: Option<ConnectionId>,
37 ) -> Result<GetCommunityResponse, LemmyError> {
38 let data: &GetCommunity = self;
40 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
43 check_private_instance(&local_user_view, context.pool()).await?;
45 let person_id = local_user_view.map(|u| u.person.id);
47 let community_id = match data.id {
50 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
51 let community_actor_id =
52 webfinger_resolve::<ApubCommunity>(&name, EndpointType::Community, context, &mut 0)
55 ObjectId::<ApubCommunity>::new(community_actor_id)
56 .dereference(context, context.client(), &mut 0)
58 .map_err(LemmyError::from)
59 .map_err(|e| e.with_message("couldnt_find_community"))?
64 let mut community_view = blocking(context.pool(), move |conn| {
65 CommunityView::read(conn, community_id, person_id)
68 .map_err(LemmyError::from)
69 .map_err(|e| e.with_message("couldnt_find_community"))?;
71 // Blank out deleted or removed info for non-logged in users
72 if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
74 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
77 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
78 CommunityModeratorView::for_community(conn, community_id)
81 .map_err(LemmyError::from)
82 .map_err(|e| e.with_message("couldnt_find_community"))?;
86 .send(GetCommunityUsersOnline { community_id })
90 let res = GetCommunityResponse {
101 #[async_trait::async_trait(?Send)]
102 impl PerformCrud for ListCommunities {
103 type Response = ListCommunitiesResponse;
105 #[tracing::instrument(skip(context, _websocket_id))]
108 context: &Data<LemmyContext>,
109 _websocket_id: Option<ConnectionId>,
110 ) -> Result<ListCommunitiesResponse, LemmyError> {
111 let data: &ListCommunities = self;
112 let local_user_view =
113 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
116 check_private_instance(&local_user_view, context.pool()).await?;
118 let person_id = local_user_view.to_owned().map(|l| l.person.id);
120 // Don't show NSFW by default
121 let show_nsfw = match &local_user_view {
122 Some(uv) => uv.local_user.show_nsfw,
126 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
127 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
129 let page = data.page;
130 let limit = data.limit;
131 let mut communities = blocking(context.pool(), move |conn| {
132 CommunityQueryBuilder::create(conn)
133 .listing_type(listing_type)
135 .show_nsfw(show_nsfw)
136 .my_person_id(person_id)
143 // Blank out deleted or removed info for non-logged in users
144 if person_id.is_none() {
145 for cv in communities
147 .filter(|cv| cv.community.deleted || cv.community.removed)
149 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
154 Ok(ListCommunitiesResponse { communities })