1pub const KEY_PREFIX: &str = "cal";
5
6pub const KEY_SEPARATOR: &str = ":";
8
9pub const JAMBONZ_KEY: &str = "cal:jambonz";
14
15pub const REGIONS_KEY: &str = "cal:regions";
18
19pub const REGION_IDENTS_KEY: &str = "cal:region:idents";
22
23pub const PROXIES_KEY: &str = "cal:proxies";
26
27pub const WS_CONNECTIONS_KEY: &str = "cal:ws:connections";
30
31pub const GLOBAL_EVENTS_CHANNEL: &str = "cal:events";
34
35pub const ACCOUNTS_KEY: &str = "cal:accounts";
40
41pub const ACCOUNT_IDENTS_KEY: &str = "cal:account:idents";
44
45pub const USERS_KEY: &str = "cal:users";
48
49pub const USER_IDENTS_KEY: &str = "cal:user:idents";
52
53#[inline]
57pub fn build_key(components: &[&str]) -> String {
58 let mut key = String::with_capacity(128);
59 key.push_str(KEY_PREFIX);
60 for component in components {
61 key.push_str(KEY_SEPARATOR);
62 key.push_str(component);
63 }
64 key
65}
66
67#[inline]
69pub fn build_account_key(account_id: &str, components: &[&str]) -> String {
70 let mut parts = vec!["account", account_id];
71 parts.extend_from_slice(components);
72 build_key(&parts)
73}
74
75#[inline]
77pub fn build_trunk_key(trunk_ip: &str) -> String {
78 build_key(&["trunk", trunk_ip])
79}
80
81pub struct AccountKeys;
84
85impl AccountKeys {
86 pub fn devices(account_id: &str) -> String {
89 build_account_key(account_id, &["devices"])
90 }
91
92 pub fn device_idents(account_id: &str) -> String {
95 build_account_key(account_id, &["device", "idents"])
96 }
97
98 pub fn ddis(account_id: &str) -> String {
101 build_account_key(account_id, &["ddis"])
102 }
103
104 pub fn trunks(account_id: &str) -> String {
107 build_account_key(account_id, &["trunks"])
108 }
109
110 pub fn hooks(account_id: &str) -> String {
113 build_account_key(account_id, &["hooks"])
114 }
115
116 pub fn assets(account_id: &str) -> String {
119 build_account_key(account_id, &["assets"])
120 }
121
122 pub fn addresses(account_id: &str) -> String {
125 build_account_key(account_id, &["addresses"])
126 }
127
128 pub fn contacts(account_id: &str) -> String {
131 build_account_key(account_id, &["contacts"])
132 }
133
134 pub fn contact_idents(account_id: &str) -> String {
137 build_account_key(account_id, &["contact", "idents"])
138 }
139}
140
141pub struct AgentKeys;
144
145impl AgentKeys {
146 pub fn status(account_id: &str, user_id: &str) -> String {
149 build_account_key(account_id, &["agent", "status", user_id])
150 }
151
152 pub fn by_user(account_id: &str) -> String {
155 build_account_key(account_id, &["agents", "by_user"])
156 }
157
158 pub fn registered(account_id: &str) -> String {
161 build_account_key(account_id, &["agents", "registered"])
162 }
163
164 pub fn available(account_id: &str) -> String {
167 build_account_key(account_id, &["agents", "available"])
168 }
169
170 pub fn connected(account_id: &str) -> String {
173 build_account_key(account_id, &["agents", "connected"])
174 }
175}
176
177pub struct SessionKeys;
180
181impl SessionKeys {
182 pub fn session(account_id: &str, session_id: &str) -> String {
185 build_account_key(account_id, &["session", session_id])
186 }
187
188 pub fn active(account_id: &str) -> String {
191 build_account_key(account_id, &["sessions", "active"])
192 }
193}
194
195pub struct QueueKeys;
198
199impl QueueKeys {
200 pub fn queue(account_id: &str, queue_name: &str) -> String {
203 build_account_key(account_id, &["queue", queue_name])
204 }
205
206 pub fn metadata(account_id: &str) -> String {
209 build_account_key(account_id, &["queues", "meta"])
210 }
211
212 pub fn active(account_id: &str) -> String {
215 build_account_key(account_id, &["queues", "active"])
216 }
217}
218
219pub struct ConversationKeys;
222
223impl ConversationKeys {
224 pub fn conversation(account_id: &str, conversation_id: &str) -> String {
227 build_account_key(account_id, &["conversation", conversation_id])
228 }
229
230 pub fn active(account_id: &str) -> String {
233 build_account_key(account_id, &["conversations", "active"])
234 }
235
236 pub fn by_phone(account_id: &str) -> String {
239 build_account_key(account_id, &["conversations", "by_phone"])
240 }
241}
242
243pub struct ChannelKeys;
246
247impl ChannelKeys {
248 pub fn account_events(account_id: &str) -> String {
251 build_account_key(account_id, &["events"])
252 }
253
254 pub fn agent_events(account_id: &str, user_id: &str) -> String {
257 build_account_key(account_id, &["agent", user_id, "events"])
258 }
259}
260
261pub fn extract_account_id(key: &str) -> Option<&str> {
266 let parts: Vec<&str> = key.split(KEY_SEPARATOR).collect();
267 if parts.len() >= 3 && parts[0] == KEY_PREFIX && parts[1] == "account" {
268 Some(parts[2])
269 } else {
270 None
271 }
272}
273
274#[cfg(test)]
275mod tests {
276 use super::*;
277
278 #[test]
279 fn test_build_key() {
280 assert_eq!(build_key(&["test", "key"]), "cal:test:key");
281 }
282
283 #[test]
284 fn test_build_account_key() {
285 assert_eq!(
286 build_account_key("123", &["devices"]),
287 "cal:account:123:devices"
288 );
289 }
290
291 #[test]
292 fn test_extract_account_id() {
293 assert_eq!(
294 extract_account_id("cal:account:123:devices"),
295 Some("123")
296 );
297 assert_eq!(
298 extract_account_id("cal:regions"),
299 None
300 );
301 }
302}