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_db_queries::{
5 from_opt_str_to_opt_enum,
6 source::community::Community_,
10 use lemmy_db_schema::source::community::*;
11 use lemmy_db_views_actor::{
12 community_moderator_view::CommunityModeratorView,
13 community_view::{CommunityQueryBuilder, CommunityView},
15 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
16 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for GetCommunity {
20 type Response = GetCommunityResponse;
24 context: &Data<LemmyContext>,
25 _websocket_id: Option<ConnectionId>,
26 ) -> Result<GetCommunityResponse, LemmyError> {
27 let data: &GetCommunity = self;
28 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
29 let person_id = local_user_view.map(|u| u.person.id);
31 let community_id = match data.id {
34 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
35 blocking(context.pool(), move |conn| {
36 Community::read_from_name(conn, &name)
39 .map_err(|_| ApiError::err("couldnt_find_community"))?
44 let community_view = blocking(context.pool(), move |conn| {
45 CommunityView::read(conn, community_id, person_id)
48 .map_err(|_| ApiError::err("couldnt_find_community"))?;
50 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
51 CommunityModeratorView::for_community(conn, community_id)
54 .map_err(|_| ApiError::err("couldnt_find_community"))?;
58 .send(GetCommunityUsersOnline { community_id })
62 let res = GetCommunityResponse {
73 #[async_trait::async_trait(?Send)]
74 impl PerformCrud for ListCommunities {
75 type Response = ListCommunitiesResponse;
79 context: &Data<LemmyContext>,
80 _websocket_id: Option<ConnectionId>,
81 ) -> Result<ListCommunitiesResponse, LemmyError> {
82 let data: &ListCommunities = self;
83 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
85 let person_id = local_user_view.to_owned().map(|l| l.person.id);
87 // Don't show NSFW by default
88 let show_nsfw = match &local_user_view {
89 Some(uv) => uv.local_user.show_nsfw,
93 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
94 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
97 let limit = data.limit;
98 let communities = blocking(context.pool(), move |conn| {
99 CommunityQueryBuilder::create(conn)
100 .listing_type(listing_type)
102 .show_nsfw(show_nsfw)
103 .my_person_id(person_id)
111 Ok(ListCommunitiesResponse { communities })