yggdrasil/src/util/structs/profile.rs

208 lines
6.2 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 anyhow::Result;
use log::debug;
use serde::{Deserialize, Serialize};
use structs::{cape::Cape, profile_attributes::{ProfileAttributes, ProfileAttributesSimple}};
use crate::*;
use super::account::Account;
#[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: ProfileAttributesSimple,
}
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(e) => { debug!("{e}"); 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(e) => { debug!("{e}"); 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(e) => { debug!("{e}"); None },
}
}
pub async fn get_owner(&self, db: &Database) -> Option<Account> {
Account::from_id(db, self.owner).await
}
pub async fn new(db: &Database, owner: Account, name: String, attr: ProfileAttributesSimple) -> Result<Profile> {
let created = (get_unix_timestamp() / 1000) as i64;
let uuidv4 = uuid::Uuid::new_v4().to_string();
let attributes = attr.to_json().to_string();
let r = sqlx::query!("INSERT INTO profiles(uuid, created, owner, name, name_history, skin_variant, attributes) VALUES ($1, $2, $3, $4, $4, $5, $6) RETURNING (id)",
uuidv4, created, owner.id, name, "NONE", attributes)
.fetch_one(&db.pool)
.await?;
Ok(Profile {
id: r.id,
uuid: uuidv4,
created,
owner: owner.id,
name: name.to_owned(),
name_history: name,
skin_variant: String::from("NONE"),
capes: None,
active_cape: None,
attributes: attr,
})
}
pub async fn del(db: &Database, id: i64) -> Result<String> {
let r = sqlx::query!("DELETE FROM profiles WHERE id = $1 RETURNING (uuid)", id)
.fetch_one(&db.pool)
.await?;
Ok(r.uuid)
}
pub fn to_simple(self) -> ProfileSimple {
ProfileSimple {
id: self.id,
owner: self.owner,
uuid: self.uuid,
name: self.name,
active_cape: self.active_cape,
attributes: self.attributes,
}
}
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 ProfileSimple {
pub id: i64,
pub owner: i64,
pub uuid: String,
pub name: String,
pub active_cape: Option<Cape>,
pub attributes: ProfileAttributesSimple
}
#[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::<ProfileAttributes>(self.attributes.as_str())
.expect("Couldn't parse profile attributes").to_simple(),
}
}
}