1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{GetCommunity, GetCommunityResponse},
5 utils::{check_private_instance, get_local_user_view_from_jwt_opt},
8 fetcher::resolve_actor_identifier,
9 objects::{community::ApubCommunity, instance::instance_actor_id_from_url},
11 use lemmy_db_schema::{
12 impls::actor_language::default_post_language,
14 actor_language::CommunityLanguage,
16 local_site::LocalSite,
19 traits::DeleteableOrRemoveable,
21 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
22 use lemmy_utils::{error::LemmyError, ConnectionId};
23 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for GetCommunity {
27 type Response = GetCommunityResponse;
29 #[tracing::instrument(skip(context, _websocket_id))]
32 context: &Data<LemmyContext>,
33 _websocket_id: Option<ConnectionId>,
34 ) -> Result<GetCommunityResponse, LemmyError> {
35 let data: &GetCommunity = self;
37 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
39 let local_site = LocalSite::read(context.pool()).await?;
41 if data.name.is_none() && data.id.is_none() {
42 return Err(LemmyError::from_message("no_id_given"));
45 check_private_instance(&local_user_view, &local_site)?;
47 let person_id = local_user_view.as_ref().map(|u| u.person.id);
49 let community_id = match data.id {
52 let name = data.name.clone().unwrap_or_else(|| "main".to_string());
53 resolve_actor_identifier::<ApubCommunity, Community>(&name, context, true)
55 .map_err(|e| e.with_message("couldnt_find_community"))?
60 let mut community_view = CommunityView::read(context.pool(), community_id, person_id)
62 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
64 // Blank out deleted or removed info for non-logged in users
65 if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
67 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
70 let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
72 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
76 .send(GetCommunityUsersOnline { community_id })
80 let site_id = instance_actor_id_from_url(community_view.community.actor_id.clone().into());
81 let mut site = Site::read_from_apub_id(context.pool(), site_id).await?;
82 // no need to include metadata for local site (its already available through other endpoints).
83 // this also prevents us from leaking the federation private key.
84 if let Some(s) = &site {
85 if s.actor_id.domain() == Some(context.settings().hostname.as_ref()) {
90 let community_id = community_view.community.id;
91 let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;
92 let default_post_language = if let Some(user) = local_user_view {
93 default_post_language(context.pool(), community_id, user.local_user.id).await?
98 let res = GetCommunityResponse {
103 discussion_languages,
104 default_post_language,