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
//! Session

use crate::HttpClient;

use std::sync::atomic::{AtomicBool, Ordering};

use async_std::sync::Mutex;

/// The struct of the current session of the bot.
pub struct Session {
    id: Mutex<String>,
    pub http: HttpClient,

    #[allow(dead_code)]
    pub(crate) state: (), // Maybe add a "global" state in the future

    is_resumable: AtomicBool,
}

impl Session {
    pub(crate) fn new(token: String) -> Self {
        Session {
            id: Mutex::new("".into()),
            http: HttpClient::new(token),
            state: (),
            is_resumable: AtomicBool::new(true),
        }
    }

    /// Set the value to resumable field
    pub(crate) fn set_resumable(&self, b: bool) {
        self.is_resumable.store(b, Ordering::Relaxed);
    }

    /// Get the value of resumable field
    pub(crate) fn is_resumable(&self) -> bool {
        self.is_resumable.load(Ordering::Relaxed)
    }

    /// Set the value to id field
    pub(crate) async fn set_id(&self, id: String) {
        let mut session_id = self.id.lock().await;
        *session_id = id;
    }

    /// Get the value to id field
    pub(crate) async fn id(&self) -> String {
        let session_id = self.id.lock().await;
        session_id.clone()
    }
}