ntfy/server/web_push.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
3.4 KiB
Go
Raw Normal View History

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