Files
silo-server/internal/catalog/server_settings_repo.go
T
QuickandClaude Fable 5 e5b210589d fix(notifications): address PR #136 review findings
Codex + CodeRabbit review fixes, all verified against current behavior:

- Web Push: single-writer VAPID provisioning via a new conditional
  SetIfAbsent settings write (no split-brain identity across nodes), and
  read/decode failures now surface instead of silently rotating the
  keypair; the eager-provisioning goroutine joins the shutdown WaitGroup
- Web Push: endpoint reassignment purges the previous owner's pending
  attempts inside the upsert transaction, with an ownership re-check at
  send time
- Webhooks: per-profile cap enforced atomically (advisory-locked
  count+insert), typed pgconn unique-violation mapping, create-time
  type/URL mismatch rejection, send-time HTTPS re-check, and Retry-After
  HTTP-date support (shared, clamped parser also used by web push)
- Delivery workers: transient delivery-row lookup errors leave the claim
  to lease expiry instead of permanently failing the attempt
- Interest: history-only imports now feed the index (userstore history
  hooks + completed-history folding in recompute/rebuild), rebuild also
  recomputes existing interest rows so removed sources get cleaned up,
  and failed flush mutations requeue (bounded) instead of dropping
- Retention: read notifications age from read_at, not created_at
- Startup: scan queue workers start only after the availability detector
  is wired, so resumed scans cannot skip availability recording
- mail: settings-store read failures propagate instead of reading as
  "not configured"
- DB: new migration adds episode ordinal/key CHECK constraints
- Web: service worker restricts notification clicks to same-origin URLs,
  preferences popover gets an error+retry state, and the realtime
  profile-rebind backoff grows to 5 minutes to keep shared channels
  stable through notifications-only outages

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 17:16:27 -04:00

86 lines
2.5 KiB
Go

package catalog
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
)
// ServerSettingsRepo provides CRUD access to the server_settings table.
type ServerSettingsRepo struct {
pool *pgxpool.Pool
}
// NewServerSettingsRepo creates a new ServerSettingsRepo.
func NewServerSettingsRepo(pool *pgxpool.Pool) *ServerSettingsRepo {
return &ServerSettingsRepo{pool: pool}
}
// Get retrieves a single setting by key. Returns empty string if not found.
func (r *ServerSettingsRepo) Get(ctx context.Context, key string) (string, error) {
var value string
err := r.pool.QueryRow(ctx,
`SELECT value FROM server_settings WHERE key = $1`, key,
).Scan(&value)
if err != nil {
if err.Error() == "no rows in result set" {
return "", nil
}
return "", fmt.Errorf("server_settings get %q: %w", key, err)
}
return value, nil
}
// Set upserts a setting.
func (r *ServerSettingsRepo) Set(ctx context.Context, key, value string) error {
_, err := r.pool.Exec(ctx,
`INSERT INTO server_settings (key, value) VALUES ($1, $2)
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value`,
key, value,
)
if err != nil {
return fmt.Errorf("server_settings set %q: %w", key, err)
}
return nil
}
// SetIfAbsent inserts a setting only when the key has no value yet (absent or
// empty), reporting whether this call won the write. Generated credentials
// (e.g. the web push VAPID keypair) must be provisioned single-writer across
// concurrent nodes: exactly one generated value may ever land.
func (r *ServerSettingsRepo) SetIfAbsent(ctx context.Context, key, value string) (bool, error) {
tag, err := r.pool.Exec(ctx,
`INSERT INTO server_settings (key, value) VALUES ($1, $2)
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value
WHERE server_settings.value = ''`,
key, value,
)
if err != nil {
return false, fmt.Errorf("server_settings set-if-absent %q: %w", key, err)
}
return tag.RowsAffected() > 0, nil
}
// GetAll retrieves all settings as a map.
func (r *ServerSettingsRepo) GetAll(ctx context.Context) (map[string]string, error) {
rows, err := r.pool.Query(ctx, `SELECT key, value FROM server_settings`)
if err != nil {
return nil, fmt.Errorf("server_settings get all: %w", err)
}
defer rows.Close()
settings := make(map[string]string)
for rows.Next() {
var k, v string
if err := rows.Scan(&k, &v); err != nil {
return nil, fmt.Errorf("server_settings scan: %w", err)
}
settings[k] = v
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("server_settings iterate: %w", err)
}
return settings, nil
}