|
|
use std::time::SystemTime;
use std::io::Write;
//use diesel::sql_types::Timestamp;
use diesel::{Insertable, Queryable, Identifiable, Associations, AsExpression, FromSqlRow};
//use bcrypt::{DEFAULT_COST, hash};
use diesel::pg::Pg;
use diesel::sql_types;
use diesel::deserialize::{self, FromSql};
use diesel::serialize::{self, ToSql, Output, IsNull};
use diesel::backend::Backend;
use libpso::character::settings;
use elseware::schema::*;
//const ELSEWHERE_COST: u32 = bcrypt::DEFAULT_COST;
const ELSEWHERE_COST: u32 = 5;
#[derive(Debug, AsExpression, FromSqlRow)]
#[sql_type="sql_types::Binary"]
pub struct EUserSettings(pub settings::UserSettings);
impl std::ops::Deref for EUserSettings {
type Target = settings::UserSettings;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Queryable, Identifiable, Debug)]
pub struct UserAccount {
pub id: i32,
pub username: String,
pub password: String,
pub guildcard: Option<i32>,
pub team_id: Option<i32>,
pub banned: bool,
pub muted_until: SystemTime,
pub created_at: SystemTime,
}
#[derive(Insertable)]
#[table_name="user_accounts"]
pub struct NewUser {
username: String,
password: String,
}
impl NewUser {
pub fn new(username: String, password: String) -> NewUser {
let crypt_password = bcrypt::hash(password, ELSEWHERE_COST).expect("could not hash password?");
NewUser {
username: username,
password: crypt_password,
}
}
}
#[derive(Queryable, Identifiable, Associations)]
#[belongs_to(UserAccount, foreign_key="user_id")]
#[table_name="user_settings"]
pub struct UserSettings {
pub id: i32,
pub user_id: i32,
//settings: Vec<u8>,
pub settings: EUserSettings,
}
#[derive(Insertable, Debug)]
#[table_name="user_settings"]
pub struct NewUserSettings {
pub user_id: i32,
pub settings: EUserSettings,
}
impl ToSql<sql_types::Binary, Pg> for EUserSettings {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
out.write_all(&self.0.as_bytes()[..])
.map(|_| IsNull::No)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
}
}
impl FromSql<sql_types::Binary, Pg> for EUserSettings {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
let bytes_vec: Vec<u8> = <Vec<u8> as FromSql<sql_types::Binary, Pg>>::from_sql(bytes)?;
let mut static_bytes = [0u8; 0x1160];
static_bytes[..0x1160].clone_from_slice(&bytes_vec);
Ok(EUserSettings(settings::UserSettings::from_bytes(static_bytes)))
}
}
|