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,
8 resolve_actor_identifier,
10 use lemmy_db_schema::{
11 from_opt_str_to_opt_enum,
12 source::community::Community,
13 traits::DeleteableOrRemoveable,
17 use lemmy_db_views_actor::{
18 community_moderator_view::CommunityModeratorView,
19 community_view::{CommunityQueryBuilder, CommunityView},
21 use lemmy_utils::{ConnectionId, LemmyError};
22 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
24 #[async_trait::async_trait(?Send)]
25 impl PerformCrud for GetCommunity {
26 type Response = GetCommunityResponse;
28 #[tracing::instrument(skip(context, _websocket_id))]
31 context: &Data<LemmyContext>,
32 _websocket_id: Option<ConnectionId>,
33 ) -> Result<GetCommunityResponse, LemmyError> {
34 let data: &GetCommunity = self;
36 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
39 check_private_instance(&local_user_view, context.pool()).await?;
41 let person_id = local_user_view.map(|u| u.person.id);
43 let community_id = match data.id {
46 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
47 resolve_actor_identifier::<Community>(&name, context.pool())
49 .map_err(LemmyError::from)
50 .map_err(|e| e.with_message("couldnt_find_community"))?
55 let mut community_view = blocking(context.pool(), move |conn| {
56 CommunityView::read(conn, community_id, person_id)
59 .map_err(LemmyError::from)
60 .map_err(|e| e.with_message("couldnt_find_community"))?;
62 // Blank out deleted or removed info for non-logged in users
63 if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
65 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
68 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
69 CommunityModeratorView::for_community(conn, community_id)
72 .map_err(LemmyError::from)
73 .map_err(|e| e.with_message("couldnt_find_community"))?;
77 .send(GetCommunityUsersOnline { community_id })
81 let res = GetCommunityResponse {
92 #[async_trait::async_trait(?Send)]
93 impl PerformCrud for ListCommunities {
94 type Response = ListCommunitiesResponse;
96 #[tracing::instrument(skip(context, _websocket_id))]
99 context: &Data<LemmyContext>,
100 _websocket_id: Option<ConnectionId>,
101 ) -> Result<ListCommunitiesResponse, LemmyError> {
102 let data: &ListCommunities = self;
103 let local_user_view =
104 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
107 check_private_instance(&local_user_view, context.pool()).await?;
109 let person_id = local_user_view.to_owned().map(|l| l.person.id);
111 // Don't show NSFW by default
112 let show_nsfw = match &local_user_view {
113 Some(uv) => uv.local_user.show_nsfw,
117 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
118 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
120 let page = data.page;
121 let limit = data.limit;
122 let mut communities = blocking(context.pool(), move |conn| {
123 CommunityQueryBuilder::create(conn)
124 .listing_type(listing_type)
126 .show_nsfw(show_nsfw)
127 .my_person_id(person_id)
134 // Blank out deleted or removed info for non-logged in users
135 if person_id.is_none() {
136 for cv in communities
138 .filter(|cv| cv.community.deleted || cv.community.removed)
140 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
145 Ok(ListCommunitiesResponse { communities })