]> Untitled Git - lemmy.git/blob - crates/apub/src/api/read_community.rs
Remove chatserver (#2919)
[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 activitypub_federation::config::Data;
7 use lemmy_api_common::{
8   community::{GetCommunity, GetCommunityResponse},
9   context::LemmyContext,
10   utils::{check_private_instance, is_mod_or_admin_opt, local_user_view_from_jwt_opt},
11 };
12 use lemmy_db_schema::source::{
13   actor_language::CommunityLanguage,
14   community::Community,
15   local_site::LocalSite,
16   site::Site,
17 };
18 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
19 use lemmy_utils::error::LemmyError;
20
21 #[async_trait::async_trait]
22 impl PerformApub for GetCommunity {
23   type Response = GetCommunityResponse;
24
25   #[tracing::instrument(skip(context))]
26   async fn perform(
27     &self,
28     context: &Data<LemmyContext>,
29   ) -> Result<GetCommunityResponse, LemmyError> {
30     let data: &GetCommunity = self;
31     let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
32     let local_site = LocalSite::read(context.pool()).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, &local_site)?;
39
40     let person_id = local_user_view.as_ref().map(|u| u.person.id);
41
42     let community_id = match data.id {
43       Some(id) => id,
44       None => {
45         let name = data.name.clone().unwrap_or_else(|| "main".to_string());
46         resolve_actor_identifier::<ApubCommunity, Community>(&name, context, &local_user_view, true)
47           .await
48           .map_err(|e| e.with_message("couldnt_find_community"))?
49           .id
50       }
51     };
52
53     let is_mod_or_admin =
54       is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), Some(community_id))
55         .await
56         .is_ok();
57
58     let community_view = CommunityView::read(
59       context.pool(),
60       community_id,
61       person_id,
62       Some(is_mod_or_admin),
63     )
64     .await
65     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
66
67     let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
68       .await
69       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
70
71     let site_id =
72       Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into());
73     let mut site = Site::read_from_apub_id(context.pool(), &site_id.into()).await?;
74     // no need to include metadata for local site (its already available through other endpoints).
75     // this also prevents us from leaking the federation private key.
76     if let Some(s) = &site {
77       if s.actor_id.domain() == Some(context.settings().hostname.as_ref()) {
78         site = None;
79       }
80     }
81
82     let community_id = community_view.community.id;
83     let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;
84
85     let res = GetCommunityResponse {
86       community_view,
87       site,
88       moderators,
89       discussion_languages,
90     };
91
92     // Return the jwt
93     Ok(res)
94   }
95 }