1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 site::{GetSite, GetSiteResponse, MyUserInfo},
7 utils::{check_user_valid, check_validator_time},
10 newtypes::LocalUserId,
12 actor_language::{LocalUserLanguage, SiteLanguage},
17 use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView};
18 use lemmy_db_views_actor::structs::{
20 CommunityFollowerView,
21 CommunityModeratorView,
25 use lemmy_utils::{claims::Claims, error::LemmyError, version};
27 #[async_trait::async_trait(?Send)]
28 impl PerformCrud for GetSite {
29 type Response = GetSiteResponse;
31 #[tracing::instrument(skip(context))]
32 async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetSiteResponse, LemmyError> {
33 let data: &GetSite = self;
35 let site_view = SiteView::read_local(context.pool()).await?;
37 let admins = PersonView::admins(context.pool()).await?;
39 // Build the local user
40 let my_user = if let Some(local_user_view) =
41 local_user_settings_view_from_jwt_opt(data.auth.as_ref(), context).await
43 let person_id = local_user_view.person.id;
44 let local_user_id = local_user_view.local_user.id;
46 let follows = CommunityFollowerView::for_person(context.pool(), person_id)
48 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
50 let person_id = local_user_view.person.id;
51 let community_blocks = CommunityBlockView::for_person(context.pool(), person_id)
53 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
55 let person_id = local_user_view.person.id;
56 let person_blocks = PersonBlockView::for_person(context.pool(), person_id)
58 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
60 let moderates = CommunityModeratorView::for_person(context.pool(), person_id)
62 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
64 let discussion_languages = LocalUserLanguage::read(context.pool(), local_user_id)
66 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
80 let all_languages = Language::read_all(context.pool()).await?;
81 let discussion_languages = SiteLanguage::read_local_raw(context.pool()).await?;
82 let taglines = Tagline::get_all(context.pool(), site_view.local_site.id).await?;
83 let custom_emojis = CustomEmojiView::get_all(context.pool(), site_view.local_site.id).await?;
88 version: version::VERSION.to_string(),
98 #[tracing::instrument(skip_all)]
99 async fn local_user_settings_view_from_jwt_opt(
100 jwt: Option<&Sensitive<String>>,
101 context: &LemmyContext,
102 ) -> Option<LocalUserView> {
105 let claims = Claims::decode(jwt.as_ref(), &context.secret().jwt_secret)
108 let local_user_id = LocalUserId(claims.sub);
109 let local_user_view = LocalUserView::read(context.pool(), local_user_id)
113 local_user_view.person.banned,
114 local_user_view.person.ban_expires,
115 local_user_view.person.deleted,
119 check_validator_time(&local_user_view.local_user.validator_time, &claims).ok()?;
121 Some(local_user_view)