2023-05-24 21:36:01 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2023-06-02 13:22:54 +02:00
|
|
|
"fmt"
|
2023-06-02 14:45:05 +02:00
|
|
|
"time"
|
2023-05-24 21:36:01 +02:00
|
|
|
|
|
|
|
_ "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,
|
2023-05-30 20:23:03 +02:00
|
|
|
user_id TEXT,
|
2023-05-24 21:36:01 +02:00
|
|
|
endpoint TEXT NOT NULL,
|
|
|
|
key_auth TEXT NOT NULL,
|
|
|
|
key_p256dh TEXT NOT NULL,
|
2023-06-02 14:45:05 +02:00
|
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
warning_sent BOOLEAN DEFAULT FALSE
|
2023-05-24 21:36:01 +02:00
|
|
|
);
|
2023-06-09 03:45:52 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS schemaVersion (
|
|
|
|
id INT PRIMARY KEY,
|
|
|
|
version INT NOT NULL
|
|
|
|
);
|
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;
|
|
|
|
`
|
2023-06-09 03:45:52 +02:00
|
|
|
|
2023-05-24 21:36:01 +02:00
|
|
|
insertWebPushSubscriptionQuery = `
|
2023-05-30 20:23:03 +02:00
|
|
|
INSERT OR REPLACE INTO subscriptions (topic, user_id, endpoint, key_auth, key_p256dh)
|
2023-05-30 19:50:24 +02:00
|
|
|
VALUES (?, ?, ?, ?, ?)
|
2023-05-24 21:36:01 +02:00
|
|
|
`
|
2023-06-08 20:30:19 +02:00
|
|
|
deleteWebPushSubscriptionByEndpointQuery = `DELETE FROM subscriptions WHERE endpoint = ?`
|
|
|
|
deleteWebPushSubscriptionByUserIDQuery = `DELETE FROM subscriptions WHERE user_id = ?`
|
|
|
|
deleteWebPushSubscriptionsByAgeQuery = `DELETE FROM subscriptions WHERE warning_sent = 1 AND updated_at <= datetime('now', ?)`
|
2023-05-24 21:36:01 +02:00
|
|
|
|
2023-06-02 14:45:05 +02:00
|
|
|
selectWebPushSubscriptionsForTopicQuery = `SELECT endpoint, key_auth, key_p256dh, user_id FROM subscriptions WHERE topic = ?`
|
2023-06-09 05:09:38 +02:00
|
|
|
selectWebPushSubscriptionsExpiringSoonQuery = `SELECT DISTINCT endpoint, key_auth, key_p256dh, user_id FROM subscriptions WHERE warning_sent = 0 AND updated_at <= datetime('now', ?)`
|
2023-06-02 14:45:05 +02:00
|
|
|
|
|
|
|
updateWarningSentQuery = `UPDATE subscriptions SET warning_sent = true WHERE warning_sent = 0 AND updated_at <= datetime('now', ?)`
|
2023-06-09 03:45:52 +02:00
|
|
|
)
|
2023-05-24 21:36:01 +02:00
|
|
|
|
2023-06-09 03:45:52 +02:00
|
|
|
// Schema management queries
|
|
|
|
const (
|
|
|
|
currentWebPushSchemaVersion = 1
|
|
|
|
insertWebPushSchemaVersion = `INSERT INTO schemaVersion VALUES (1, ?)`
|
|
|
|
selectWebPushSchemaVersionQuery = `SELECT version FROM schemaVersion WHERE id = 1`
|
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-06-09 03:45:52 +02:00
|
|
|
if err := setupWebPushDB(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-06-09 03:45:52 +02:00
|
|
|
func setupWebPushDB(db *sql.DB) error {
|
|
|
|
// If 'schemaVersion' table does not exist, this must be a new database
|
|
|
|
rows, err := db.Query(selectWebPushSchemaVersionQuery)
|
2023-05-24 21:36:01 +02:00
|
|
|
if err != nil {
|
2023-06-09 03:45:52 +02:00
|
|
|
return setupNewWebPushDB(db)
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
2023-06-08 18:20:12 +02:00
|
|
|
return rows.Close()
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
|
|
|
|
2023-06-09 03:45:52 +02:00
|
|
|
func setupNewWebPushDB(db *sql.DB) error {
|
2023-05-24 21:36:01 +02:00
|
|
|
if _, err := db.Exec(createWebPushSubscriptionsTableQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-09 03:45:52 +02:00
|
|
|
if _, err := db.Exec(insertWebPushSchemaVersion, currentWebPushSchemaVersion); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 21:36:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-09 05:09:38 +02:00
|
|
|
// UpsertSubscription adds or updates Web Push subscriptions for the given topics and user ID. It always first deletes all
|
2023-06-09 03:45:52 +02:00
|
|
|
// existing entries for a given endpoint.
|
2023-06-09 05:09:38 +02:00
|
|
|
func (c *webPushStore) UpsertSubscription(endpoint string, topics []string, userID, auth, p256dh string) error {
|
2023-06-02 13:22:54 +02:00
|
|
|
tx, err := c.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2023-06-09 05:09:38 +02:00
|
|
|
if _, err := tx.Exec(deleteWebPushSubscriptionByEndpointQuery, endpoint); err != nil {
|
2023-06-02 13:22:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, topic := range topics {
|
2023-06-09 05:09:38 +02:00
|
|
|
if _, err = tx.Exec(insertWebPushSubscriptionQuery, topic, userID, endpoint, auth, p256dh); err != nil {
|
2023-06-02 13:22:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
|
|
|
|
2023-06-09 05:09:38 +02:00
|
|
|
func (c *webPushStore) SubscriptionsForTopic(topic string) ([]*webPushSubscription, 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-06-09 05:09:38 +02:00
|
|
|
subscriptions := make([]*webPushSubscription, 0)
|
2023-05-24 21:36:01 +02:00
|
|
|
for rows.Next() {
|
2023-06-09 05:09:38 +02:00
|
|
|
var endpoint, auth, p256dh, userID string
|
2023-06-08 18:20:12 +02:00
|
|
|
if err = rows.Scan(&endpoint, &auth, &p256dh, &userID); err != nil {
|
2023-05-24 21:36:01 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-09 05:09:38 +02:00
|
|
|
subscriptions = append(subscriptions, &webPushSubscription{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
Auth: auth,
|
|
|
|
P256dh: p256dh,
|
|
|
|
UserID: userID,
|
2023-06-08 18:20:12 +02:00
|
|
|
})
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
2023-06-09 05:09:38 +02:00
|
|
|
return subscriptions, nil
|
2023-05-24 21:36:01 +02:00
|
|
|
}
|
|
|
|
|
2023-06-09 05:09:38 +02:00
|
|
|
func (c *webPushStore) ExpireAndGetExpiringSubscriptions(warningDuration time.Duration, expiryDuration time.Duration) ([]*webPushSubscription, error) {
|
2023-06-08 18:20:12 +02:00
|
|
|
// TODO this should be two functions
|
2023-06-02 14:45:05 +02:00
|
|
|
tx, err := c.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
_, err = tx.Exec(deleteWebPushSubscriptionsByAgeQuery, fmt.Sprintf("-%.2f seconds", expiryDuration.Seconds()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := tx.Query(selectWebPushSubscriptionsExpiringSoonQuery, fmt.Sprintf("-%.2f seconds", warningDuration.Seconds()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-06-09 05:09:38 +02:00
|
|
|
subscriptions := make([]*webPushSubscription, 0)
|
2023-06-02 14:45:05 +02:00
|
|
|
for rows.Next() {
|
2023-06-09 05:09:38 +02:00
|
|
|
var endpoint, auth, p256dh, userID string
|
|
|
|
if err = rows.Scan(&endpoint, &auth, &p256dh, &userID); err != nil {
|
2023-06-02 14:45:05 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-09 05:09:38 +02:00
|
|
|
subscriptions = append(subscriptions, &webPushSubscription{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
Auth: auth,
|
|
|
|
P256dh: p256dh,
|
|
|
|
UserID: userID,
|
|
|
|
})
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// also set warning as sent
|
|
|
|
_, err = tx.Exec(updateWarningSentQuery, fmt.Sprintf("-%.2f seconds", warningDuration.Seconds()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:30:19 +02:00
|
|
|
if err = tx.Commit(); err != nil {
|
2023-06-02 14:45:05 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-09 05:09:38 +02:00
|
|
|
return subscriptions, nil
|
2023-06-02 14:45:05 +02:00
|
|
|
}
|
|
|
|
|
2023-06-09 05:09:38 +02:00
|
|
|
func (c *webPushStore) RemoveSubscriptionsByEndpoint(endpoint string) error {
|
|
|
|
_, err := c.db.Exec(deleteWebPushSubscriptionByEndpointQuery, endpoint)
|
2023-05-24 21:36:01 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-09 05:09:38 +02:00
|
|
|
func (c *webPushStore) RemoveSubscriptionsByUserID(userID string) error {
|
|
|
|
_, err := c.db.Exec(deleteWebPushSubscriptionByUserIDQuery, userID)
|
2023-05-24 21:36:01 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-06-02 14:45:05 +02:00
|
|
|
|
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()
|
|
|
|
}
|