Dav Push: Logic to register subscriptions

This commit is contained in:
Lennart
2025-01-12 20:39:53 +01:00
parent 185eb8bddd
commit 5b418ec583
12 changed files with 261 additions and 15 deletions

View File

@@ -0,0 +1,7 @@
CREATE TABLE subscriptions (
id TEXT NOT NULL,
topic TEXT NOT NULL,
expiration DATETIME NOT NULL,
push_resource TEXT NOT NULL,
PRIMARY KEY (id)
);

View File

@@ -4,6 +4,7 @@ use sqlx::{sqlite::SqliteConnectOptions, Pool, Sqlite, SqlitePool};
pub mod addressbook_store;
pub mod calendar_store;
pub mod error;
pub mod subscription_store;
pub use error::Error;

View File

@@ -0,0 +1,51 @@
use crate::SqliteStore;
use async_trait::async_trait;
use rustical_store::{Error, Subscription, SubscriptionStore};
#[async_trait(?Send)]
impl SubscriptionStore for SqliteStore {
async fn get_subscriptions(&self, topic: &str) -> Result<Vec<Subscription>, Error> {
Ok(sqlx::query_as!(
Subscription,
r#"SELECT id, topic, expiration, push_resource
FROM subscriptions
WHERE (topic) = (?)"#,
topic
)
.fetch_all(&self.db)
.await
.map_err(crate::Error::from)?)
}
async fn get_subscription(&self, id: &str) -> Result<Subscription, Error> {
Ok(sqlx::query_as!(
Subscription,
r#"SELECT id, topic, expiration, push_resource
FROM subscriptions
WHERE (id) = (?)"#,
id
)
.fetch_one(&self.db)
.await
.map_err(crate::Error::from)?)
}
async fn upsert_subscription(&self, sub: Subscription) -> Result<bool, Error> {
sqlx::query!(
r#"INSERT OR REPLACE INTO subscriptions (id, topic, expiration, push_resource) VALUES (?, ?, ?, ?)"#,
sub.id,
sub.topic,
sub.expiration,
sub.push_resource
).execute(&self.db).await.map_err(crate::Error::from)?;
// TODO: Correctly return whether a subscription already existed
Ok(false)
}
async fn delete_subscription(&self, id: &str) -> Result<(), Error> {
sqlx::query!(r#"DELETE FROM subscriptions WHERE id = ? "#, id)
.execute(&self.db)
.await
.map_err(crate::Error::from)?;
Ok(())
}
}