aboutsummaryrefslogtreecommitdiff
path: root/src/database/types.rs
blob: 0d89adb63a916dec5ffb5f486242da0c63178463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use serde::Serialize;
use std::time::SystemTime;

#[derive(Serialize, Clone, Debug)]
pub struct Date {
    date: u64,
}
impl Date {
    pub fn now() -> Date {
        Date{
            date: SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()
        }
    }
    pub fn new(s: i64) -> Date {
        Date {
            date: s as u64,
        }
    }
}

#[derive(Serialize, Clone, Debug)]
pub struct User {
    username: String,
    id: usize
}
impl User {
    pub fn new(name: String, id: usize) -> User {
        User{
            username: name,
            id: id
        }
    }
}

#[derive(Serialize, Clone, Debug)]
pub struct Message {
    date: Date,
    message: String,
    sender: User,
    id: usize,
    reply_to: Option<usize>, // Message ID
    deleted: bool,
}
impl Message {
    pub fn new(msg: String, sender: User, id: usize, reply_to: Option<usize>) -> Message {
        Message {
            date: Date::now(),
            sender: sender,
            message: msg,
            id: id,
            reply_to: reply_to,
            deleted: false,
        }
    }

    pub fn construct(msg: String, sender: User, id: usize, reply_to: Option<usize>, date: Date, deleted: bool) -> Message {
        Message {
            date: date,
            sender: sender,
            message: msg,
            id: id,
            reply_to: reply_to,
            deleted: deleted,
        }
    }

    pub fn id(&self) -> usize {
        self.id
    }

    pub fn date_as_i64(&self) -> i64 {
        self.date.date as i64
    }
    pub fn user_id(&self) -> i64 {
        self.sender.id as i64
    }
    pub fn message(&self) -> String {
        self.message.clone()
    }
    pub fn reply_to(&self) -> Option<i64> {
        match self.reply_to {
            Some(n) => Some(n as i64),
            None => None
        }
    }
}

#[derive(Serialize)]
pub struct Info {
    name: String,
    version: &'static str,
    users: usize,
}
impl Info {
    pub fn get(db: std::sync::MutexGuard<crate::database::Database>) -> Info {
        Info{
            name: String::from("Testing"),
            version: option_env!("CARGO_PKG_VERSION").unwrap(),
            users: db.get_user_count(),
        }
    }
}