jmap_client/event_source/
mod.rs

1/*
2 * Copyright Stalwart Labs LLC See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 * option. This file may not be copied, modified, or distributed
9 * except according to those terms.
10 */
11
12pub mod parser;
13pub mod stream;
14
15use crate::{core::session::URLParser, CalendarAlert, DataType};
16use ahash::AHashMap;
17use serde::{Deserialize, Serialize};
18
19pub enum URLParameter {
20    Types,
21    CloseAfter,
22    Ping,
23}
24
25impl URLParser for URLParameter {
26    fn parse(value: &str) -> Option<Self> {
27        match value {
28            "types" => Some(URLParameter::Types),
29            "closeafter" => Some(URLParameter::CloseAfter),
30            "ping" => Some(URLParameter::Ping),
31            _ => None,
32        }
33    }
34}
35
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub enum PushNotification {
38    StateChange(Changes),
39    CalendarAlert(CalendarAlert),
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43pub struct Changes {
44    id: Option<String>,
45    changes: AHashMap<String, AHashMap<DataType, String>>,
46}
47
48impl Changes {
49    pub fn new(id: Option<String>, changes: AHashMap<String, AHashMap<DataType, String>>) -> Self {
50        Self { id, changes }
51    }
52
53    pub fn id(&self) -> Option<&str> {
54        self.id.as_deref()
55    }
56
57    pub fn account_changes(&mut self, account_id: &str) -> Option<AHashMap<DataType, String>> {
58        self.changes.remove(account_id)
59    }
60
61    pub fn changed_accounts(&self) -> impl Iterator<Item = &String> {
62        self.changes.keys()
63    }
64
65    pub fn changes(&self, account_id: &str) -> Option<impl Iterator<Item = (&DataType, &String)>> {
66        self.changes.get(account_id).map(|changes| changes.iter())
67    }
68
69    pub fn has_type(&self, type_: DataType) -> bool {
70        self.changes
71            .values()
72            .any(|changes| changes.contains_key(&type_))
73    }
74
75    pub fn into_inner(self) -> AHashMap<String, AHashMap<DataType, String>> {
76        self.changes
77    }
78
79    pub fn is_empty(&self) -> bool {
80        !self.changes.values().any(|changes| !changes.is_empty())
81    }
82}