summaryrefslogtreecommitdiff
path: root/src/push/repo.rs
blob: 56bbc3defb07aa2d94c5d4751d42e287afc060f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use sqlx::{SqliteConnection, Transaction, sqlite::Sqlite};
use web_push::SubscriptionInfo;

use super::Id;
use crate::user::{self, User};

pub trait Provider {
    fn subscriptions(&mut self) -> Subscriptions;
}

impl Provider for Transaction<'_, Sqlite> {
    fn subscriptions(&mut self) -> Subscriptions {
        Subscriptions(self)
    }
}

pub struct Subscriptions<'t>(&'t mut SqliteConnection);

impl Subscriptions<'_> {
    pub async fn create(
        &mut self,
        user: &User,
        info: &SubscriptionInfo,
    ) -> Result<Id, sqlx::Error> {
        let id = Id::generate();

        sqlx::query!(
            r#"
                insert into subscription (id, user, endpoint, key_p256dh, key_auth)
                values ($1, $2, $3, $4, $5)
            "#,
            id,
            user.id,
            info.endpoint,
            info.keys.p256dh,
            info.keys.auth,
        )
        .execute(&mut *self.0)
        .await?;

        Ok(id)
    }

    pub async fn all(&mut self) -> Result<Vec<Subscription>, sqlx::Error> {
        let subscriptions = sqlx::query!(
            r#"
                select
                    id as "id: Id",
                    user as "user: user::Id",
                    endpoint,
                    key_p256dh,
                    key_auth
                from subscription
            "#
        )
        .map(|row| Subscription {
            id: row.id,
            user: row.user,
            info: SubscriptionInfo::new(row.endpoint, row.key_p256dh, row.key_auth),
        })
        .fetch_all(&mut *self.0)
        .await?;

        Ok(subscriptions)
    }

    pub async fn by_endpoint(&mut self, endpoint: &String) -> Result<Subscription, sqlx::Error> {
        let subscription = sqlx::query!(
            r#"
                select
                    id as "id: Id",
                    user as "user: user::Id",
                    endpoint,
                    key_p256dh,
                    key_auth
                from subscription
                where endpoint = $1
            "#,
            endpoint,
        )
        .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(subscription)
    }

    pub async fn delete(&mut self, subscription: &Subscription) -> Result<(), sqlx::Error> {
        sqlx::query!(
            r#"
                delete from subscription
                where endpoint = $1
            "#,
            subscription.info.endpoint,
        )
        .execute(&mut *self.0)
        .await?;
        Ok(())
    }
}

pub struct Subscription {
    pub id: Id,
    pub user: user::Id,
    pub info: SubscriptionInfo,
}