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