diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-07-24 22:32:27 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-07-24 22:32:27 -0400 |
| commit | b63380b251d04dd92f06aa5bbc22a72ca3e4bf8e (patch) | |
| tree | 3956ec457131ce049fd91f2f53309bc0620fffe2 /src/push/repo.rs | |
| parent | 2e42057694851b82574e0a406ded429fb95a07fa (diff) | |
wip: 83B78D40-D7CB-4419-9FE7-E7D858909443
Diffstat (limited to 'src/push/repo.rs')
| -rw-r--r-- | src/push/repo.rs | 81 |
1 files changed, 37 insertions, 44 deletions
diff --git a/src/push/repo.rs b/src/push/repo.rs index 2d492ea..ddef706 100644 --- a/src/push/repo.rs +++ b/src/push/repo.rs @@ -1,9 +1,8 @@ use sqlx::{SqliteConnection, Transaction, sqlite::Sqlite}; +use web_push::SubscriptionInfo; -use super::{Subscription, Id}; -use crate::{ - user::{self, User}, -} +use super::Id; +use crate::user::{self, User}; pub trait Provider { fn subscriptions(&mut self) -> Subscriptions; @@ -21,74 +20,68 @@ impl Subscriptions<'_> { pub async fn create( &mut self, user: &User, - endpoint: &String, - key_p256dh: &String, - key_auth: &String, - expiration_time: &String, - ) -> Result<Subscription, sqlx::Error> { + info: &SubscriptionInfo, + ) -> Result<Id, sqlx::Error> { let id = Id::generate(); - let subscription = sqlx::query!( + sqlx::query!( r#" - insert into subscription - (id, user, endpoint, key_p256dh, key_auth, expiration_time) - values ($1, $2, $3, $4, $5, $6) - returning - id as "id: Id", - user as "user: user::Id", - endpoint as "endpoint: String", - key_p256dh as "key_p256dh: String", - key_auth as "key_auth: String", - expiration_time as "expiration_time: String" + insert into subscription (id, user, endpoint, key_p256dh, key_auth) + values ($1, $2, $3, $4, $5) "#, id, user.id, - endpoint, - key_p256dh, - key_auth, - expiration_time, + info.endpoint, + info.keys.p256dh, + info.keys.auth, ) - .fetch_one(&mut *self.0) + .execute(&mut *self.0) .await?; - Ok(subscription) + Ok(id) } - pub async fn for_user(&mut self, user: &User) -> Result<vec<Subscription>, sqlx::Error> { - let subscriptions = sqlx::query!( + pub async fn by_id(&mut self, id: &Id) -> Result<Subscription, sqlx::Error> { + let subscription = sqlx::query!( r#" select id as "id: Id", user as "user: user::Id", - endpoint as "endpoint: String", - key_p256dh as "key_p256dh: String", - key_auth as "key_auth: String", + endpoint, + key_p256dh, + key_auth from subscription - where user = $1 + where id = $1 "#, - user.id, + id, ) - .fetch_all(&mut *self.0) + .map(|row| Subscription { + id: row.id, + user: row.user, + info: SubscriptionInfo::new(row.endpoint, row.key_p256dh, row.key_auth), + }) + .fetch_one(&mut *self.0) .await?; - Ok(subscriptions) + Ok(subscription) } - pub async fn delete( - &mut self, - subscription: &Subscription, - deleted: &Instant, - ) -> Result<(), sqlx::Error> { - let id = subscription.id(); - + pub async fn delete(&mut self, subscription: &Subscription) -> Result<(), sqlx::Error> { sqlx::query!( r#" - delete from subscription where id = $1 + delete from subscription + where id = $1 "#, - id, + subscription.id, ) .execute(&mut *self.0) .await?; Ok(()) } } + +pub struct Subscription { + pub id: Id, + pub user: user::Id, + pub info: SubscriptionInfo, +} |
