diff --git a/src/main.rs b/src/main.rs index 3b3c85e..bd04cea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/proto.rs b/src/proto.rs index 3de30ca..ed174f5 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -40,6 +40,12 @@ pub struct Property { pub signature: Option, } +impl From 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 { diff --git a/src/routes/auth.rs b/src/routes/auth.rs index a6077aa..a7222fe 100644 --- a/src/routes/auth.rs +++ b/src/routes/auth.rs @@ -61,6 +61,13 @@ pub async fn authenticate(State(state): State, Json(payload): Json 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, Json(payload): Json { + 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, Path(user_id): Path) -> Result, StatusCode> { +pub async fn profile(State(state): State, Path(user_id): Path) -> proto::Response { 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())) } }