move my_user to separate endpoint

This commit is contained in:
Felix Ableitner 2024-11-22 09:55:18 +01:00
parent 0c580887d7
commit 88b0aba101
6 changed files with 50 additions and 50 deletions

View file

@ -69,7 +69,6 @@ pub async fn leave_admin(
site_view, site_view,
admins, admins,
version: VERSION.to_string(), version: VERSION.to_string(),
my_user: None,
all_languages, all_languages,
discussion_languages, discussion_languages,
oauth_providers: Some(oauth_providers), oauth_providers: Some(oauth_providers),

View file

@ -448,8 +448,6 @@ pub struct GetSiteResponse {
pub site_view: SiteView, pub site_view: SiteView,
pub admins: Vec<PersonView>, pub admins: Vec<PersonView>,
pub version: String, pub version: String,
#[cfg_attr(feature = "full", ts(optional))]
pub my_user: Option<MyUserInfo>,
pub all_languages: Vec<Language>, pub all_languages: Vec<Language>,
pub discussion_languages: Vec<LanguageId>, pub discussion_languages: Vec<LanguageId>,
/// deprecated, use field `tagline` or /api/v3/tagline/list /// deprecated, use field `tagline` or /api/v3/tagline/list

View file

@ -1,22 +1,16 @@
use actix_web::web::{Data, Json}; use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{context::LemmyContext, site::GetSiteResponse};
context::LemmyContext,
site::{GetSiteResponse, MyUserInfo},
};
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
actor_language::{LocalUserLanguage, SiteLanguage}, actor_language::SiteLanguage,
community_block::CommunityBlock,
instance_block::InstanceBlock,
language::Language, language::Language,
local_site_url_blocklist::LocalSiteUrlBlocklist, local_site_url_blocklist::LocalSiteUrlBlocklist,
oauth_provider::OAuthProvider, oauth_provider::OAuthProvider,
person_block::PersonBlock,
tagline::Tagline, tagline::Tagline,
}; };
use lemmy_db_views::structs::{LocalUserView, SiteView}; use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_db_views_actor::structs::{CommunityFollowerView, CommunityModeratorView, PersonView}; use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{ use lemmy_utils::{
error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult}, error::{LemmyError, LemmyResult},
CACHE_DURATION_API, CACHE_DURATION_API,
VERSION, VERSION,
}; };
@ -52,7 +46,6 @@ pub async fn get_site(
site_view, site_view,
admins, admins,
version: VERSION.to_string(), version: VERSION.to_string(),
my_user: None,
all_languages, all_languages,
discussion_languages, discussion_languages,
blocked_urls, blocked_urls,
@ -66,42 +59,6 @@ pub async fn get_site(
.await .await
.map_err(|e| anyhow::anyhow!("Failed to construct site response: {e}"))?; .map_err(|e| anyhow::anyhow!("Failed to construct site response: {e}"))?;
// Build the local user with parallel queries and add it to site response
site_response.my_user = if let Some(ref local_user_view) = local_user_view {
let person_id = local_user_view.person.id;
let local_user_id = local_user_view.local_user.id;
let pool = &mut context.pool();
let (
follows,
community_blocks,
instance_blocks,
person_blocks,
moderates,
discussion_languages,
) = lemmy_db_schema::try_join_with_pool!(pool => (
|pool| CommunityFollowerView::for_person(pool, person_id),
|pool| CommunityBlock::for_person(pool, person_id),
|pool| InstanceBlock::for_person(pool, person_id),
|pool| PersonBlock::for_person(pool, person_id),
|pool| CommunityModeratorView::for_person(pool, person_id, Some(&local_user_view.local_user)),
|pool| LocalUserLanguage::read(pool, local_user_id)
))
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
Some(MyUserInfo {
local_user_view: local_user_view.clone(),
follows,
moderates,
community_blocks,
instance_blocks,
person_blocks,
discussion_languages,
})
} else {
None
};
// filter oauth_providers for public access // filter oauth_providers for public access
if !local_user_view if !local_user_view
.map(|l| l.local_user.admin) .map(|l| l.local_user.admin)

View file

@ -1,2 +1,3 @@
pub mod create; pub mod create;
pub mod delete; pub mod delete;
pub mod my_user;

View file

@ -0,0 +1,43 @@
use actix_web::web::{Data, Json};
use lemmy_api_common::{context::LemmyContext, site::MyUserInfo};
use lemmy_db_schema::source::{
actor_language::LocalUserLanguage,
community_block::CommunityBlock,
instance_block::InstanceBlock,
person_block::PersonBlock,
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views_actor::structs::{CommunityFollowerView, CommunityModeratorView};
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
#[tracing::instrument(skip(context))]
pub async fn get_my_user(
local_user_view: LocalUserView,
context: Data<LemmyContext>,
) -> LemmyResult<Json<MyUserInfo>> {
// Build the local user with parallel queries and add it to site response
let person_id = local_user_view.person.id;
let local_user_id = local_user_view.local_user.id;
let pool = &mut context.pool();
let (follows, community_blocks, instance_blocks, person_blocks, moderates, discussion_languages) =
lemmy_db_schema::try_join_with_pool!(pool => (
|pool| CommunityFollowerView::for_person(pool, person_id),
|pool| CommunityBlock::for_person(pool, person_id),
|pool| InstanceBlock::for_person(pool, person_id),
|pool| PersonBlock::for_person(pool, person_id),
|pool| CommunityModeratorView::for_person(pool, person_id, Some(&local_user_view.local_user)),
|pool| LocalUserLanguage::read(pool, local_user_id)
))
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
Ok(Json(MyUserInfo {
local_user_view: local_user_view.clone(),
follows,
moderates,
community_blocks,
instance_blocks,
person_blocks,
discussion_languages,
}))
}

View file

@ -145,6 +145,7 @@ use lemmy_api_crud::{
user::{ user::{
create::{authenticate_with_oauth, register}, create::{authenticate_with_oauth, register},
delete::delete_account, delete::delete_account,
my_user::get_my_user,
}, },
}; };
use lemmy_apub::api::{ use lemmy_apub::api::{
@ -304,6 +305,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
) )
.service( .service(
web::scope("/account") web::scope("/account")
.route("/my_user", web::get().to(get_my_user))
.route("/list_media", web::get().to(list_media)) .route("/list_media", web::get().to(list_media))
.route("/mention", web::get().to(list_mentions)) .route("/mention", web::get().to(list_mentions))
.route("/replies", web::get().to(list_replies)) .route("/replies", web::get().to(list_replies))