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},
7 utils::{check_private_instance, is_mod_or_admin_opt, local_user_view_from_jwt_opt},
9 use lemmy_db_schema::source::{
10 actor_language::CommunityLanguage,
12 local_site::LocalSite,
15 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
16 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorExt2, LemmyErrorType};
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?;
26 if data.name.is_none() && data.id.is_none() {
27 return Err(LemmyErrorType::NoIdGiven)?;
30 check_private_instance(&local_user_view, &local_site)?;
32 let person_id = local_user_view.as_ref().map(|u| u.person.id);
34 let community_id = match data.id {
37 let name = data.name.clone().unwrap_or_else(|| "main".to_string());
38 resolve_actor_identifier::<ApubCommunity, Community>(&name, &context, &local_user_view, true)
40 .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?
45 let is_mod_or_admin = is_mod_or_admin_opt(
47 local_user_view.as_ref(),
53 let community_view = CommunityView::read(
57 Some(is_mod_or_admin),
60 .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
62 let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id)
64 .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
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()) {
76 let community_id = community_view.community.id;
77 let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;
79 Ok(Json(GetCommunityResponse {