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(|e| e.with_message("couldnt_find_community"))?
54 let mut community_view = blocking(context.pool(), move |conn| {
55 CommunityView::read(conn, community_id, person_id)
58 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
60 // Blank out deleted or removed info for non-logged in users
61 if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
63 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
66 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
67 CommunityModeratorView::for_community(conn, community_id)
70 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
74 .send(GetCommunityUsersOnline { community_id })
78 let res = GetCommunityResponse {
89 #[async_trait::async_trait(?Send)]
90 impl PerformCrud for ListCommunities {
91 type Response = ListCommunitiesResponse;
93 #[tracing::instrument(skip(context, _websocket_id))]
96 context: &Data<LemmyContext>,
97 _websocket_id: Option<ConnectionId>,
98 ) -> Result<ListCommunitiesResponse, LemmyError> {
99 let data: &ListCommunities = self;
100 let local_user_view =
101 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
104 check_private_instance(&local_user_view, context.pool()).await?;
106 let person_id = local_user_view.to_owned().map(|l| l.person.id);
108 // Don't show NSFW by default
109 let show_nsfw = match &local_user_view {
110 Some(uv) => uv.local_user.show_nsfw,
114 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
115 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
117 let page = data.page;
118 let limit = data.limit;
119 let mut communities = blocking(context.pool(), move |conn| {
120 CommunityQueryBuilder::create(conn)
121 .listing_type(listing_type)
123 .show_nsfw(show_nsfw)
124 .my_person_id(person_id)
131 // Blank out deleted or removed info for non-logged in users
132 if person_id.is_none() {
133 for cv in communities
135 .filter(|cv| cv.community.deleted || cv.community.removed)
137 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
142 Ok(ListCommunitiesResponse { communities })