use utils::{WalletAddress, Amount, Address};
use std::collections::{btree_set, BTreeSet, hash_map, HashMap};
use chrono::{DateTime, UTC, NaiveDate};
use dto::{UserDTO, FromDTO, FromDTOError};
use std::result::Result as StdResult;
#[derive(Clone, Debug)]
pub struct User {
id: u64,
username: String,
displayname: String,
email: (String, bool),
first: Option<(String, bool)>,
last: Option<(String, bool)>,
device_count: u8,
wallet_addresses: BTreeSet<WalletAddress>,
checking_balance: Amount,
cold_balance: Amount,
bonds: HashMap<DateTime<UTC>, u64>,
birthday: Option<(NaiveDate, bool)>,
phone: Option<(String, bool)>,
image: Option<String>,
address: Option<(Address, bool)>,
sybil_score: i8,
trust_score: i8,
enabled: bool,
registered: DateTime<UTC>,
last_activity: DateTime<UTC>,
banned: Option<DateTime<UTC>>,
}
impl User {
pub fn get_id(&self) -> u64 {
self.id
}
pub fn get_username(&self) -> &str {
&self.username
}
pub fn get_displayname(&self) -> &str {
&self.displayname
}
pub fn get_email(&self) -> &str {
&self.email.0
}
pub fn is_email_confirmed(&self) -> bool {
self.email.1
}
pub fn get_first_name(&self) -> Option<&str> {
match self.first {
Some((ref f, _c)) => Some(f),
None => None,
}
}
pub fn is_first_name_confirmed(&self) -> bool {
match self.first {
Some((_, c)) => c,
None => false,
}
}
pub fn get_last_name(&self) -> Option<&str> {
match self.last {
Some((ref l, _c)) => Some(l),
None => None,
}
}
pub fn is_last_name_confirmed(&self) -> bool {
match self.last {
Some((_, c)) => c,
None => false,
}
}
pub fn get_device_count(&self) -> u8 {
self.device_count
}
pub fn wallet_addresses(&self) -> btree_set::Iter<WalletAddress> {
self.wallet_addresses.iter()
}
pub fn get_checking_balance(&self) -> Amount {
self.checking_balance
}
pub fn get_cold_balance(&self) -> Amount {
self.cold_balance
}
pub fn bonds(&self) -> hash_map::Iter<DateTime<UTC>, u64> {
self.bonds.iter()
}
pub fn get_birthday(&self) -> Option<NaiveDate> {
match self.birthday {
Some((b, _c)) => Some(b),
None => None,
}
}
pub fn is_birthday_confirmed(&self) -> bool {
match self.birthday {
Some((_b, c)) => c,
None => false,
}
}
pub fn get_phone(&self) -> Option<&str> {
match self.phone {
Some((ref p, _c)) => Some(p),
None => None,
}
}
pub fn is_phone_confirmed(&self) -> bool {
match self.phone {
Some((_, c)) => c,
None => false,
}
}
pub fn get_image(&self) -> Option<&str> {
match self.image {
Some(ref i) => Some(i),
None => None,
}
}
pub fn get_address(&self) -> Option<&Address> {
match self.address {
Some((ref a, _c)) => Some(&a),
None => None,
}
}
pub fn is_address_confirmed(&self) -> bool {
match self.address {
Some((_, c)) => c,
None => false,
}
}
pub fn get_sybil_score(&self) -> i8 {
self.sybil_score
}
pub fn get_trust_score(&self) -> i8 {
self.trust_score
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn get_registration_time(&self) -> DateTime<UTC> {
self.registered
}
pub fn get_last_activity(&self) -> DateTime<UTC> {
self.last_activity
}
pub fn is_banned(&self) -> bool {
self.banned.is_none() || self.banned.unwrap() > UTC::now()
}
pub fn ban_expiration(&self) -> Option<DateTime<UTC>> {
self.banned
}
}
impl FromDTO<UserDTO> for User {
fn from_dto(dto: UserDTO) -> StdResult<User, FromDTOError> {
let first_opt = match dto.first {
Some(first) => Some((first, dto.first_confirmed)),
None => None,
};
let last_opt = match dto.last {
Some(last) => Some((last, dto.last_confirmed)),
None => None,
};
let birthday_opt = match dto.birthday {
Some(birthday) => Some((birthday, dto.birthday_confirmed)),
None => None,
};
let phone_opt = match dto.phone {
Some(phone) => Some((phone, dto.phone_confirmed)),
None => None,
};
let adress_opt = match dto.address {
Some(address) => Some((address, dto.address_confirmed)),
None => None,
};
Ok(User {
id: dto.id,
username: dto.username,
displayname: dto.displayname,
email: (dto.email, dto.email_confirmed),
first: first_opt,
last: last_opt,
device_count: dto.device_count,
wallet_addresses: dto.wallet_addresses,
checking_balance: dto.checking_balance,
cold_balance: dto.cold_balance,
bonds: dto.bonds,
birthday: birthday_opt,
phone: phone_opt,
image: dto.image,
address: adress_opt,
sybil_score: dto.sybil_score,
trust_score: dto.trust_score,
enabled: dto.enabled,
registered: dto.registered,
last_activity: dto.last_activity,
banned: dto.banned,
})
}
}
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct Transaction {
id: u64,
origin_user: u64,
destination_user: u64,
destination: WalletAddress,
amount: Amount,
timestamp: DateTime<UTC>,
}
impl Transaction {
pub fn get_id(&self) -> u64 {
self.id
}
pub fn get_destination_user(&self) -> u64 {
self.destination_user
}
pub fn get_destination(&self) -> &WalletAddress {
&self.destination
}
pub fn get_origin_user(&self) -> u64 {
self.origin_user
}
pub fn get_amount(&self) -> Amount {
self.amount
}
pub fn get_timestamp(&self) -> &DateTime<UTC> {
&self.timestamp
}
}