push/internal/storage/
record.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use rusqlite::Row;
6
7use crate::error::Result;
8use crate::internal::crypto::KeyV1 as Key;
9
10use types::Timestamp;
11
12pub type ChannelID = String;
13
14#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct PushRecord {
16    /// Designation label provided by the subscribing service
17    pub channel_id: ChannelID,
18
19    /// Endpoint provided from the push server
20    pub endpoint: String,
21
22    /// The recipient (service worker)'s scope
23    pub scope: String,
24
25    /// Private EC Prime256v1 key info.
26    pub key: Vec<u8>,
27
28    /// Time this subscription was created.
29    pub ctime: Timestamp,
30
31    /// VAPID public key to restrict subscription updates for only those that sign
32    /// using the private VAPID key.
33    pub app_server_key: Option<String>,
34}
35
36impl PushRecord {
37    /// Create a Push Record from the Subscription info: endpoint, encryption
38    /// keys, etc.
39    pub fn new(chid: &str, endpoint: &str, scope: &str, key: Key) -> Result<Self> {
40        Ok(Self {
41            channel_id: chid.to_owned(),
42            endpoint: endpoint.to_owned(),
43            scope: scope.to_owned(),
44            key: key.serialize()?,
45            ctime: Timestamp::now(),
46            app_server_key: None,
47        })
48    }
49
50    pub(crate) fn from_row(row: &Row<'_>) -> Result<Self> {
51        Ok(PushRecord {
52            channel_id: row.get("channel_id")?,
53            endpoint: row.get("endpoint")?,
54            scope: row.get("scope")?,
55            key: row.get("key")?,
56            ctime: row.get("ctime")?,
57            app_server_key: row.get("app_server_key")?,
58        })
59    }
60}