]> Untitled Git - lemmy.git/blob - crates/apub/src/api/read_community.rs
Making the chat server an actor. (#2793)
[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::{
14   impls::actor_language::default_post_language,
15   source::{
16     actor_language::CommunityLanguage,
17     community::Community,
18     local_site::LocalSite,
19     site::Site,
20   },
21 };
22 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
23 use lemmy_utils::{error::LemmyError, ConnectionId};
24
25 #[async_trait::async_trait]
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, &local_user_view, true)
54           .await
55           .map_err(|e| e.with_message("couldnt_find_community"))?
56           .id
57       }
58     };
59
60     let is_mod_or_admin =
61       is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), Some(community_id))
62         .await
63         .is_ok();
64
65     let community_view = CommunityView::read(
66       context.pool(),
67       community_id,
68       person_id,
69       Some(is_mod_or_admin),
70     )
71     .await
72     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
73
74     let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
75       .await
76       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
77
78     let online = context
79       .chat_server()
80       .send(GetCommunityUsersOnline { community_id })
81       .await?;
82
83     let site_id =
84       Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into());
85     let mut site = Site::read_from_apub_id(context.pool(), &site_id.into()).await?;
86     // no need to include metadata for local site (its already available through other endpoints).
87     // this also prevents us from leaking the federation private key.
88     if let Some(s) = &site {
89       if s.actor_id.domain() == Some(context.settings().hostname.as_ref()) {
90         site = None;
91       }
92     }
93
94     let community_id = community_view.community.id;
95     let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;
96     let default_post_language = if let Some(user) = local_user_view {
97       default_post_language(context.pool(), community_id, user.local_user.id).await?
98     } else {
99       None
100     };
101
102     let res = GetCommunityResponse {
103       community_view,
104       site,
105       moderators,
106       online,
107       discussion_languages,
108       default_post_language,
109     };
110
111     // Return the jwt
112     Ok(res)
113   }
114 }