]> Untitled Git - lemmy.git/blob - crates/apub/src/api/read_community.rs
Replace Option<bool> with bool for PostQuery and CommentQuery (#3819) (#3857)
[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 get_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(&mut 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 = is_mod_or_admin_opt(
46     &mut context.pool(),
47     local_user_view.as_ref(),
48     Some(community_id),
49   )
50   .await
51   .is_ok();
52
53   let community_view = CommunityView::read(
54     &mut context.pool(),
55     community_id,
56     person_id,
57     is_mod_or_admin,
58   )
59   .await
60   .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
61
62   let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id)
63     .await
64     .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
65
66   let site_id = Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into());
67   let mut site = Site::read_from_apub_id(&mut context.pool(), &site_id.into()).await?;
68   // no need to include metadata for local site (its already available through other endpoints).
69   // this also prevents us from leaking the federation private key.
70   if let Some(s) = &site {
71     if s.actor_id.domain() == Some(context.settings().hostname.as_ref()) {
72       site = None;
73     }
74   }
75
76   let community_id = community_view.community.id;
77   let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;
78
79   Ok(Json(GetCommunityResponse {
80     community_view,
81     site,
82     moderators,
83     discussion_languages,
84   }))
85 }