| 1 |
package webpush |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql" |
| 5 |
"errors" |
| 6 |
"net/netip" |
| 7 |
"time" |
| 8 |
|
| 9 |
"heckel.io/ntfy/v2/db" |
| 10 |
"heckel.io/ntfy/v2/util" |
| 11 |
) |
| 12 |
|
| 13 |
const ( |
| 14 |
subscriptionIDPrefix = "wps_" |
| 15 |
subscriptionIDLength = 10 |
| 16 |
subscriptionEndpointLimitPerSubscriberIP = 10 |
| 17 |
|
| 18 |
schemaStore = "webpush" |
| 19 |
) |
| 20 |
|
| 21 |
// Errors returned by the store |
| 22 |
var ( |
| 23 |
ErrWebPushTooManySubscriptions = errors.New("too many subscriptions") |
| 24 |
ErrWebPushUserIDCannotBeEmpty = errors.New("user ID cannot be empty") |
| 25 |
) |
| 26 |
|
| 27 |
// Store holds the database connection and queries for web push subscriptions. |
| 28 |
type Store struct { |
| 29 |
db *db.DB |
| 30 |
queries queries |
| 31 |
} |
| 32 |
|
| 33 |
// queries holds the database-specific SQL queries. |
| 34 |
type queries struct { |
| 35 |
selectSubscriptionIDByEndpoint string |
| 36 |
selectSubscriptionCountBySubscriberIP string |
| 37 |
selectSubscriptionsForTopic string |
| 38 |
selectSubscriptionsExpiringSoon string |
| 39 |
upsertSubscription string |
| 40 |
updateSubscriptionWarningSent string |
| 41 |
updateSubscriptionUpdatedAt string |
| 42 |
deleteSubscriptionByEndpoint string |
| 43 |
deleteSubscriptionByUserID string |
| 44 |
deleteSubscriptionByAge string |
| 45 |
insertSubscriptionTopic string |
| 46 |
deleteSubscriptionTopicAll string |
| 47 |
deleteSubscriptionTopicWithoutSubscription string |
| 48 |
} |
| 49 |
|
| 50 |
// UpsertSubscription adds or updates Web Push subscriptions for the given topics and user ID. |
| 51 |
func (s *Store) UpsertSubscription(endpoint string, auth, p256dh, userID string, subscriberIP netip.Addr, topics []string) error { |
| 52 |
return db.ExecTx(s.db, func(tx *sql.Tx) error { |
| 53 |
// Read number of subscriptions for subscriber IP address |
| 54 |
var subscriptionCount int |
| 55 |
if err := tx.QueryRow(s.queries.selectSubscriptionCountBySubscriberIP, subscriberIP.String()).Scan(&subscriptionCount); err != nil { |
| 56 |
return err |
| 57 |
} |
| 58 |
// Read existing subscription ID for endpoint (or create new ID) |
| 59 |
var subscriptionID string |
| 60 |
if err := tx.QueryRow(s.queries.selectSubscriptionIDByEndpoint, endpoint).Scan(&subscriptionID); errors.Is(err, sql.ErrNoRows) { |
| 61 |
if subscriptionCount >= subscriptionEndpointLimitPerSubscriberIP { |
| 62 |
return ErrWebPushTooManySubscriptions |
| 63 |
} |
| 64 |
subscriptionID = util.RandomStringPrefix(subscriptionIDPrefix, subscriptionIDLength) |
| 65 |
} else if err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
// Insert or update subscription, and read back the actual ID (which may differ from |
| 69 |
// the generated one if another request for the same endpoint raced us and inserted first) |
| 70 |
updatedAt, warnedAt := time.Now().Unix(), 0 |
| 71 |
if err := tx.QueryRow(s.queries.upsertSubscription, subscriptionID, endpoint, auth, p256dh, userID, subscriberIP.String(), updatedAt, warnedAt).Scan(&subscriptionID); err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
// Replace all subscription topics |
| 75 |
if _, err := tx.Exec(s.queries.deleteSubscriptionTopicAll, subscriptionID); err != nil { |
| 76 |
return err |
| 77 |
} |
| 78 |
for _, topic := range topics { |
| 79 |
if _, err := tx.Exec(s.queries.insertSubscriptionTopic, subscriptionID, topic); err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
} |
| 83 |
return nil |
| 84 |
}) |
| 85 |
} |
| 86 |
|
| 87 |
// SubscriptionsForTopic returns all subscriptions for the given topic. |
| 88 |
func (s *Store) SubscriptionsForTopic(topic string) ([]*Subscription, error) { |
| 89 |
rows, err := s.db.ReadOnly().Query(s.queries.selectSubscriptionsForTopic, topic) |
| 90 |
if err != nil { |
| 91 |
return nil, err |
| 92 |
} |
| 93 |
defer rows.Close() |
| 94 |
return subscriptionsFromRows(rows) |
| 95 |
} |
| 96 |
|
| 97 |
// SubscriptionsExpiring returns all subscriptions that have not been updated for a given time period. |
| 98 |
func (s *Store) SubscriptionsExpiring(warnAfter time.Duration) ([]*Subscription, error) { |
| 99 |
rows, err := s.db.ReadOnly().Query(s.queries.selectSubscriptionsExpiringSoon, time.Now().Add(-warnAfter).Unix()) |
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
defer rows.Close() |
| 104 |
return subscriptionsFromRows(rows) |
| 105 |
} |
| 106 |
|
| 107 |
// MarkExpiryWarningSent marks the given subscriptions as having received a warning about expiring soon. |
| 108 |
func (s *Store) MarkExpiryWarningSent(subscriptions []*Subscription) error { |
| 109 |
return db.ExecTx(s.db, func(tx *sql.Tx) error { |
| 110 |
for _, subscription := range subscriptions { |
| 111 |
if _, err := tx.Exec(s.queries.updateSubscriptionWarningSent, time.Now().Unix(), subscription.ID); err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
} |
| 115 |
return nil |
| 116 |
}) |
| 117 |
} |
| 118 |
|
| 119 |
// RemoveSubscriptionsByEndpoint removes the subscription for the given endpoint. |
| 120 |
func (s *Store) RemoveSubscriptionsByEndpoint(endpoint string) error { |
| 121 |
_, err := s.db.Exec(s.queries.deleteSubscriptionByEndpoint, endpoint) |
| 122 |
return err |
| 123 |
} |
| 124 |
|
| 125 |
// RemoveSubscriptionsByUserID removes all subscriptions for the given user ID. |
| 126 |
func (s *Store) RemoveSubscriptionsByUserID(userID string) error { |
| 127 |
if userID == "" { |
| 128 |
return ErrWebPushUserIDCannotBeEmpty |
| 129 |
} |
| 130 |
_, err := s.db.Exec(s.queries.deleteSubscriptionByUserID, userID) |
| 131 |
return err |
| 132 |
} |
| 133 |
|
| 134 |
// RemoveExpiredSubscriptions removes all subscriptions that have not been updated for a given time period. |
| 135 |
func (s *Store) RemoveExpiredSubscriptions(expireAfter time.Duration) error { |
| 136 |
return db.ExecTx(s.db, func(tx *sql.Tx) error { |
| 137 |
if _, err := tx.Exec(s.queries.deleteSubscriptionByAge, time.Now().Add(-expireAfter).Unix()); err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
_, err := tx.Exec(s.queries.deleteSubscriptionTopicWithoutSubscription) |
| 141 |
return err |
| 142 |
}) |
| 143 |
} |
| 144 |
|
| 145 |
// SetSubscriptionUpdatedAt updates the updated_at timestamp for a subscription by endpoint. This is |
| 146 |
// exported for testing purposes. |
| 147 |
func (s *Store) SetSubscriptionUpdatedAt(endpoint string, updatedAt int64) error { |
| 148 |
_, err := s.db.Exec(s.queries.updateSubscriptionUpdatedAt, updatedAt, endpoint) |
| 149 |
return err |
| 150 |
} |
| 151 |
|
| 152 |
// Close closes the underlying database connection. |
| 153 |
func (s *Store) Close() error { |
| 154 |
return s.db.Close() |
| 155 |
} |
| 156 |
|
| 157 |
func subscriptionsFromRows(rows *sql.Rows) ([]*Subscription, error) { |
| 158 |
subscriptions := make([]*Subscription, 0) |
| 159 |
for rows.Next() { |
| 160 |
var id, endpoint, auth, p256dh, userID string |
| 161 |
if err := rows.Scan(&id, &endpoint, &auth, &p256dh, &userID); err != nil { |
| 162 |
return nil, err |
| 163 |
} |
| 164 |
subscriptions = append(subscriptions, &Subscription{ |
| 165 |
ID: id, |
| 166 |
Endpoint: endpoint, |
| 167 |
Auth: auth, |
| 168 |
P256dh: p256dh, |
| 169 |
UserID: userID, |
| 170 |
}) |
| 171 |
} |
| 172 |
return subscriptions, nil |
| 173 |
} |