]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/read.rs
Fix API and clippy warnings
[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::{source::community::Community_, ListingType, SortType};
5 use lemmy_db_schema::source::community::*;
6 use lemmy_db_views_actor::{
7   community_moderator_view::CommunityModeratorView,
8   community_view::{CommunityQueryBuilder, CommunityView},
9 };
10 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
11 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
12 use std::str::FromStr;
13
14 #[async_trait::async_trait(?Send)]
15 impl PerformCrud for GetCommunity {
16   type Response = GetCommunityResponse;
17
18   async fn perform(
19     &self,
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);
26
27     let community_id = match data.id {
28       Some(id) => id,
29       None => {
30         let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
31         match blocking(context.pool(), move |conn| {
32           Community::read_from_name(conn, &name)
33         })
34         .await?
35         {
36           Ok(community) => community,
37           Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
38         }
39         .id
40       }
41     };
42
43     let community_view = match blocking(context.pool(), move |conn| {
44       CommunityView::read(conn, community_id, person_id)
45     })
46     .await?
47     {
48       Ok(community) => community,
49       Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
50     };
51
52     let moderators: Vec<CommunityModeratorView> = match blocking(context.pool(), move |conn| {
53       CommunityModeratorView::for_community(conn, community_id)
54     })
55     .await?
56     {
57       Ok(moderators) => moderators,
58       Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
59     };
60
61     let online = context
62       .chat_server()
63       .send(GetCommunityUsersOnline { community_id })
64       .await
65       .unwrap_or(1);
66
67     let res = GetCommunityResponse {
68       community_view,
69       moderators,
70       online,
71     };
72
73     // Return the jwt
74     Ok(res)
75   }
76 }
77
78 #[async_trait::async_trait(?Send)]
79 impl PerformCrud for ListCommunities {
80   type Response = ListCommunitiesResponse;
81
82   async fn perform(
83     &self,
84     context: &Data<LemmyContext>,
85     _websocket_id: Option<ConnectionId>,
86   ) -> Result<ListCommunitiesResponse, LemmyError> {
87     let data: &ListCommunities = &self;
88     let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
89
90     let person_id = local_user_view.to_owned().map(|l| l.person.id);
91
92     // Don't show NSFW by default
93     let show_nsfw = match &local_user_view {
94       Some(uv) => uv.local_user.show_nsfw,
95       None => false,
96     };
97
98     let type_ = ListingType::from_str(&data.type_)?;
99     let sort = SortType::from_str(&data.sort)?;
100
101     let page = data.page;
102     let limit = data.limit;
103     let communities = blocking(context.pool(), move |conn| {
104       CommunityQueryBuilder::create(conn)
105         .listing_type(&type_)
106         .sort(&sort)
107         .show_nsfw(show_nsfw)
108         .my_person_id(person_id)
109         .page(page)
110         .limit(limit)
111         .list()
112     })
113     .await??;
114
115     // Return the jwt
116     Ok(ListCommunitiesResponse { communities })
117   }
118 }