yggdrasil/src/util/structs/Profile.rs

144 lines
4.3 KiB
Rust

/*
* Yggdrasil: Minecraft authentication server
* Copyright (C) 2023 0xf8.dev@proton.me
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use serde::{Deserialize, Serialize};
use structs::{Cape::Cape, ProfileAttributes::ProfileAttributes};
use crate::*;
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Profile {
pub id: i64,
pub uuid: String,
pub created: i64, // unix timestamp / 1000
pub owner: i64,
pub name: String,
pub name_history: String,
pub skin_variant: String,
pub capes: Option<Vec<Cape>>,
pub active_cape: Option<Cape>,
pub attributes: ProfileAttributes,
}
impl Profile {
pub async fn from_id(db: &Database, id: i64) -> Option<Self> {
let record = sqlx::query_as!(ProfileRaw, "SELECT * FROM profiles WHERE id = $1", id)
.fetch_one(&db.pool)
.await;
match record {
Ok(r) => Some(r.complete(db).await),
Err(_) => None,
}
}
pub async fn from_uuid(db: &Database, uuid: String) -> Option<Self> {
let record = sqlx::query_as!(ProfileRaw, "SELECT * FROM profiles WHERE uuid = $1", uuid)
.fetch_one(&db.pool)
.await;
match record {
Ok(r) => Some(r.complete(db).await),
Err(_) => None
}
}
pub async fn from_name(db: &Database, name: String) -> Option<Self> {
let record = sqlx::query_as!(ProfileRaw, "SELECT * FROM profiles WHERE name = $1", name)
.fetch_one(&db.pool)
.await;
match record {
Ok(r) => Some(r.complete(db).await),
Err(_) => None
}
}
pub async fn get_skin(&self, db: &Database) -> Option<String> {
// TODO: skin overrides
if self.skin_variant == "NONE" {
return None;
}
Some(format!(
"{}/textures/skins/{}",
db.config.external_base_url, self.uuid
))
}
pub async fn get_cape(&self, db: &Database) -> Option<String> {
// TODO: cape overrides
if self.active_cape.is_none() {
return None;
}
let cape = self.active_cape.as_ref().unwrap();
Some(format!(
"{}/textures/capes/{}",
db.config.external_base_url, cape.alias
))
}
}
#[derive(Deserialize, Serialize, Debug)]
pub struct ProfileRaw {
pub id: i64,
pub uuid: String,
pub created: i64,
pub owner: i64,
pub name: String,
pub name_history: String,
pub skin_variant: String,
pub capes: Option<String>,
pub active_cape: Option<i64>,
pub attributes: String,
}
impl ProfileRaw {
pub async fn complete(self, db: &Database) -> Profile {
Profile {
id: self.id,
uuid: self.uuid,
created: self.created,
owner: self.owner,
name: self.name,
name_history: self.name_history,
skin_variant: self.skin_variant,
capes: match self.capes {
None => None,
Some(capes) => Some(
json::parse(capes.as_str())
.map(|c| {
serde_json::from_str::<Cape>(c.to_string().as_str())
.expect("Couldn't parse cape")
})
.into_iter()
.collect(),
),
},
active_cape: match self.active_cape {
None => None,
Some(active_cape) => Cape::from_id(db, active_cape).await,
},
attributes: serde_json::from_str(self.attributes.as_str())
.expect("Couldn't parse profile attributes"),
}
}
}