]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/read.rs
Running clippy --fix (#1647)
[lemmy.git] / crates / api_crud / src / community / read.rs
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_,
7   ListingType,
8   SortType,
9 };
10 use lemmy_db_schema::source::community::*;
11 use lemmy_db_views_actor::{
12   community_moderator_view::CommunityModeratorView,
13   community_view::{CommunityQueryBuilder, CommunityView},
14 };
15 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
16 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
17
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for GetCommunity {
20   type Response = GetCommunityResponse;
21
22   async fn perform(
23     &self,
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);
30
31     let community_id = match data.id {
32       Some(id) => id,
33       None => {
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)
37         })
38         .await?
39         .map_err(|_| ApiError::err("couldnt_find_community"))?
40         .id
41       }
42     };
43
44     let community_view = blocking(context.pool(), move |conn| {
45       CommunityView::read(conn, community_id, person_id)
46     })
47     .await?
48     .map_err(|_| ApiError::err("couldnt_find_community"))?;
49
50     let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
51       CommunityModeratorView::for_community(conn, community_id)
52     })
53     .await?
54     .map_err(|_| ApiError::err("couldnt_find_community"))?;
55
56     let online = context
57       .chat_server()
58       .send(GetCommunityUsersOnline { community_id })
59       .await
60       .unwrap_or(1);
61
62     let res = GetCommunityResponse {
63       community_view,
64       moderators,
65       online,
66     };
67
68     // Return the jwt
69     Ok(res)
70   }
71 }
72
73 #[async_trait::async_trait(?Send)]
74 impl PerformCrud for ListCommunities {
75   type Response = ListCommunitiesResponse;
76
77   async fn perform(
78     &self,
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?;
84
85     let person_id = local_user_view.to_owned().map(|l| l.person.id);
86
87     // Don't show NSFW by default
88     let show_nsfw = match &local_user_view {
89       Some(uv) => uv.local_user.show_nsfw,
90       None => false,
91     };
92
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_);
95
96     let page = data.page;
97     let limit = data.limit;
98     let communities = blocking(context.pool(), move |conn| {
99       CommunityQueryBuilder::create(conn)
100         .listing_type(listing_type)
101         .sort(sort)
102         .show_nsfw(show_nsfw)
103         .my_person_id(person_id)
104         .page(page)
105         .limit(limit)
106         .list()
107     })
108     .await??;
109
110     // Return the jwt
111     Ok(ListCommunitiesResponse { communities })
112   }
113 }