2023-09-21 10:42:28 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-09-21 10:42:28 +00:00
|
|
|
site::{GetSiteResponse, MyUserInfo},
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_schema::source::{
|
|
|
|
actor_language::{LocalUserLanguage, SiteLanguage},
|
|
|
|
language::Language,
|
|
|
|
tagline::Tagline,
|
2022-12-19 11:40:22 +00:00
|
|
|
};
|
2023-05-25 14:50:07 +00:00
|
|
|
use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views_actor::structs::{
|
|
|
|
CommunityBlockView,
|
|
|
|
CommunityFollowerView,
|
|
|
|
CommunityModeratorView,
|
2023-09-20 09:56:13 +00:00
|
|
|
InstanceBlockView,
|
2022-05-03 17:44:13 +00:00
|
|
|
PersonBlockView,
|
2023-03-01 17:19:46 +00:00
|
|
|
PersonView,
|
2021-08-19 20:54:15 +00:00
|
|
|
};
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
error::{LemmyError, LemmyErrorExt, LemmyErrorType},
|
|
|
|
version,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-28 14:39:38 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn get_site(
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: Option<LocalUserView>,
|
2023-10-09 10:46:12 +00:00
|
|
|
context: Data<LemmyContext>,
|
2023-07-28 14:39:38 +00:00
|
|
|
) -> Result<Json<GetSiteResponse>, LemmyError> {
|
|
|
|
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
|
|
|
|
|
|
|
let admins = PersonView::admins(&mut context.pool()).await?;
|
|
|
|
|
|
|
|
// Build the local user
|
2023-09-21 10:42:28 +00:00
|
|
|
let my_user = if let Some(local_user_view) = local_user_view {
|
2023-07-28 14:39:38 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let local_user_id = local_user_view.local_user.id;
|
|
|
|
|
|
|
|
let follows = CommunityFollowerView::for_person(&mut context.pool(), person_id)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
|
|
|
|
|
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let community_blocks = CommunityBlockView::for_person(&mut context.pool(), person_id)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
|
|
|
|
|
2023-09-20 09:56:13 +00:00
|
|
|
let instance_blocks = InstanceBlockView::for_person(&mut context.pool(), person_id)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
|
|
|
|
|
2023-07-28 14:39:38 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let person_blocks = PersonBlockView::for_person(&mut context.pool(), person_id)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
|
|
|
|
|
|
|
|
let moderates = CommunityModeratorView::for_person(&mut context.pool(), person_id)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
|
|
|
|
|
|
|
|
let discussion_languages = LocalUserLanguage::read(&mut context.pool(), local_user_id)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
|
|
|
|
|
|
|
|
Some(MyUserInfo {
|
|
|
|
local_user_view,
|
|
|
|
follows,
|
|
|
|
moderates,
|
|
|
|
community_blocks,
|
2023-09-20 09:56:13 +00:00
|
|
|
instance_blocks,
|
2023-07-28 14:39:38 +00:00
|
|
|
person_blocks,
|
2022-10-06 18:27:58 +00:00
|
|
|
discussion_languages,
|
2021-03-25 19:19:40 +00:00
|
|
|
})
|
2023-07-28 14:39:38 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let all_languages = Language::read_all(&mut context.pool()).await?;
|
|
|
|
let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
|
|
|
|
let taglines = Tagline::get_all(&mut context.pool(), site_view.local_site.id).await?;
|
|
|
|
let custom_emojis =
|
|
|
|
CustomEmojiView::get_all(&mut context.pool(), site_view.local_site.id).await?;
|
|
|
|
|
|
|
|
Ok(Json(GetSiteResponse {
|
|
|
|
site_view,
|
|
|
|
admins,
|
|
|
|
version: version::VERSION.to_string(),
|
|
|
|
my_user,
|
|
|
|
all_languages,
|
|
|
|
discussion_languages,
|
|
|
|
taglines,
|
|
|
|
custom_emojis,
|
|
|
|
}))
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|