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},
8 websocket::handlers::online_users::GetUsersOnline,
10 use lemmy_db_schema::{
11 newtypes::LocalUserId,
13 actor_language::{LocalUserLanguage, SiteLanguage},
18 use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView};
19 use lemmy_db_views_actor::structs::{
21 CommunityFollowerView,
22 CommunityModeratorView,
26 use lemmy_utils::{claims::Claims, error::LemmyError, version, ConnectionId};
28 #[async_trait::async_trait(?Send)]
29 impl PerformCrud for GetSite {
30 type Response = GetSiteResponse;
32 #[tracing::instrument(skip(context, _websocket_id))]
35 context: &Data<LemmyContext>,
36 _websocket_id: Option<ConnectionId>,
37 ) -> Result<GetSiteResponse, LemmyError> {
38 let data: &GetSite = self;
40 let site_view = SiteView::read_local(context.pool()).await?;
42 let admins = PersonView::admins(context.pool()).await?;
44 let online = context.chat_server().send(GetUsersOnline).await?;
46 // Build the local user
47 let my_user = if let Some(local_user_view) =
48 local_user_settings_view_from_jwt_opt(data.auth.as_ref(), context).await
50 let person_id = local_user_view.person.id;
51 let local_user_id = local_user_view.local_user.id;
53 let follows = CommunityFollowerView::for_person(context.pool(), person_id)
55 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
57 let person_id = local_user_view.person.id;
58 let community_blocks = CommunityBlockView::for_person(context.pool(), person_id)
60 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
62 let person_id = local_user_view.person.id;
63 let person_blocks = PersonBlockView::for_person(context.pool(), person_id)
65 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
67 let moderates = CommunityModeratorView::for_person(context.pool(), person_id)
69 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
71 let discussion_languages = LocalUserLanguage::read(context.pool(), local_user_id)
73 .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
87 let all_languages = Language::read_all(context.pool()).await?;
88 let discussion_languages = SiteLanguage::read_local_raw(context.pool()).await?;
89 let taglines = Tagline::get_all(context.pool(), site_view.local_site.id).await?;
90 let custom_emojis = CustomEmojiView::get_all(context.pool(), site_view.local_site.id).await?;
96 version: version::VERSION.to_string(),
106 #[tracing::instrument(skip_all)]
107 async fn local_user_settings_view_from_jwt_opt(
108 jwt: Option<&Sensitive<String>>,
109 context: &LemmyContext,
110 ) -> Option<LocalUserView> {
113 let claims = Claims::decode(jwt.as_ref(), &context.secret().jwt_secret)
116 let local_user_id = LocalUserId(claims.sub);
117 let local_user_view = LocalUserView::read(context.pool(), local_user_id)
121 local_user_view.person.banned,
122 local_user_view.person.ban_expires,
123 local_user_view.person.deleted,
127 check_validator_time(&local_user_view.local_user.validator_time, &claims).ok()?;
129 Some(local_user_view)