]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/read.rs
fda9912210f630d2b0ba3002eb1c732f87b6b71f
[lemmy.git] / crates / api_crud / src / community / read.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   community::{GetCommunity, GetCommunityResponse},
5   utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
6 };
7 use lemmy_apub::{
8   fetcher::resolve_actor_identifier,
9   objects::{community::ApubCommunity, instance::instance_actor_id_from_url},
10 };
11 use lemmy_db_schema::{
12   source::{community::Community, site::Site},
13   traits::DeleteableOrRemoveable,
14 };
15 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
16 use lemmy_utils::{error::LemmyError, ConnectionId};
17 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
18
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetCommunity {
21   type Response = GetCommunityResponse;
22
23   #[tracing::instrument(skip(context, _websocket_id))]
24   async fn perform(
25     &self,
26     context: &Data<LemmyContext>,
27     _websocket_id: Option<ConnectionId>,
28   ) -> Result<GetCommunityResponse, LemmyError> {
29     let data: &GetCommunity = self;
30     let local_user_view =
31       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
32         .await?;
33
34     if data.name.is_none() && data.id.is_none() {
35       return Err(LemmyError::from_message("no_id_given"));
36     }
37
38     check_private_instance(&local_user_view, context.pool()).await?;
39
40     let person_id = local_user_view.map(|u| u.person.id);
41
42     let community_id = match data.id {
43       Some(id) => id,
44       None => {
45         let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
46         resolve_actor_identifier::<ApubCommunity, Community>(&name, context, true)
47           .await
48           .map_err(|e| e.with_message("couldnt_find_community"))?
49           .id
50       }
51     };
52
53     let mut community_view = blocking(context.pool(), move |conn| {
54       CommunityView::read(conn, community_id, person_id)
55     })
56     .await?
57     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
58
59     // Blank out deleted or removed info for non-logged in users
60     if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
61     {
62       community_view.community = community_view.community.blank_out_deleted_or_removed_info();
63     }
64
65     let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
66       CommunityModeratorView::for_community(conn, community_id)
67     })
68     .await?
69     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
70
71     let online = context
72       .chat_server()
73       .send(GetCommunityUsersOnline { community_id })
74       .await
75       .unwrap_or(1);
76
77     let site_id = instance_actor_id_from_url(community_view.community.actor_id.clone().into());
78     let mut site: Option<Site> = blocking(context.pool(), move |conn| {
79       Site::read_from_apub_id(conn, site_id)
80     })
81     .await??;
82     // no need to include metadata for local site (its already available through other endpoints).
83     // this also prevents us from leaking the federation private key.
84     if let Some(s) = &site {
85       if s.actor_id.domain() == Some(context.settings().hostname.as_ref()) {
86         site = None;
87       }
88     }
89
90     let res = GetCommunityResponse {
91       community_view,
92       site,
93       moderators,
94       online,
95     };
96
97     // Return the jwt
98     Ok(res)
99   }
100 }