jmap_client/push_subscription/
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 get;
13pub mod helpers;
14pub mod set;
15
16use std::fmt::Display;
17
18use chrono::{DateTime, Utc};
19use serde::{Deserialize, Serialize};
20
21use crate::core::changes::ChangesObject;
22use crate::core::set::list_not_set;
23use crate::core::Object;
24use crate::{Get, Set, DataType};
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct PushSubscription<State = Get> {
28    #[serde(skip)]
29    _create_id: Option<usize>,
30
31    #[serde(skip)]
32    _state: std::marker::PhantomData<State>,
33
34    #[serde(rename = "id")]
35    #[serde(skip_serializing_if = "Option::is_none")]
36    id: Option<String>,
37
38    #[serde(rename = "deviceClientId")]
39    #[serde(skip_serializing_if = "Option::is_none")]
40    device_client_id: Option<String>,
41
42    #[serde(rename = "url")]
43    #[serde(skip_serializing_if = "Option::is_none")]
44    url: Option<String>,
45
46    #[serde(rename = "keys")]
47    #[serde(skip_serializing_if = "Option::is_none")]
48    keys: Option<Keys>,
49
50    #[serde(rename = "verificationCode")]
51    #[serde(skip_serializing_if = "Option::is_none")]
52    verification_code: Option<String>,
53
54    #[serde(rename = "expires")]
55    #[serde(skip_serializing_if = "Option::is_none")]
56    expires: Option<DateTime<Utc>>,
57
58    #[serde(rename = "types")]
59    #[serde(skip_serializing_if = "list_not_set")]
60    types: Option<Vec<DataType>>,
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Copy)]
64pub enum Property {
65    #[serde(rename = "id")]
66    Id,
67    #[serde(rename = "deviceClientId")]
68    DeviceClientId,
69    #[serde(rename = "url")]
70    Url,
71    #[serde(rename = "keys")]
72    Keys,
73    #[serde(rename = "verificationCode")]
74    VerificationCode,
75    #[serde(rename = "expires")]
76    Expires,
77    #[serde(rename = "types")]
78    Types,
79}
80
81impl Display for Property {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        match self {
84            Property::Id => write!(f, "id"),
85            Property::DeviceClientId => write!(f, "deviceClientId"),
86            Property::Url => write!(f, "url"),
87            Property::Keys => write!(f, "keys"),
88            Property::VerificationCode => write!(f, "verificationCode"),
89            Property::Expires => write!(f, "expires"),
90            Property::Types => write!(f, "types"),
91        }
92    }
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct Keys {
97    p256dh: String,
98    auth: String,
99}
100
101impl Object for PushSubscription<Set> {
102    type Property = Property;
103
104    fn requires_account_id() -> bool {
105        false
106    }
107}
108
109impl Object for PushSubscription<Get> {
110    type Property = Property;
111
112    fn requires_account_id() -> bool {
113        false
114    }
115}
116
117impl ChangesObject for PushSubscription<Set> {
118    type ChangesResponse = ();
119}
120
121impl ChangesObject for PushSubscription<Get> {
122    type ChangesResponse = ();
123}