Skip to main content

rustical_store/
subscription_store.rs

1use crate::Error;
2use async_trait::async_trait;
3use chrono::{DateTime, NaiveDateTime, TimeZone};
4
5pub struct Subscription {
6    pub id: String,
7    pub topic: String,
8    // Naive because sqlite has no concept of timezones
9    // In reality, this is UTC
10    pub expiration: NaiveDateTime,
11    pub push_resource: String,
12    pub public_key: String,
13    pub public_key_type: String,
14    pub auth_secret: String,
15}
16
17impl Subscription {
18    #[must_use]
19    pub fn is_expired(&self, now: &DateTime<impl TimeZone>) -> bool {
20        self.expiration < now.naive_utc()
21    }
22}
23
24#[async_trait]
25pub trait SubscriptionStore: Send + Sync + 'static {
26    async fn get_subscriptions(&self, topic: &str) -> Result<Vec<Subscription>, Error>;
27    async fn get_subscription(&self, id: &str) -> Result<Subscription, Error>;
28    /// Returns whether a subscription under the id already existed
29    async fn upsert_subscription(&self, sub: Subscription) -> Result<bool, Error>;
30    async fn delete_subscription(&self, id: &str) -> Result<(), Error>;
31}