fix: fetch skins on endpoints, some error handling

This commit is contained in:
dev@ftbsc 2023-01-23 01:34:42 +01:00
parent a090e6f1aa
commit a67add7f74
4 changed files with 42 additions and 8 deletions

View file

@ -6,8 +6,8 @@ use std::{collections::HashMap, sync::Arc};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use clap::Parser; use clap::Parser;
use axum::{Router, routing::{get, post}, response::IntoResponse, Json, http::StatusCode, extract::State}; use axum::{Router, routing::{get, post}, response::IntoResponse, Json, http::StatusCode};
use sea_orm::{EntityTrait, DatabaseConnection, Database, ActiveValue::NotSet, Set}; use sea_orm::{DatabaseConnection, Database};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tracing_subscriber::filter::filter_fn; use tracing_subscriber::filter::filter_fn;
use uuid::Uuid; use uuid::Uuid;

View file

@ -40,6 +40,12 @@ pub struct Property {
pub signature: Option<String>, pub signature: Option<String>,
} }
impl From<crate::entities::property::Model> for Property {
fn from(value: crate::entities::property::Model) -> Self {
Property { name: value.name, value: value.value, signature: value.signature }
}
}
impl Property { impl Property {
pub fn default_skin() -> Property { pub fn default_skin() -> Property {
Property { Property {

View file

@ -61,6 +61,13 @@ pub async fn authenticate(State(state): State<AppState>, Json(payload): Json<pro
if let Some(u) = user { if let Some(u) = user {
if payload.password == u.password { if payload.password == u.password {
let s = entities::property::Entity::find().filter(
entities::property::Column::UserId.eq(u.id)
).one(&state.db).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let skin = match s {
Some(s) => proto::Property::from(s),
None => proto::Property::default_skin(),
};
// make new token // make new token
let access_token = Uuid::new_v4().to_string(); // TODO maybe use a JWT? let access_token = Uuid::new_v4().to_string(); // TODO maybe use a JWT?
entities::token::Entity::insert(entities::token::ActiveModel { entities::token::Entity::insert(entities::token::ActiveModel {
@ -76,7 +83,7 @@ pub async fn authenticate(State(state): State<AppState>, Json(payload): Json<pro
}; };
let response =proto::AuthenticateResponse { let response =proto::AuthenticateResponse {
accessToken: access_token, accessToken: access_token,
user: proto::User { id: u.uuid, username: u.name, properties: Some(vec![ proto::Property::default_skin() ]) }, user: proto::User { id: u.uuid, username: u.name, properties: Some(vec![ skin ]) },
clientToken: client_token, clientToken: client_token,
availableProfiles: vec![profile.clone()], availableProfiles: vec![profile.clone()],
selectedProfile: profile, selectedProfile: profile,

View file

@ -66,6 +66,14 @@ pub async fn has_joined_local(state: &AppState, username: &String, server_id: &S
match user { match user {
Some(user) => { Some(user) => {
let s = entities::property::Entity::find().filter(
entities::property::Column::UserId.eq(user.id)
).one(&state.db).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let skin = match s {
Some(s) => proto::Property::from(s),
None => proto::Property::default_skin(),
};
match state.store.lock().await.get(&user.uuid) { match state.store.lock().await.get(&user.uuid) {
Some(join) => { Some(join) => {
if Utc::now() - join.time < Duration::seconds(state.cfg.time_window as i64) if Utc::now() - join.time < Duration::seconds(state.cfg.time_window as i64)
@ -73,7 +81,7 @@ pub async fn has_joined_local(state: &AppState, username: &String, server_id: &S
let response = proto::JoinResponse { let response = proto::JoinResponse {
id: user.uuid, id: user.uuid,
name: username.clone(), name: username.clone(),
properties: vec![ proto::Property::default_skin() ], properties: vec![ skin ],
}; };
info!(target: "SESSION", "[HAS_JOINED] server found user -> {:?}", response); info!(target: "SESSION", "[HAS_JOINED] server found user -> {:?}", response);
Ok(Json(response)) Ok(Json(response))
@ -95,18 +103,31 @@ pub async fn has_joined_local(state: &AppState, username: &String, server_id: &S
} }
} }
pub async fn profile(State(state): State<AppState>, Path(user_id): Path<Uuid>) -> Result<Json<proto::SessionUser>, StatusCode> { pub async fn profile(State(state): State<AppState>, Path(user_id): Path<Uuid>) -> proto::Response<proto::SessionUser> {
let user = entities::user::Entity::find().filter( let user = entities::user::Entity::find().filter(
entities::user::Column::Uuid.eq(user_id) entities::user::Column::Uuid.eq(user_id)
).one(&state.db).await.unwrap(); ).one(&state.db).await
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, proto::Error::simple("db error").json()))?;
if let Some(u) = user { if let Some(u) = user {
let skin = entities::property::Entity::find().filter(
entities::property::Column::UserId.eq(u.id)
// entities::property::Column::Name.eq("textures"),
).one(&state.db).await.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, proto::Error::simple("db error").json()))?;
let p;
if let Some(s) = skin {
p = proto::Property::from(s);
} else {
p = proto::Property::default_skin();
}
Ok(Json(proto::SessionUser { Ok(Json(proto::SessionUser {
id: u.uuid, id: u.uuid,
name: u.name, name: u.name,
properties: Some(vec![ proto::Property::default_skin() ]), properties: Some(vec![ p ]),
})) }))
} else { } else {
Err(StatusCode::NOT_FOUND) Err((StatusCode::NOT_FOUND, proto::Error::simple("user not found").json()))
} }
} }