Skip to main content

server_watchdog/domain/
chat.rs

1use std::collections::HashMap;
2use derive_new::new;
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6#[derive(Serialize, Deserialize, new, Clone)]
7pub struct ChatList {
8    #[new(default)]
9    pub chats: Vec<Chat>
10}
11
12#[derive(Serialize, Deserialize, Clone)]
13pub struct Chat {
14    pub id: String,
15    pub client_name: String,
16    pub identity: String
17}
18
19impl Chat {
20    pub fn new(client_name: String, identity: String) -> Self {
21        Self {
22            id: Uuid::new_v4().to_string(),
23            client_name,
24            identity
25        }
26    }
27}
28
29pub struct ChatMap {
30    chats: HashMap<(String, String), Chat>
31}
32
33impl ChatMap {
34
35    pub fn get_id(&self, client_name: &str, identity: &str) -> Option<&str> {
36        let chat = self.chats.get(&(client_name.to_string(), identity.to_string()))?;
37        Some(chat.id.as_str())
38    }
39
40    pub fn contains(&self, client_name: &str, identity: &str) -> bool {
41        self.chats.contains_key(&(client_name.to_string(), identity.to_string()))
42    }
43
44    pub fn from(chat_list: ChatList) -> Self {
45        let mut chats = HashMap::new();
46        for chat in chat_list.chats.into_iter() {
47            chats.insert((chat.client_name.clone(), chat.identity.clone()), chat);
48        }
49
50        Self {
51            chats
52        }
53    }
54}