2023-05-24 21:36:01 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3" // SQLite driver
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
createWebPushSubscriptionsTableQuery = `
|
|
|
|
BEGIN;
|
2023-05-30 19:50:24 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS subscriptions (
|
2023-05-24 21:36:01 +02:00
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
topic TEXT NOT NULL,
|
|
|
|
username TEXT,
|
|
|
|
endpoint TEXT NOT NULL,
|
|
|
|
key_auth TEXT NOT NULL,
|
|
|
|
key_p256dh TEXT NOT NULL,
|
|
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
|
|
);
|
2023-05-30 19:50:24 +02:00
|
|
|
CREATE INDEX IF NOT EXISTS idx_topic ON subscriptions (topic);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_endpoint ON subscriptions (endpoint);
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_topic_endpoint ON subscriptions (topic, endpoint);
|
2023-05-24 21:36:01 +02:00
|
|
|
COMMIT;
|
|
|
|
`
|
|
|
|
insertWebPushSubscriptionQuery = `
|
2023-05-30 19:50:24 +02:00
|
|
|
INSERT OR REPLACE INTO subscriptions (topic, username, endpoint, key_auth, key_p256dh)
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
2023-05-24 21:36:01 +02:00
|
|
|
`
|
2023-05-30 19:50:24 +02:00
|
|
|
deleteWebPushSubscriptionByEndpointQuery = `DELETE FROM subscriptions WHERE endpoint = ?`
|
|
|
|
deleteWebPushSubscriptionByUsernameQuery = `DELETE FROM subscriptions WHERE username = ?`
|
|
|
|
deleteWebPushSubscriptionByTopicAndEndpointQuery = `DELETE FROM subscriptions WHERE topic = ? AND endpoint = ?`
|
2023-05-24 21:36:01 +02:00
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
selectWebPushSubscriptionsForTopicQuery = `SELECT endpoint, key_auth, key_p256dh, username FROM subscriptions WHERE topic = ?`
|
2023-05-24 21:36:01 +02:00
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
selectWebPushSubscriptionsCountQuery = `SELECT COUNT(*) FROM subscriptions`
|
2023-05-24 21:36:01 +02:00
|
|
|
)
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
type webPushStore struct {
|
2023-05-24 21:36:01 +02:00
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
func newWebPushStore(filename string) (*webPushStore, error) {
|
2023-05-24 21:36:01 +02:00
|
|
|
db, err := sql.Open("sqlite3", filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-30 19:50:24 +02:00
|
|
|
if err := setupSubscriptionsDB(db); err != nil {
|
2023-05-24 21:36:01 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-30 19:50:24 +02:00
|
|
|
return &webPushStore{
|
2023-05-24 21:36:01 +02:00
|
|
|
db: db,
|
2023-05-30 19:50:24 +02:00
|
|
|
}, nil
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
func setupSubscriptionsDB(db *sql.DB) error {
|
|
|
|
// If 'subscriptions' table does not exist, this must be a new database
|
2023-05-24 21:36:01 +02:00
|
|
|
rowsMC, err := db.Query(selectWebPushSubscriptionsCountQuery)
|
|
|
|
if err != nil {
|
2023-05-30 19:50:24 +02:00
|
|
|
return setupNewSubscriptionsDB(db)
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
2023-05-30 19:50:24 +02:00
|
|
|
return rowsMC.Close()
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
func setupNewSubscriptionsDB(db *sql.DB) error {
|
2023-05-24 21:36:01 +02:00
|
|
|
if _, err := db.Exec(createWebPushSubscriptionsTableQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
func (c *webPushStore) AddSubscription(topic string, username string, subscription webPushSubscribePayload) error {
|
2023-05-24 21:36:01 +02:00
|
|
|
_, err := c.db.Exec(
|
|
|
|
insertWebPushSubscriptionQuery,
|
|
|
|
topic,
|
|
|
|
username,
|
|
|
|
subscription.BrowserSubscription.Endpoint,
|
|
|
|
subscription.BrowserSubscription.Keys.Auth,
|
|
|
|
subscription.BrowserSubscription.Keys.P256dh,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
func (c *webPushStore) RemoveSubscription(topic string, endpoint string) error {
|
2023-05-24 21:36:01 +02:00
|
|
|
_, err := c.db.Exec(
|
|
|
|
deleteWebPushSubscriptionByTopicAndEndpointQuery,
|
|
|
|
topic,
|
|
|
|
endpoint,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
func (c *webPushStore) GetSubscriptionsForTopic(topic string) (subscriptions []webPushSubscription, err error) {
|
2023-05-24 21:36:01 +02:00
|
|
|
rows, err := c.db.Query(selectWebPushSubscriptionsForTopicQuery, topic)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
var data []webPushSubscription
|
2023-05-24 21:36:01 +02:00
|
|
|
for rows.Next() {
|
|
|
|
i := webPushSubscription{}
|
|
|
|
err = rows.Scan(&i.BrowserSubscription.Endpoint, &i.BrowserSubscription.Keys.Auth, &i.BrowserSubscription.Keys.P256dh, &i.Username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data = append(data, i)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
func (c *webPushStore) ExpireWebPushEndpoint(endpoint string) error {
|
2023-05-24 21:36:01 +02:00
|
|
|
_, err := c.db.Exec(
|
|
|
|
deleteWebPushSubscriptionByEndpointQuery,
|
|
|
|
endpoint,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:50:24 +02:00
|
|
|
func (c *webPushStore) ExpireWebPushForUser(username string) error {
|
2023-05-24 21:36:01 +02:00
|
|
|
_, err := c.db.Exec(
|
|
|
|
deleteWebPushSubscriptionByUsernameQuery,
|
|
|
|
username,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
2023-05-30 19:50:24 +02:00
|
|
|
func (c *webPushStore) Close() error {
|
2023-05-24 21:36:01 +02:00
|
|
|
return c.db.Close()
|
|
|
|
}
|