]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/read.rs
Check user accepted before sending jwt in password reset (fixes #2591) (#2597)
[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::{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::{
14     actor_language::CommunityLanguage,
15     community::Community,
16     local_site::LocalSite,
17     site::Site,
18   },
19   traits::DeleteableOrRemoveable,
20 };
21 use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
22 use lemmy_utils::{error::LemmyError, ConnectionId};
23 use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext};
24
25 #[async_trait::async_trait(?Send)]
26 impl PerformCrud for GetCommunity {
27   type Response = GetCommunityResponse;
28
29   #[tracing::instrument(skip(context, _websocket_id))]
30   async fn perform(
31     &self,
32     context: &Data<LemmyContext>,
33     _websocket_id: Option<ConnectionId>,
34   ) -> Result<GetCommunityResponse, LemmyError> {
35     let data: &GetCommunity = self;
36     let local_user_view =
37       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
38         .await?;
39     let local_site = LocalSite::read(context.pool()).await?;
40
41     if data.name.is_none() && data.id.is_none() {
42       return Err(LemmyError::from_message("no_id_given"));
43     }
44
45     check_private_instance(&local_user_view, &local_site)?;
46
47     let person_id = local_user_view.as_ref().map(|u| u.person.id);
48
49     let community_id = match data.id {
50       Some(id) => id,
51       None => {
52         let name = data.name.clone().unwrap_or_else(|| "main".to_string());
53         resolve_actor_identifier::<ApubCommunity, Community>(&name, context, true)
54           .await
55           .map_err(|e| e.with_message("couldnt_find_community"))?
56           .id
57       }
58     };
59
60     let mut community_view = CommunityView::read(context.pool(), community_id, person_id)
61       .await
62       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
63
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)
66     {
67       community_view.community = community_view.community.blank_out_deleted_or_removed_info();
68     }
69
70     let moderators = CommunityModeratorView::for_community(context.pool(), community_id)
71       .await
72       .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
73
74     let online = context
75       .chat_server()
76       .send(GetCommunityUsersOnline { community_id })
77       .await
78       .unwrap_or(1);
79
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()) {
86         site = None;
87       }
88     }
89
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?
94     } else {
95       None
96     };
97
98     let res = GetCommunityResponse {
99       community_view,
100       site,
101       moderators,
102       online,
103       discussion_languages,
104       default_post_language,
105     };
106
107     // Return the jwt
108     Ok(res)
109   }
110 }