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::{from_opt_str_to_opt_enum, ApubObject, ListingType, SortType};
6 use lemmy_db_schema::source::community::*;
7 use lemmy_db_views_actor::{
8 community_moderator_view::CommunityModeratorView,
9 community_view::{CommunityQueryBuilder, CommunityView},
11 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
12 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
14 #[async_trait::async_trait(?Send)]
15 impl PerformCrud for GetCommunity {
16 type Response = GetCommunityResponse;
20 context: &Data<LemmyContext>,
21 _websocket_id: Option<ConnectionId>,
22 ) -> Result<GetCommunityResponse, LemmyError> {
23 let data: &GetCommunity = self;
24 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
25 let person_id = local_user_view.map(|u| u.person.id);
27 let community_id = match data.id {
30 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
31 let community_actor_id = build_actor_id_from_shortname(EndpointType::Community, &name)?;
33 blocking(context.pool(), move |conn| {
34 Community::read_from_apub_id(conn, &community_actor_id)
37 .map_err(|_| ApiError::err("couldnt_find_community"))?
42 let community_view = blocking(context.pool(), move |conn| {
43 CommunityView::read(conn, community_id, person_id)
46 .map_err(|_| ApiError::err("couldnt_find_community"))?;
48 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
49 CommunityModeratorView::for_community(conn, community_id)
52 .map_err(|_| ApiError::err("couldnt_find_community"))?;
56 .send(GetCommunityUsersOnline { community_id })
60 let res = GetCommunityResponse {
71 #[async_trait::async_trait(?Send)]
72 impl PerformCrud for ListCommunities {
73 type Response = ListCommunitiesResponse;
77 context: &Data<LemmyContext>,
78 _websocket_id: Option<ConnectionId>,
79 ) -> Result<ListCommunitiesResponse, LemmyError> {
80 let data: &ListCommunities = self;
81 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
83 let person_id = local_user_view.to_owned().map(|l| l.person.id);
85 // Don't show NSFW by default
86 let show_nsfw = match &local_user_view {
87 Some(uv) => uv.local_user.show_nsfw,
91 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
92 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
95 let limit = data.limit;
96 let communities = blocking(context.pool(), move |conn| {
97 CommunityQueryBuilder::create(conn)
98 .listing_type(listing_type)
100 .show_nsfw(show_nsfw)
101 .my_person_id(person_id)
109 Ok(ListCommunitiesResponse { communities })