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, pub team_id: Option, 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, pub settings: EUserSettings, } #[derive(Insertable, Debug)] #[table_name="user_settings"] pub struct NewUserSettings { pub user_id: i32, pub settings: EUserSettings, } impl ToSql for EUserSettings { fn to_sql(&self, out: &mut Output) -> serialize::Result { out.write_all(&self.0.as_bytes()[..]) .map(|_| IsNull::No) .map_err(|e| Box::new(e) as Box) } } impl FromSql for EUserSettings { fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { let bytes_vec: Vec = as FromSql>::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))) } }