1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{GetCommunity, GetCommunityResponse},
5 utils::{blocking, 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 source::{community::Community, site::Site},
13 traits::DeleteableOrRemoveable,
15 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
16 use lemmy_utils::{error::LemmyError, ConnectionId};
17 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetCommunity {
21 type Response = GetCommunityResponse;
23 #[tracing::instrument(skip(context, _websocket_id))]
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<GetCommunityResponse, LemmyError> {
29 let data: &GetCommunity = self;
31 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
34 if data.name.is_none() && data.id.is_none() {
35 return Err(LemmyError::from_message("no_id_given"));
38 check_private_instance(&local_user_view, context.pool()).await?;
40 let person_id = local_user_view.map(|u| u.person.id);
42 let community_id = match data.id {
45 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
46 resolve_actor_identifier::<ApubCommunity, Community>(&name, context)
48 .map_err(|e| e.with_message("couldnt_find_community"))?
53 let mut community_view = blocking(context.pool(), move |conn| {
54 CommunityView::read(conn, community_id, person_id)
57 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
59 // Blank out deleted or removed info for non-logged in users
60 if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
62 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
65 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
66 CommunityModeratorView::for_community(conn, community_id)
69 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
73 .send(GetCommunityUsersOnline { community_id })
77 let site_id = instance_actor_id_from_url(community_view.community.actor_id.clone().into());
78 let mut site: Option<Site> = blocking(context.pool(), move |conn| {
79 Site::read_from_apub_id(conn, site_id)
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 res = GetCommunityResponse {