1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt_opt};
4 use lemmy_apub::{build_actor_id_from_shortname, EndpointType};
5 use lemmy_db_queries::{
6 from_opt_str_to_opt_enum,
8 DeleteableOrRemoveable,
12 use lemmy_db_schema::source::community::*;
13 use lemmy_db_views_actor::{
14 community_moderator_view::CommunityModeratorView,
15 community_view::{CommunityQueryBuilder, CommunityView},
17 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
18 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for GetCommunity {
22 type Response = GetCommunityResponse;
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<GetCommunityResponse, LemmyError> {
29 let data: &GetCommunity = self;
30 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
31 let person_id = local_user_view.map(|u| u.person.id);
33 let community_id = match data.id {
36 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
37 let community_actor_id = build_actor_id_from_shortname(EndpointType::Community, &name)?;
39 blocking(context.pool(), move |conn| {
40 Community::read_from_apub_id(conn, &community_actor_id)
43 .map_err(|_| ApiError::err("couldnt_find_community"))?
48 let mut community_view = blocking(context.pool(), move |conn| {
49 CommunityView::read(conn, community_id, person_id)
52 .map_err(|_| ApiError::err("couldnt_find_community"))?;
54 // Blank out deleted or removed info
55 if community_view.community.deleted || community_view.community.removed {
56 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
59 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
60 CommunityModeratorView::for_community(conn, community_id)
63 .map_err(|_| ApiError::err("couldnt_find_community"))?;
67 .send(GetCommunityUsersOnline { community_id })
71 let res = GetCommunityResponse {
82 #[async_trait::async_trait(?Send)]
83 impl PerformCrud for ListCommunities {
84 type Response = ListCommunitiesResponse;
88 context: &Data<LemmyContext>,
89 _websocket_id: Option<ConnectionId>,
90 ) -> Result<ListCommunitiesResponse, LemmyError> {
91 let data: &ListCommunities = self;
92 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
94 let person_id = local_user_view.to_owned().map(|l| l.person.id);
96 // Don't show NSFW by default
97 let show_nsfw = match &local_user_view {
98 Some(uv) => uv.local_user.show_nsfw,
102 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
103 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
105 let page = data.page;
106 let limit = data.limit;
107 let mut communities = blocking(context.pool(), move |conn| {
108 CommunityQueryBuilder::create(conn)
109 .listing_type(listing_type)
111 .show_nsfw(show_nsfw)
112 .my_person_id(person_id)
119 // Blank out deleted or removed info
120 for cv in communities
122 .filter(|cv| cv.community.deleted || cv.community.removed)
124 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
128 Ok(ListCommunitiesResponse { communities })