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