jmap_client/push_subscription/
set.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
12use crate::{
13    core::set::{from_timestamp, SetObject},
14    email_submission::SetArguments,
15    Get, Set, DataType,
16};
17
18use super::{Keys, PushSubscription};
19
20impl PushSubscription<Set> {
21    pub fn device_client_id(&mut self, device_client_id: impl Into<String>) -> &mut Self {
22        self.device_client_id = Some(device_client_id.into());
23        self
24    }
25
26    pub fn url(&mut self, url: impl Into<String>) -> &mut Self {
27        self.url = Some(url.into());
28        self
29    }
30
31    pub fn verification_code(&mut self, verification_code: impl Into<String>) -> &mut Self {
32        self.verification_code = Some(verification_code.into());
33        self
34    }
35
36    pub fn keys(&mut self, keys: Keys) -> &mut Self {
37        self.keys = Some(keys);
38        self
39    }
40
41    pub fn expires(&mut self, expires: i64) -> &mut Self {
42        self.expires = Some(from_timestamp(expires));
43        self
44    }
45
46    pub fn types(&mut self, types: Option<impl IntoIterator<Item = DataType>>) -> &mut Self {
47        self.types = types.map(|s| s.into_iter().collect());
48        self
49    }
50}
51
52impl SetObject for PushSubscription<Set> {
53    type SetArguments = SetArguments;
54
55    fn new(_create_id: Option<usize>) -> Self {
56        PushSubscription {
57            _create_id,
58            _state: Default::default(),
59            id: None,
60            device_client_id: None,
61            url: None,
62            keys: None,
63            verification_code: None,
64            expires: None,
65            types: Vec::with_capacity(0).into(),
66        }
67    }
68
69    fn create_id(&self) -> Option<String> {
70        self._create_id.map(|id| format!("c{}", id))
71    }
72}
73
74impl SetObject for PushSubscription<Get> {
75    type SetArguments = SetArguments;
76
77    fn new(_create_id: Option<usize>) -> Self {
78        unimplemented!()
79    }
80
81    fn create_id(&self) -> Option<String> {
82        None
83    }
84}
85
86impl Keys {
87    pub fn new(p256dh: &[u8], auth: &[u8]) -> Self {
88        Keys {
89            p256dh: base64::encode_config(p256dh, base64::URL_SAFE),
90            auth: base64::encode_config(auth, base64::URL_SAFE),
91        }
92    }
93}