Debian Codegraph @

webpush/store_postgres.go

raw
1 package webpush
2
3 import (
4 "heckel.io/ntfy/v2/db"
5 "heckel.io/ntfy/v2/db/schema"
6 )
7
8 const (
9 postgresSelectSubscriptionIDByEndpointQuery = `SELECT id FROM webpush_subscription WHERE endpoint = $1`
10 postgresSelectSubscriptionCountBySubscriberIPQuery = `SELECT COUNT(*) FROM webpush_subscription WHERE subscriber_ip = $1`
11 postgresSelectSubscriptionsForTopicQuery = `
12 SELECT s.id, s.endpoint, s.key_auth, s.key_p256dh, s.user_id
13 FROM webpush_subscription_topic st
14 JOIN webpush_subscription s ON s.id = st.subscription_id
15 WHERE st.topic = $1
16 ORDER BY s.endpoint
17 `
18 postgresSelectSubscriptionsExpiringSoonQuery = `
19 SELECT id, endpoint, key_auth, key_p256dh, user_id
20 FROM webpush_subscription
21 WHERE warned_at = 0 AND updated_at <= $1
22 `
23 postgresUpsertSubscriptionQuery = `
24 INSERT INTO webpush_subscription (id, endpoint, key_auth, key_p256dh, user_id, subscriber_ip, updated_at, warned_at)
25 VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
26 ON CONFLICT (endpoint)
27 DO UPDATE SET key_auth = excluded.key_auth, key_p256dh = excluded.key_p256dh, user_id = excluded.user_id, subscriber_ip = excluded.subscriber_ip, updated_at = excluded.updated_at, warned_at = excluded.warned_at
28 RETURNING id
29 `
30 postgresUpdateSubscriptionWarningSentQuery = `UPDATE webpush_subscription SET warned_at = $1 WHERE id = $2`
31 postgresUpdateSubscriptionUpdatedAtQuery = `UPDATE webpush_subscription SET updated_at = $1 WHERE endpoint = $2`
32 postgresDeleteSubscriptionByEndpointQuery = `DELETE FROM webpush_subscription WHERE endpoint = $1`
33 postgresDeleteSubscriptionByUserIDQuery = `DELETE FROM webpush_subscription WHERE user_id = $1`
34 postgresDeleteSubscriptionByAgeQuery = `DELETE FROM webpush_subscription WHERE updated_at <= $1`
35
36 postgresInsertSubscriptionTopicQuery = `INSERT INTO webpush_subscription_topic (subscription_id, topic) VALUES ($1, $2)`
37 postgresDeleteSubscriptionTopicAllQuery = `DELETE FROM webpush_subscription_topic WHERE subscription_id = $1`
38 postgresDeleteSubscriptionTopicWithoutSubscriptionQuery = `DELETE FROM webpush_subscription_topic WHERE subscription_id NOT IN (SELECT id FROM webpush_subscription)`
39 )
40
41 // Schema version and queries
42 const (
43 postgresCurrentSchemaVersion = 1
44 )
45
46 var (
47 postgresCreateTables = schema.AsMigrateFunc(`
48 CREATE TABLE IF NOT EXISTS webpush_subscription (
49 id TEXT PRIMARY KEY,
50 endpoint TEXT NOT NULL UNIQUE,
51 key_auth TEXT NOT NULL,
52 key_p256dh TEXT NOT NULL,
53 user_id TEXT NOT NULL,
54 subscriber_ip TEXT NOT NULL,
55 updated_at BIGINT NOT NULL,
56 warned_at BIGINT NOT NULL DEFAULT 0
57 );
58 CREATE INDEX IF NOT EXISTS idx_webpush_subscriber_ip ON webpush_subscription (subscriber_ip);
59 CREATE INDEX IF NOT EXISTS idx_webpush_updated_at ON webpush_subscription (updated_at);
60 CREATE INDEX IF NOT EXISTS idx_webpush_user_id ON webpush_subscription (user_id);
61 CREATE TABLE IF NOT EXISTS webpush_subscription_topic (
62 subscription_id TEXT NOT NULL REFERENCES webpush_subscription (id) ON DELETE CASCADE,
63 topic TEXT NOT NULL,
64 PRIMARY KEY (subscription_id, topic)
65 );
66 CREATE INDEX IF NOT EXISTS idx_webpush_topic ON webpush_subscription_topic (topic);
67 `)
68 )
69
70 // NewPostgresStore creates a new PostgreSQL-backed web push store using an existing database connection pool.
71 func NewPostgresStore(d *db.DB) (*Store, error) {
72 if err := schema.Migrate(d.Primary(), schema.Postgres, schemaStore, postgresCurrentSchemaVersion, postgresCreateTables, nil); err != nil {
73 return nil, err
74 }
75 return &Store{
76 db: d,
77 queries: queries{
78 selectSubscriptionIDByEndpoint: postgresSelectSubscriptionIDByEndpointQuery,
79 selectSubscriptionCountBySubscriberIP: postgresSelectSubscriptionCountBySubscriberIPQuery,
80 selectSubscriptionsForTopic: postgresSelectSubscriptionsForTopicQuery,
81 selectSubscriptionsExpiringSoon: postgresSelectSubscriptionsExpiringSoonQuery,
82 upsertSubscription: postgresUpsertSubscriptionQuery,
83 updateSubscriptionWarningSent: postgresUpdateSubscriptionWarningSentQuery,
84 updateSubscriptionUpdatedAt: postgresUpdateSubscriptionUpdatedAtQuery,
85 deleteSubscriptionByEndpoint: postgresDeleteSubscriptionByEndpointQuery,
86 deleteSubscriptionByUserID: postgresDeleteSubscriptionByUserIDQuery,
87 deleteSubscriptionByAge: postgresDeleteSubscriptionByAgeQuery,
88 insertSubscriptionTopic: postgresInsertSubscriptionTopicQuery,
89 deleteSubscriptionTopicAll: postgresDeleteSubscriptionTopicAllQuery,
90 deleteSubscriptionTopicWithoutSubscription: postgresDeleteSubscriptionTopicWithoutSubscriptionQuery,
91 },
92 }, nil
93 }