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