]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/read.rs
implement language tags for site/community in db and api (#2434)
[lemmy.git] / crates / api_crud / src / community / read.rs
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},
6 };
7 use lemmy_apub::{
8   fetcher::resolve_actor_identifier,
9   objects::{community::ApubCommunity, instance::instance_actor_id_from_url},
10 };
11 use lemmy_db_schema::{
12   impls::actor_language::default_post_language,
13   source::{actor_language::CommunityLanguage, community::Community, site::Site},
14   traits::DeleteableOrRemoveable,
15 };
16 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
19
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for GetCommunity {
22   type Response = GetCommunityResponse;
23
24   #[tracing::instrument(skip(context, _websocket_id))]
25   async fn perform(
26     &self,
27     context: &Data<LemmyContext>,
28     _websocket_id: Option<ConnectionId>,
29   ) -> Result<GetCommunityResponse, LemmyError> {
30     let data: &GetCommunity = self;
31     let local_user_view =
32       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
33         .await?;
34
35     if data.name.is_none() && data.id.is_none() {
36       return Err(LemmyError::from_message("no_id_given"));
37     }
38
39     check_private_instance(&local_user_view, context.pool()).await?;
40
41     let person_id = local_user_view.as_ref().map(|u| u.person.id);
42
43     let community_id = match data.id {
44       Some(id) => id,
45       None => {
46         let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
47         resolve_actor_identifier::<ApubCommunity, Community>(&name, context, true)
48           .await
49           .map_err(|e| e.with_message("couldnt_find_community"))?
50           .id
51       }
52     };
53
54     let mut community_view = blocking(context.pool(), move |conn| {
55       CommunityView::read(conn, community_id, person_id)
56     })
57     .await?
58     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
59
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)
62     {
63       community_view.community = community_view.community.blank_out_deleted_or_removed_info();
64     }
65
66     let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
67       CommunityModeratorView::for_community(conn, community_id)
68     })
69     .await?
70     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
71
72     let online = context
73       .chat_server()
74       .send(GetCommunityUsersOnline { community_id })
75       .await
76       .unwrap_or(1);
77
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)
81     })
82     .await??;
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()) {
87         site = None;
88       }
89     }
90
91     let community_id = community_view.community.id;
92     let discussion_languages = blocking(context.pool(), move |conn| {
93       CommunityLanguage::read(conn, community_id)
94     })
95     .await??;
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)
99       })
100       .await??
101     } else {
102       None
103     };
104
105     let res = GetCommunityResponse {
106       community_view,
107       site,
108       moderators,
109       online,
110       discussion_languages,
111       default_post_language,
112     };
113
114     // Return the jwt
115     Ok(res)
116   }
117 }