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/. */
45use rusqlite::Row;
67use crate::error::Result;
8use crate::internal::crypto::KeyV1 as Key;
910use types::Timestamp;
1112pub type ChannelID = String;
1314#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct PushRecord {
16/// Designation label provided by the subscribing service
17pub channel_id: ChannelID,
1819/// Endpoint provided from the push server
20pub endpoint: String,
2122/// The recipient (service worker)'s scope
23pub scope: String,
2425/// Private EC Prime256v1 key info.
26pub key: Vec<u8>,
2728/// Time this subscription was created.
29pub ctime: Timestamp,
3031/// VAPID public key to restrict subscription updates for only those that sign
32 /// using the private VAPID key.
33pub app_server_key: Option<String>,
34}
3536impl PushRecord {
37/// Create a Push Record from the Subscription info: endpoint, encryption
38 /// keys, etc.
39pub fn new(chid: &str, endpoint: &str, scope: &str, key: Key) -> Result<Self> {
40Ok(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 }
4950pub(crate) fn from_row(row: &Row<'_>) -> Result<Self> {
51Ok(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}