3 fetcher::resolve_actor_identifier,
4 objects::community::ApubCommunity,
6 use actix_web::web::Data;
7 use lemmy_api_common::{
8 community::{GetCommunity, GetCommunityResponse},
10 utils::{check_private_instance, get_local_user_view_from_jwt_opt},
11 websocket::messages::GetCommunityUsersOnline,
13 use lemmy_db_schema::{
14 impls::actor_language::default_post_language,
16 actor_language::CommunityLanguage,
18 local_site::LocalSite,
21 traits::DeleteableOrRemoveable,
23 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
24 use lemmy_utils::{error::LemmyError, ConnectionId};
26 #[async_trait::async_trait(?Send)]
27 impl PerformApub for GetCommunity {
28 type Response = GetCommunityResponse;
30 #[tracing::instrument(skip(context, _websocket_id))]
33 context: &Data<LemmyContext>,
34 _websocket_id: Option<ConnectionId>,
35 ) -> Result<GetCommunityResponse, LemmyError> {
36 let data: &GetCommunity = self;
38 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
40 let local_site = LocalSite::read(context.pool()).await?;
42 if data.name.is_none() && data.id.is_none() {
43 return Err(LemmyError::from_message("no_id_given"));
46 check_private_instance(&local_user_view, &local_site)?;
48 let person_id = local_user_view.as_ref().map(|u| u.person.id);
50 let community_id = match data.id {
53 let name = data.name.clone().unwrap_or_else(|| "main".to_string());
54 resolve_actor_identifier::<ApubCommunity, Community>(&name, context, true)
56 .map_err(|e| e.with_message("couldnt_find_community"))?
61 let mut community_view = CommunityView::read(context.pool(), community_id, person_id)
63 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
65 // Blank out deleted or removed info for non-logged in users
66 if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
68 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
71 let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
73 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
77 .send(GetCommunityUsersOnline { community_id })
82 Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into());
83 let mut site = Site::read_from_apub_id(context.pool(), site_id).await?;
84 // no need to include metadata for local site (its already available through other endpoints).
85 // this also prevents us from leaking the federation private key.
86 if let Some(s) = &site {
87 if s.actor_id.domain() == Some(context.settings().hostname.as_ref()) {
92 let community_id = community_view.community.id;
93 let discussion_languages = CommunityLanguage::read(context.pool(), community_id).await?;
94 let default_post_language = if let Some(user) = local_user_view {
95 default_post_language(context.pool(), community_id, user.local_user.id).await?
100 let res = GetCommunityResponse {
105 discussion_languages,
106 default_post_language,