session.rs

 1use db::kvp::KEY_VALUE_STORE;
 2use util::ResultExt;
 3use uuid::Uuid;
 4
 5#[derive(Clone, Debug)]
 6pub struct Session {
 7    session_id: String,
 8    old_session_id: Option<String>,
 9}
10
11impl Session {
12    pub async fn new() -> Self {
13        let key_name = "session_id".to_string();
14
15        let old_session_id = KEY_VALUE_STORE.read_kvp(&key_name).ok().flatten();
16
17        let session_id = Uuid::new_v4().to_string();
18
19        KEY_VALUE_STORE
20            .write_kvp(key_name, session_id.clone())
21            .await
22            .log_err();
23
24        Self {
25            session_id,
26            old_session_id,
27        }
28    }
29
30    #[cfg(any(test, feature = "test-support"))]
31    pub fn test() -> Self {
32        Self {
33            session_id: Uuid::new_v4().to_string(),
34            old_session_id: None,
35        }
36    }
37
38    pub fn id(&self) -> &str {
39        &self.session_id
40    }
41    pub fn last_session_id(&self) -> Option<&str> {
42        self.old_session_id.as_deref()
43    }
44}