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 impls::actor_language::default_post_language,
13 source::{actor_language::CommunityLanguage, community::Community, site::Site},
14 traits::DeleteableOrRemoveable,
16 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for GetCommunity {
22 type Response = GetCommunityResponse;
24 #[tracing::instrument(skip(context, _websocket_id))]
27 context: &Data<LemmyContext>,
28 _websocket_id: Option<ConnectionId>,
29 ) -> Result<GetCommunityResponse, LemmyError> {
30 let data: &GetCommunity = self;
32 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
35 if data.name.is_none() && data.id.is_none() {
36 return Err(LemmyError::from_message("no_id_given"));
39 check_private_instance(&local_user_view, context.pool()).await?;
41 let person_id = local_user_view.as_ref().map(|u| u.person.id);
43 let community_id = match data.id {
46 let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
47 resolve_actor_identifier::<ApubCommunity, Community>(&name, context, true)
49 .map_err(|e| e.with_message("couldnt_find_community"))?
54 let mut community_view = blocking(context.pool(), move |conn| {
55 CommunityView::read(conn, community_id, person_id)
58 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
60 // Blank out deleted or removed info for non-logged in users
61 if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
63 community_view.community = community_view.community.blank_out_deleted_or_removed_info();
66 let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
67 CommunityModeratorView::for_community(conn, community_id)
70 .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
74 .send(GetCommunityUsersOnline { community_id })
78 let site_id = instance_actor_id_from_url(community_view.community.actor_id.clone().into());
79 let mut site: Option<Site> = blocking(context.pool(), move |conn| {
80 Site::read_from_apub_id(conn, site_id)
83 // no need to include metadata for local site (its already available through other endpoints).
84 // this also prevents us from leaking the federation private key.
85 if let Some(s) = &site {
86 if s.actor_id.domain() == Some(context.settings().hostname.as_ref()) {
91 let community_id = community_view.community.id;
92 let discussion_languages = blocking(context.pool(), move |conn| {
93 CommunityLanguage::read(conn, community_id)
96 let default_post_language = if let Some(user) = local_user_view {
97 blocking(context.pool(), move |conn| {
98 default_post_language(conn, community_id, user.local_user.id)
105 let res = GetCommunityResponse {
110 discussion_languages,
111 default_post_language,