]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/read.rs
Adding shortname fetching for users and communities. Fixes #1662 (#1663)
[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_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},
10 };
11 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
12 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
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         let community_actor_id = build_actor_id_from_shortname(EndpointType::Community, &name)?;
32
33         blocking(context.pool(), move |conn| {
34           Community::read_from_apub_id(conn, &community_actor_id)
35         })
36         .await?
37         .map_err(|_| ApiError::err("couldnt_find_community"))?
38         .id
39       }
40     };
41
42     let community_view = blocking(context.pool(), move |conn| {
43       CommunityView::read(conn, community_id, person_id)
44     })
45     .await?
46     .map_err(|_| ApiError::err("couldnt_find_community"))?;
47
48     let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
49       CommunityModeratorView::for_community(conn, community_id)
50     })
51     .await?
52     .map_err(|_| ApiError::err("couldnt_find_community"))?;
53
54     let online = context
55       .chat_server()
56       .send(GetCommunityUsersOnline { community_id })
57       .await
58       .unwrap_or(1);
59
60     let res = GetCommunityResponse {
61       community_view,
62       moderators,
63       online,
64     };
65
66     // Return the jwt
67     Ok(res)
68   }
69 }
70
71 #[async_trait::async_trait(?Send)]
72 impl PerformCrud for ListCommunities {
73   type Response = ListCommunitiesResponse;
74
75   async fn perform(
76     &self,
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?;
82
83     let person_id = local_user_view.to_owned().map(|l| l.person.id);
84
85     // Don't show NSFW by default
86     let show_nsfw = match &local_user_view {
87       Some(uv) => uv.local_user.show_nsfw,
88       None => false,
89     };
90
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_);
93
94     let page = data.page;
95     let limit = data.limit;
96     let communities = blocking(context.pool(), move |conn| {
97       CommunityQueryBuilder::create(conn)
98         .listing_type(listing_type)
99         .sort(sort)
100         .show_nsfw(show_nsfw)
101         .my_person_id(person_id)
102         .page(page)
103         .limit(limit)
104         .list()
105     })
106     .await??;
107
108     // Return the jwt
109     Ok(ListCommunitiesResponse { communities })
110   }
111 }