ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / test (pull_request) Successful in 9m5s
ci / lint (pull_request) Successful in 10m24s
ci / build (pull_request) Successful in 18m10s
ci / image (pull_request) Successful in 5m16s
Visitors can subscribe to a page's incidents by email where the organization allows it. They confirm their address first; confirmation and unsubscribe links only show a button, so mail scanners do not act on them, and mails carry RFC 8058 one-click unsubscribe headers. Subscribers get a mail when an incident opens or resolves and for every public update. Every page also has an RSS feed of incidents and planned maintenance and a read-only JSON API (status, summary, components, incidents), both served from the cached snapshot. Abuse protection: status page servers limit feed and API reads, subscription requests and link actions per visitor; the API limits subscription requests per visitor, mailbox and page, and a new limit caps the subscriptions an organization's pages gain in seven days (platform default and per-organization override, 1000 in the cloud). Unconfirmed subscriptions count towards it and are deleted after a week. The fixed cap of 200 monitors per status page is gone; the configurable limit alone decides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
443 lines
13 KiB
Go
443 lines
13 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: subscribers.sql
|
|
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const claimSubscriberNotifications = `-- name: ClaimSubscriberNotifications :many
|
|
UPDATE subscriber_notifications SET next_attempt_at = now() + interval '5 minutes'
|
|
WHERE id IN (
|
|
SELECT id FROM subscriber_notifications
|
|
WHERE status = 'pending' AND next_attempt_at <= now()
|
|
ORDER BY next_attempt_at
|
|
LIMIT $1
|
|
FOR UPDATE SKIP LOCKED
|
|
)
|
|
RETURNING id
|
|
`
|
|
|
|
// System scope; leased like ClaimNotifications.
|
|
func (q *Queries) ClaimSubscriberNotifications(ctx context.Context, max int32) ([]uuid.UUID, error) {
|
|
rows, err := q.db.Query(ctx, claimSubscriberNotifications, max)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []uuid.UUID{}
|
|
for rows.Next() {
|
|
var id uuid.UUID
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const confirmPageSubscriber = `-- name: ConfirmPageSubscriber :exec
|
|
UPDATE status_page_subscribers SET confirmed_at = coalesce(confirmed_at, now()) WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) ConfirmPageSubscriber(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, confirmPageSubscriber, id)
|
|
return err
|
|
}
|
|
|
|
const countPageSubscribers = `-- name: CountPageSubscribers :one
|
|
SELECT count(*) FILTER (WHERE confirmed_at IS NOT NULL)::integer AS confirmed,
|
|
count(*) FILTER (WHERE confirmed_at IS NULL)::integer AS pending
|
|
FROM status_page_subscribers WHERE page_id = $1
|
|
`
|
|
|
|
type CountPageSubscribersRow struct {
|
|
Confirmed int32
|
|
Pending int32
|
|
}
|
|
|
|
func (q *Queries) CountPageSubscribers(ctx context.Context, pageID uuid.UUID) (CountPageSubscribersRow, error) {
|
|
row := q.db.QueryRow(ctx, countPageSubscribers, pageID)
|
|
var i CountPageSubscribersRow
|
|
err := row.Scan(&i.Confirmed, &i.Pending)
|
|
return i, err
|
|
}
|
|
|
|
const countRecentSubscribers = `-- name: CountRecentSubscribers :one
|
|
SELECT count(*) FROM status_page_subscribers WHERE org_id = $1 AND created_at > $2
|
|
`
|
|
|
|
type CountRecentSubscribersParams struct {
|
|
OrgID uuid.UUID
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// Subscriptions the organization's pages gained since $2, confirmed or
|
|
// not, for the weekly limit.
|
|
func (q *Queries) CountRecentSubscribers(ctx context.Context, arg CountRecentSubscribersParams) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countRecentSubscribers, arg.OrgID, arg.CreatedAt)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const createPageSubscriber = `-- name: CreatePageSubscriber :one
|
|
INSERT INTO status_page_subscribers (org_id, page_id, email, locale, page_url)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING id, org_id, page_id, email, locale, page_url, created_at, confirmed_at
|
|
`
|
|
|
|
type CreatePageSubscriberParams struct {
|
|
OrgID uuid.UUID
|
|
PageID uuid.UUID
|
|
Email string
|
|
Locale string
|
|
PageUrl string
|
|
}
|
|
|
|
func (q *Queries) CreatePageSubscriber(ctx context.Context, arg CreatePageSubscriberParams) (StatusPageSubscriber, error) {
|
|
row := q.db.QueryRow(ctx, createPageSubscriber,
|
|
arg.OrgID,
|
|
arg.PageID,
|
|
arg.Email,
|
|
arg.Locale,
|
|
arg.PageUrl,
|
|
)
|
|
var i StatusPageSubscriber
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.PageID,
|
|
&i.Email,
|
|
&i.Locale,
|
|
&i.PageUrl,
|
|
&i.CreatedAt,
|
|
&i.ConfirmedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deletePageSubscriber = `-- name: DeletePageSubscriber :execrows
|
|
DELETE FROM status_page_subscribers WHERE id = $1 AND page_id = $2
|
|
`
|
|
|
|
type DeletePageSubscriberParams struct {
|
|
ID uuid.UUID
|
|
PageID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) DeletePageSubscriber(ctx context.Context, arg DeletePageSubscriberParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deletePageSubscriber, arg.ID, arg.PageID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const enqueueSubscriberNotifications = `-- name: EnqueueSubscriberNotifications :execrows
|
|
INSERT INTO subscriber_notifications (org_id, subscriber_id, incident_id, update_id, event)
|
|
SELECT s.org_id, s.id, i.id, $1::uuid, $2
|
|
FROM incidents i
|
|
JOIN status_page_monitors spm ON spm.monitor_id = i.monitor_id
|
|
JOIN status_pages p ON p.id = spm.page_id
|
|
JOIN status_page_subscribers s ON s.page_id = p.id
|
|
WHERE i.id = $3 AND s.confirmed_at IS NOT NULL
|
|
AND p.published AND p.subscriptions AND p.suspended_at IS NULL
|
|
ON CONFLICT DO NOTHING
|
|
`
|
|
|
|
type EnqueueSubscriberNotificationsParams struct {
|
|
UpdateID *uuid.UUID
|
|
Event string
|
|
IncidentID uuid.UUID
|
|
}
|
|
|
|
// Queues the event of incident for every confirmed subscriber of the
|
|
// published pages showing its monitor that take subscriptions.
|
|
func (q *Queries) EnqueueSubscriberNotifications(ctx context.Context, arg EnqueueSubscriberNotificationsParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, enqueueSubscriberNotifications, arg.UpdateID, arg.Event, arg.IncidentID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const getPageSubscriber = `-- name: GetPageSubscriber :one
|
|
SELECT s.id, s.org_id, s.page_id, s.email, s.locale, s.page_url, s.created_at, s.confirmed_at, p.title AS page_title FROM status_page_subscribers s
|
|
JOIN status_pages p ON p.id = s.page_id
|
|
WHERE s.id = $1
|
|
`
|
|
|
|
type GetPageSubscriberRow struct {
|
|
ID uuid.UUID
|
|
OrgID uuid.UUID
|
|
PageID uuid.UUID
|
|
Email string
|
|
Locale string
|
|
PageUrl string
|
|
CreatedAt time.Time
|
|
ConfirmedAt *time.Time
|
|
PageTitle string
|
|
}
|
|
|
|
func (q *Queries) GetPageSubscriber(ctx context.Context, id uuid.UUID) (GetPageSubscriberRow, error) {
|
|
row := q.db.QueryRow(ctx, getPageSubscriber, id)
|
|
var i GetPageSubscriberRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.PageID,
|
|
&i.Email,
|
|
&i.Locale,
|
|
&i.PageUrl,
|
|
&i.CreatedAt,
|
|
&i.ConfirmedAt,
|
|
&i.PageTitle,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getPageSubscriberByEmail = `-- name: GetPageSubscriberByEmail :one
|
|
SELECT id, org_id, page_id, email, locale, page_url, created_at, confirmed_at FROM status_page_subscribers WHERE page_id = $1 AND email = $2
|
|
`
|
|
|
|
type GetPageSubscriberByEmailParams struct {
|
|
PageID uuid.UUID
|
|
Email string
|
|
}
|
|
|
|
func (q *Queries) GetPageSubscriberByEmail(ctx context.Context, arg GetPageSubscriberByEmailParams) (StatusPageSubscriber, error) {
|
|
row := q.db.QueryRow(ctx, getPageSubscriberByEmail, arg.PageID, arg.Email)
|
|
var i StatusPageSubscriber
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.PageID,
|
|
&i.Email,
|
|
&i.Locale,
|
|
&i.PageUrl,
|
|
&i.CreatedAt,
|
|
&i.ConfirmedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listPageSubscribers = `-- name: ListPageSubscribers :many
|
|
SELECT id, email, locale, created_at, confirmed_at FROM status_page_subscribers
|
|
WHERE page_id = $1
|
|
ORDER BY created_at DESC, id
|
|
LIMIT $2
|
|
`
|
|
|
|
type ListPageSubscribersParams struct {
|
|
PageID uuid.UUID
|
|
MaxRows int32
|
|
}
|
|
|
|
type ListPageSubscribersRow struct {
|
|
ID uuid.UUID
|
|
Email string
|
|
Locale string
|
|
CreatedAt time.Time
|
|
ConfirmedAt *time.Time
|
|
}
|
|
|
|
// The newest subscribers of a page first.
|
|
func (q *Queries) ListPageSubscribers(ctx context.Context, arg ListPageSubscribersParams) ([]ListPageSubscribersRow, error) {
|
|
rows, err := q.db.Query(ctx, listPageSubscribers, arg.PageID, arg.MaxRows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListPageSubscribersRow{}
|
|
for rows.Next() {
|
|
var i ListPageSubscribersRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Email,
|
|
&i.Locale,
|
|
&i.CreatedAt,
|
|
&i.ConfirmedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const markSubscriberNotificationFailed = `-- name: MarkSubscriberNotificationFailed :exec
|
|
UPDATE subscriber_notifications SET attempts = attempts + 1, last_error = $1,
|
|
status = CASE WHEN attempts + 1 >= $2::int THEN 'failed' ELSE 'pending' END,
|
|
next_attempt_at = now() + make_interval(secs => $3::int)
|
|
WHERE id = $4
|
|
`
|
|
|
|
type MarkSubscriberNotificationFailedParams struct {
|
|
LastError *string
|
|
MaxAttempts int32
|
|
RetryInSeconds int32
|
|
ID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) MarkSubscriberNotificationFailed(ctx context.Context, arg MarkSubscriberNotificationFailedParams) error {
|
|
_, err := q.db.Exec(ctx, markSubscriberNotificationFailed,
|
|
arg.LastError,
|
|
arg.MaxAttempts,
|
|
arg.RetryInSeconds,
|
|
arg.ID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const markSubscriberNotificationSent = `-- name: MarkSubscriberNotificationSent :exec
|
|
UPDATE subscriber_notifications SET status = 'sent', sent_at = now(), attempts = attempts + 1, last_error = NULL
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) MarkSubscriberNotificationSent(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, markSubscriberNotificationSent, id)
|
|
return err
|
|
}
|
|
|
|
const purgeSubscriberNotifications = `-- name: PurgeSubscriberNotifications :execrows
|
|
DELETE FROM subscriber_notifications WHERE status IN ('sent', 'failed') AND created_at < $1
|
|
`
|
|
|
|
func (q *Queries) PurgeSubscriberNotifications(ctx context.Context, createdAt time.Time) (int64, error) {
|
|
result, err := q.db.Exec(ctx, purgeSubscriberNotifications, createdAt)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const purgeUnconfirmedSubscribers = `-- name: PurgeUnconfirmedSubscribers :execrows
|
|
DELETE FROM status_page_subscribers WHERE confirmed_at IS NULL AND created_at < $1
|
|
`
|
|
|
|
// Unconfirmed subscriptions are kept for as long as they count towards the
|
|
// weekly limit, so letting them lapse frees no room for more.
|
|
func (q *Queries) PurgeUnconfirmedSubscribers(ctx context.Context, createdAt time.Time) (int64, error) {
|
|
result, err := q.db.Exec(ctx, purgeUnconfirmedSubscribers, createdAt)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const subscriberNotificationDetails = `-- name: SubscriberNotificationDetails :many
|
|
SELECT n.id, n.event, n.attempts,
|
|
s.id AS subscriber_id, s.email, s.locale, s.page_url,
|
|
p.id AS page_id, p.slug, p.title AS page_title, p.custom_domain, p.domain_verified_at,
|
|
EXISTS (SELECT 1 FROM agents a WHERE a.org_id = p.org_id AND a.kind = 'statuspage' AND a.last_seen_at IS NOT NULL) AS own_servers,
|
|
i.started_at, i.resolved_at, i.title AS incident_title,
|
|
coalesce(nullif(spm.label, ''), m.name)::text AS component,
|
|
coalesce(u.status, '')::text AS update_status, coalesce(st.name, '')::text AS update_status_name,
|
|
coalesce(u.message, '')::text AS update_message
|
|
FROM subscriber_notifications n
|
|
JOIN status_page_subscribers s ON s.id = n.subscriber_id
|
|
JOIN status_pages p ON p.id = s.page_id
|
|
JOIN incidents i ON i.id = n.incident_id
|
|
JOIN monitors m ON m.id = i.monitor_id
|
|
LEFT JOIN status_page_monitors spm ON spm.page_id = p.id AND spm.monitor_id = m.id
|
|
LEFT JOIN incident_updates u ON u.id = n.update_id
|
|
LEFT JOIN incident_statuses st ON st.id = u.status_id
|
|
WHERE n.id = ANY ($1::uuid[])
|
|
`
|
|
|
|
type SubscriberNotificationDetailsRow struct {
|
|
ID uuid.UUID
|
|
Event string
|
|
Attempts int32
|
|
SubscriberID uuid.UUID
|
|
Email string
|
|
Locale string
|
|
PageUrl string
|
|
PageID uuid.UUID
|
|
Slug string
|
|
PageTitle string
|
|
CustomDomain *string
|
|
DomainVerifiedAt *time.Time
|
|
OwnServers bool
|
|
StartedAt time.Time
|
|
ResolvedAt *time.Time
|
|
IncidentTitle string
|
|
Component string
|
|
UpdateStatus string
|
|
UpdateStatusName string
|
|
UpdateMessage string
|
|
}
|
|
|
|
// System scope: what a mail to a subscriber says.
|
|
func (q *Queries) SubscriberNotificationDetails(ctx context.Context, ids []uuid.UUID) ([]SubscriberNotificationDetailsRow, error) {
|
|
rows, err := q.db.Query(ctx, subscriberNotificationDetails, ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []SubscriberNotificationDetailsRow{}
|
|
for rows.Next() {
|
|
var i SubscriberNotificationDetailsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Event,
|
|
&i.Attempts,
|
|
&i.SubscriberID,
|
|
&i.Email,
|
|
&i.Locale,
|
|
&i.PageUrl,
|
|
&i.PageID,
|
|
&i.Slug,
|
|
&i.PageTitle,
|
|
&i.CustomDomain,
|
|
&i.DomainVerifiedAt,
|
|
&i.OwnServers,
|
|
&i.StartedAt,
|
|
&i.ResolvedAt,
|
|
&i.IncidentTitle,
|
|
&i.Component,
|
|
&i.UpdateStatus,
|
|
&i.UpdateStatusName,
|
|
&i.UpdateMessage,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const touchPendingSubscriber = `-- name: TouchPendingSubscriber :exec
|
|
UPDATE status_page_subscribers SET locale = $2, page_url = $3
|
|
WHERE id = $1 AND confirmed_at IS NULL
|
|
`
|
|
|
|
type TouchPendingSubscriberParams struct {
|
|
ID uuid.UUID
|
|
Locale string
|
|
PageUrl string
|
|
}
|
|
|
|
// Keeps a pending subscription's language and link current when the
|
|
// visitor asks again.
|
|
func (q *Queries) TouchPendingSubscriber(ctx context.Context, arg TouchPendingSubscriberParams) error {
|
|
_, err := q.db.Exec(ctx, touchPendingSubscriber, arg.ID, arg.Locale, arg.PageUrl)
|
|
return err
|
|
}
|