-- name: CountRecentSubscribers :one -- Subscriptions the organization's pages gained since $2, confirmed or -- not, for the weekly limit. SELECT count(*) FROM status_page_subscribers WHERE org_id = $1 AND created_at > $2; -- name: GetPageSubscriberByEmail :one SELECT * FROM status_page_subscribers WHERE page_id = $1 AND email = $2; -- name: CreatePageSubscriber :one INSERT INTO status_page_subscribers (org_id, page_id, email, locale, page_url) VALUES ($1, $2, $3, $4, $5) RETURNING *; -- name: TouchPendingSubscriber :exec -- Keeps a pending subscription's language and link current when the -- visitor asks again. UPDATE status_page_subscribers SET locale = $2, page_url = $3 WHERE id = $1 AND confirmed_at IS NULL; -- name: GetPageSubscriber :one SELECT s.*, p.title AS page_title FROM status_page_subscribers s JOIN status_pages p ON p.id = s.page_id WHERE s.id = $1; -- name: ConfirmPageSubscriber :exec UPDATE status_page_subscribers SET confirmed_at = coalesce(confirmed_at, now()) WHERE id = $1; -- name: DeletePageSubscriber :execrows DELETE FROM status_page_subscribers WHERE id = $1 AND page_id = $2; -- name: ListPageSubscribers :many -- The newest subscribers of a page first. SELECT id, email, locale, created_at, confirmed_at FROM status_page_subscribers WHERE page_id = $1 ORDER BY created_at DESC, id LIMIT sqlc.arg(max_rows); -- 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; -- name: EnqueueSubscriberNotifications :execrows -- Queues the event of incident for every confirmed subscriber of the -- published pages showing its monitor that take subscriptions. INSERT INTO subscriber_notifications (org_id, subscriber_id, incident_id, update_id, event) SELECT s.org_id, s.id, i.id, sqlc.narg(update_id)::uuid, sqlc.arg(event) 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 = sqlc.arg(incident_id) AND s.confirmed_at IS NOT NULL AND p.published AND p.subscriptions AND p.suspended_at IS NULL ON CONFLICT DO NOTHING; -- name: ClaimSubscriberNotifications :many -- System scope; leased like ClaimNotifications. 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 sqlc.arg(max) FOR UPDATE SKIP LOCKED ) RETURNING id; -- name: SubscriberNotificationDetails :many -- System scope: what a mail to a subscriber says. 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 (sqlc.arg(ids)::uuid[]); -- name: MarkSubscriberNotificationSent :exec UPDATE subscriber_notifications SET status = 'sent', sent_at = now(), attempts = attempts + 1, last_error = NULL WHERE id = $1; -- name: MarkSubscriberNotificationFailed :exec UPDATE subscriber_notifications SET attempts = attempts + 1, last_error = sqlc.arg(last_error), status = CASE WHEN attempts + 1 >= sqlc.arg(max_attempts)::int THEN 'failed' ELSE 'pending' END, next_attempt_at = now() + make_interval(secs => sqlc.arg(retry_in_seconds)::int) WHERE id = sqlc.arg(id); -- name: PurgeUnconfirmedSubscribers :execrows -- Unconfirmed subscriptions are kept for as long as they count towards the -- weekly limit, so letting them lapse frees no room for more. DELETE FROM status_page_subscribers WHERE confirmed_at IS NULL AND created_at < $1; -- name: PurgeSubscriberNotifications :execrows DELETE FROM subscriber_notifications WHERE status IN ('sent', 'failed') AND created_at < $1;