fix: fetch skins on endpoints, some error handling
This commit is contained in:
parent
a090e6f1aa
commit
a67add7f74
4 changed files with 42 additions and 8 deletions
|
@ -6,8 +6,8 @@ use std::{collections::HashMap, sync::Arc};
|
|||
|
||||
use chrono::{DateTime, Utc};
|
||||
use clap::Parser;
|
||||
use axum::{Router, routing::{get, post}, response::IntoResponse, Json, http::StatusCode, extract::State};
|
||||
use sea_orm::{EntityTrait, DatabaseConnection, Database, ActiveValue::NotSet, Set};
|
||||
use axum::{Router, routing::{get, post}, response::IntoResponse, Json, http::StatusCode};
|
||||
use sea_orm::{DatabaseConnection, Database};
|
||||
use tokio::sync::Mutex;
|
||||
use tracing_subscriber::filter::filter_fn;
|
||||
use uuid::Uuid;
|
||||
|
|
|
@ -40,6 +40,12 @@ pub struct Property {
|
|||
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 {
|
||||
pub fn default_skin() -> Property {
|
||||
Property {
|
||||
|
|
|
@ -61,6 +61,13 @@ pub async fn authenticate(State(state): State<AppState>, Json(payload): Json<pro
|
|||
|
||||
if let Some(u) = user {
|
||||
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
|
||||
let access_token = Uuid::new_v4().to_string(); // TODO maybe use a JWT?
|
||||
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 {
|
||||
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,
|
||||
availableProfiles: vec![profile.clone()],
|
||||
selectedProfile: profile,
|
||||
|
|
|
@ -66,6 +66,14 @@ pub async fn has_joined_local(state: &AppState, username: &String, server_id: &S
|
|||
|
||||
match 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) {
|
||||
Some(join) => {
|
||||
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 {
|
||||
id: user.uuid,
|
||||
name: username.clone(),
|
||||
properties: vec![ proto::Property::default_skin() ],
|
||||
properties: vec![ skin ],
|
||||
};
|
||||
info!(target: "SESSION", "[HAS_JOINED] server found user -> {:?}", 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(
|
||||
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 {
|
||||
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 {
|
||||
id: u.uuid,
|
||||
name: u.name,
|
||||
properties: Some(vec![ proto::Property::default_skin() ]),
|
||||
properties: Some(vec![ p ]),
|
||||
}))
|
||||
} else {
|
||||
Err(StatusCode::NOT_FOUND)
|
||||
Err((StatusCode::NOT_FOUND, proto::Error::simple("user not found").json()))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue