]> Untitled Git - lemmy.git/blob - crates/apub/src/api/read_community.rs
Dont return error in case optional auth is invalid (#2879)
[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   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 = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
34     let local_site = LocalSite::read(context.pool()).await?;
35
36     if data.name.is_none() && data.id.is_none() {
37       return Err(LemmyError::from_message("no_id_given"));
38     }
39
40     check_private_instance(&local_user_view, &local_site)?;
41
42     let person_id = local_user_view.as_ref().map(|u| u.person.id);
43
44     let community_id = match data.id {
45       Some(id) => id,
46       None => {
47         let name = data.name.clone().unwrap_or_else(|| "main".to_string());
48         resolve_actor_identifier::<ApubCommunity, Community>(&name, context, &local_user_view, true)
49           .await
50           .map_err(|e| e.with_message("couldnt_find_community"))?
51           .id
52       }
53     };
54
55     let is_mod_or_admin =
56       is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), Some(community_id))
57         .await
58         .is_ok();
59
60     let community_view = CommunityView::read(
61       context.pool(),
62       community_id,
63       person_id,
64       Some(is_mod_or_admin),
65     )
66     .await
67     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
68
69     let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
70       .await
71       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
72
73     let online = context
74       .chat_server()
75       .send(GetCommunityUsersOnline { community_id })
76       .await?;
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.into()).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
92     let res = GetCommunityResponse {
93       community_view,
94       site,
95       moderators,
96       online,
97       discussion_languages,
98     };
99
100     // Return the jwt
101     Ok(res)
102   }
103 }