Manual refactor
This commit is contained in:
@@ -40,7 +40,7 @@ type storeQueries struct {
|
||||
selectSubscriptionCountBySubscriberIP string
|
||||
selectSubscriptionsForTopic string
|
||||
selectSubscriptionsExpiringSoon string
|
||||
insertSubscription string
|
||||
upsertSubscription string
|
||||
updateSubscriptionWarningSent string
|
||||
updateSubscriptionUpdatedAt string
|
||||
deleteSubscriptionByEndpoint string
|
||||
@@ -71,8 +71,7 @@ func (s *commonStore) UpsertSubscription(endpoint string, auth, p256dh, userID s
|
||||
}
|
||||
// Read existing subscription ID for endpoint (or create new ID)
|
||||
var subscriptionID string
|
||||
err = tx.QueryRow(s.queries.selectSubscriptionIDByEndpoint, endpoint).Scan(&subscriptionID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
if err := tx.QueryRow(s.queries.selectSubscriptionIDByEndpoint, endpoint).Scan(&subscriptionID); errors.Is(err, sql.ErrNoRows) {
|
||||
if subscriptionCount >= subscriptionEndpointLimitPerSubscriberIP {
|
||||
return ErrWebPushTooManySubscriptions
|
||||
}
|
||||
@@ -82,7 +81,7 @@ func (s *commonStore) UpsertSubscription(endpoint string, auth, p256dh, userID s
|
||||
}
|
||||
// Insert or update subscription
|
||||
updatedAt, warnedAt := time.Now().Unix(), 0
|
||||
if _, err = tx.Exec(s.queries.insertSubscription, subscriptionID, endpoint, auth, p256dh, userID, subscriberIP.String(), updatedAt, warnedAt); err != nil {
|
||||
if _, err := tx.Exec(s.queries.upsertSubscription, subscriptionID, endpoint, auth, p256dh, userID, subscriberIP.String(), updatedAt, warnedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
// Replace all subscription topics
|
||||
|
||||
@@ -44,7 +44,7 @@ const (
|
||||
FROM webpush_subscription
|
||||
WHERE warned_at = 0 AND updated_at <= $1
|
||||
`
|
||||
postgresInsertSubscriptionQuery = `
|
||||
postgresUpsertSubscriptionQuery = `
|
||||
INSERT INTO webpush_subscription (id, endpoint, key_auth, key_p256dh, user_id, subscriber_ip, updated_at, warned_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (endpoint)
|
||||
@@ -80,7 +80,7 @@ func NewPostgresStore(db *sql.DB) (Store, error) {
|
||||
selectSubscriptionCountBySubscriberIP: postgresSelectSubscriptionCountBySubscriberIPQuery,
|
||||
selectSubscriptionsForTopic: postgresSelectSubscriptionsForTopicQuery,
|
||||
selectSubscriptionsExpiringSoon: postgresSelectSubscriptionsExpiringSoonQuery,
|
||||
insertSubscription: postgresInsertSubscriptionQuery,
|
||||
upsertSubscription: postgresUpsertSubscriptionQuery,
|
||||
updateSubscriptionWarningSent: postgresUpdateSubscriptionWarningSentQuery,
|
||||
updateSubscriptionUpdatedAt: postgresUpdateSubscriptionUpdatedAtQuery,
|
||||
deleteSubscriptionByEndpoint: postgresDeleteSubscriptionByEndpointQuery,
|
||||
|
||||
@@ -96,7 +96,7 @@ func NewSQLiteStore(filename, startupQueries string) (Store, error) {
|
||||
selectSubscriptionCountBySubscriberIP: sqliteSelectWebPushSubscriptionCountBySubscriberIPQuery,
|
||||
selectSubscriptionsForTopic: sqliteSelectWebPushSubscriptionsForTopicQuery,
|
||||
selectSubscriptionsExpiringSoon: sqliteSelectWebPushSubscriptionsExpiringSoonQuery,
|
||||
insertSubscription: sqliteInsertWebPushSubscriptionQuery,
|
||||
upsertSubscription: sqliteInsertWebPushSubscriptionQuery,
|
||||
updateSubscriptionWarningSent: sqliteUpdateWebPushSubscriptionWarningSentQuery,
|
||||
updateSubscriptionUpdatedAt: sqliteUpdateWebPushSubscriptionUpdatedAtQuery,
|
||||
deleteSubscriptionByEndpoint: sqliteDeleteWebPushSubscriptionByEndpointQuery,
|
||||
|
||||
Reference in New Issue
Block a user