]> Untitled Git - lemmy.git/blob - crates/apub/src/api/read_community.rs
Error enum fixed (#3487)
[lemmy.git] / crates / apub / src / api / read_community.rs
1 use crate::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
2 use activitypub_federation::config::Data;
3 use actix_web::web::{Json, Query};
4 use lemmy_api_common::{
5   community::{GetCommunity, GetCommunityResponse},
6   context::LemmyContext,
7   utils::{check_private_instance, is_mod_or_admin_opt, local_user_view_from_jwt_opt},
8 };
9 use lemmy_db_schema::source::{
10   actor_language::CommunityLanguage,
11   community::Community,
12   local_site::LocalSite,
13   site::Site,
14 };
15 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
16 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorExt2, LemmyErrorType};
17
18 #[tracing::instrument(skip(context))]
19 pub async fn read_community(
20   data: Query<GetCommunity>,
21   context: Data<LemmyContext>,
22 ) -> Result<Json<GetCommunityResponse>, LemmyError> {
23   let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
24   let local_site = LocalSite::read(context.pool()).await?;
25
26   if data.name.is_none() && data.id.is_none() {
27     return Err(LemmyErrorType::NoIdGiven)?;
28   }
29
30   check_private_instance(&local_user_view, &local_site)?;
31
32   let person_id = local_user_view.as_ref().map(|u| u.person.id);
33
34   let community_id = match data.id {
35     Some(id) => id,
36     None => {
37       let name = data.name.clone().unwrap_or_else(|| "main".to_string());
38       resolve_actor_identifier::<ApubCommunity, Community>(&name, &context, &local_user_view, true)
39         .await
40         .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?
41         .id
42     }
43   };
44
45   let is_mod_or_admin =
46     is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), Some(community_id))
47       .await
48       .is_ok();
49
50   let community_view = CommunityView::read(
51     context.pool(),
52     community_id,
53     person_id,
54     Some(is_mod_or_admin),
55   )
56   .await
57   .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
58
59   let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
60     .await
61     .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
62
63   let site_id = Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into());
64   let mut site = Site::read_from_apub_id(context.pool(), &site_id.into()).await?;
65   // no need to include metadata for local site (its already available through other endpoints).
66   // this also prevents us from leaking the federation private key.
67   if let Some(s) = &site {
68     if s.actor_id.domain() == Some(context.settings().hostname.as_ref()) {
69       site = None;
70     }
71   }
72
73   let community_id = community_view.community.id;
74   let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;
75
76   Ok(Json(GetCommunityResponse {
77     community_view,
78     site,
79     moderators,
80     discussion_languages,
81   }))
82 }