Feature 19: Notifications - Web Push and Google Chat (#169)
CI / ci (push) Successful in 8m31s
CI / ci (push) Successful in 8m31s
Co-authored-by: orchestrator-agent <20+orchestrator-agent@code.cubecraftcreations.com>
This commit was merged in pull request #169.
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
-- 0017_shopping_list_chat_send_unconfirmed: a send of a Supply run to
|
||||
-- Google Chat is recorded as begun, in chat_send_unconfirmed_at, before it
|
||||
-- is posted, and cleared when Google Chat's answer is known: with
|
||||
-- sent_to_chat_at when it accepted the post, alone when it refused it. A
|
||||
-- value left behind (Google Chat did not answer, the process stopped, or
|
||||
-- recording the answer failed) means the post may or may not have been
|
||||
-- made, so it is never posted again without resend. Existing rows have no
|
||||
-- send under way.
|
||||
|
||||
ALTER TABLE shopping_lists ADD COLUMN chat_send_unconfirmed_at TEXT;
|
||||
@@ -0,0 +1,68 @@
|
||||
-- name: GetNotificationPref :one
|
||||
SELECT * FROM notification_prefs
|
||||
WHERE event = ?;
|
||||
|
||||
-- name: InsertNotification :execrows
|
||||
-- Stores a pending notification for one channel, due at once. A row already
|
||||
-- stored for the event key and channel, in any status, is left unchanged and
|
||||
-- no row is inserted.
|
||||
INSERT INTO notification_outbox (
|
||||
id, event_key, channel, payload, status, attempts, next_attempt_at, created_at, updated_at
|
||||
) VALUES (
|
||||
sqlc.arg(id), sqlc.arg(event_key), sqlc.arg(channel), sqlc.arg(payload), 'pending', 0, sqlc.arg(next_attempt_at), sqlc.arg(now), sqlc.arg(now)
|
||||
)
|
||||
ON CONFLICT (event_key, channel) DO NOTHING;
|
||||
|
||||
-- name: ClaimDueNotification :one
|
||||
-- Leases the pending notification due longest ago by moving its
|
||||
-- next_attempt_at to lease_until, and returns it. A claimed row is not due
|
||||
-- again until the lease ends, so no other claim takes it while it is being
|
||||
-- delivered. Returns sql.ErrNoRows when none is due.
|
||||
UPDATE notification_outbox
|
||||
SET next_attempt_at = sqlc.arg(lease_until), updated_at = sqlc.arg(now)
|
||||
WHERE id = (
|
||||
SELECT id FROM notification_outbox
|
||||
WHERE status = 'pending' AND next_attempt_at <= sqlc.arg(now)
|
||||
ORDER BY next_attempt_at, id
|
||||
LIMIT 1
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: MarkNotificationSent :execrows
|
||||
-- Records a delivered notification; updated_at is when it was sent. The
|
||||
-- lease must still be the claim's.
|
||||
UPDATE notification_outbox
|
||||
SET status = 'sent', attempts = attempts + 1, next_attempt_at = NULL, updated_at = sqlc.arg(now)
|
||||
WHERE id = sqlc.arg(id) AND status = 'pending' AND next_attempt_at = sqlc.arg(lease_until);
|
||||
|
||||
-- name: RetryNotification :execrows
|
||||
-- Records a failed delivery and when to try again. The lease must still be
|
||||
-- the claim's.
|
||||
UPDATE notification_outbox
|
||||
SET attempts = attempts + 1, next_attempt_at = sqlc.arg(next_attempt_at), last_error = sqlc.arg(last_error), updated_at = sqlc.arg(now)
|
||||
WHERE id = sqlc.arg(id) AND status = 'pending' AND next_attempt_at = sqlc.arg(lease_until);
|
||||
|
||||
-- name: FailNotification :execrows
|
||||
-- Gives up on a notification. attempts counts the delivery attempt that
|
||||
-- failed, if one was made. The lease must still be the claim's.
|
||||
UPDATE notification_outbox
|
||||
SET status = 'failed', attempts = attempts + sqlc.arg(attempted), next_attempt_at = NULL, last_error = sqlc.arg(last_error), updated_at = sqlc.arg(now)
|
||||
WHERE id = sqlc.arg(id) AND status = 'pending' AND next_attempt_at = sqlc.arg(lease_until);
|
||||
|
||||
-- name: NextNotificationDue :one
|
||||
-- Returns when the next pending notification is due. Returns sql.ErrNoRows
|
||||
-- when none is pending.
|
||||
SELECT next_attempt_at FROM notification_outbox
|
||||
WHERE status = 'pending'
|
||||
ORDER BY next_attempt_at
|
||||
LIMIT 1;
|
||||
|
||||
-- name: ListNotificationPrefs :many
|
||||
SELECT * FROM notification_prefs;
|
||||
|
||||
-- name: UpdateNotificationPref :execrows
|
||||
-- Sets which channels an event goes to; affects no row when the event has
|
||||
-- no preference row.
|
||||
UPDATE notification_prefs
|
||||
SET push = ?, chat = ?, updated_at = ?
|
||||
WHERE event = ?;
|
||||
@@ -0,0 +1,44 @@
|
||||
-- name: UpsertPushSubscription :one
|
||||
-- Stores a browser's push subscription, keyed by its endpoint. Subscribing
|
||||
-- an endpoint already stored replaces its keys and user agent and clears
|
||||
-- its failure count, keeping its id, created_at and last_success_at.
|
||||
INSERT INTO push_subscriptions (
|
||||
id, endpoint, p256dh, auth, user_agent, failure_count, created_at, updated_at
|
||||
) VALUES (
|
||||
?, ?, ?, ?, ?, 0, ?, ?
|
||||
)
|
||||
ON CONFLICT (endpoint) DO UPDATE SET
|
||||
p256dh = excluded.p256dh,
|
||||
auth = excluded.auth,
|
||||
user_agent = excluded.user_agent,
|
||||
failure_count = 0,
|
||||
updated_at = excluded.updated_at
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeletePushSubscription :execrows
|
||||
DELETE FROM push_subscriptions
|
||||
WHERE endpoint = ?;
|
||||
|
||||
-- name: ListPushSubscriptions :many
|
||||
-- Every stored subscription with its keys, for the push sender, oldest
|
||||
-- first.
|
||||
SELECT id, endpoint, p256dh, auth
|
||||
FROM push_subscriptions
|
||||
ORDER BY created_at, id;
|
||||
|
||||
-- name: RecordPushSuccess :exec
|
||||
-- Records that the push service accepted a notification for a subscription.
|
||||
UPDATE push_subscriptions
|
||||
SET last_success_at = ?
|
||||
WHERE id = ?;
|
||||
|
||||
-- name: RecordPushFailure :exec
|
||||
-- Counts a delivery to a subscription that the push service did not accept.
|
||||
UPDATE push_subscriptions
|
||||
SET failure_count = failure_count + 1
|
||||
WHERE id = ?;
|
||||
|
||||
-- name: DeletePushSubscriptionByID :exec
|
||||
-- Removes a subscription the push service reports gone (404 or 410).
|
||||
DELETE FROM push_subscriptions
|
||||
WHERE id = ?;
|
||||
@@ -146,3 +146,11 @@ ON CONFLICT (id) DO UPDATE SET
|
||||
last_test_status = excluded.last_test_status,
|
||||
updated_at = excluded.updated_at
|
||||
RETURNING *;
|
||||
|
||||
-- name: RecordChatTest :execrows
|
||||
-- Records the outcome of a test message sent to the webhook sealed in
|
||||
-- webhook_url_ct; affects no row when the webhook has been replaced or
|
||||
-- removed since the test began.
|
||||
UPDATE chat_settings
|
||||
SET last_test_at = ?, last_test_status = ?, updated_at = ?
|
||||
WHERE id = 1 AND webhook_url_ct = ?;
|
||||
|
||||
@@ -89,6 +89,27 @@ UPDATE shopping_lists
|
||||
SET title = ?, updated_at = ?
|
||||
WHERE id = ? AND title IS NULL AND status = 'current' AND frozen_at IS NULL;
|
||||
|
||||
-- name: BeginShoppingListChatSend :execrows
|
||||
-- Records a send of list id to Google Chat begun at
|
||||
-- chat_send_unconfirmed_at, before it is posted, unless the list is frozen
|
||||
-- into the Supply archive.
|
||||
UPDATE shopping_lists
|
||||
SET chat_send_unconfirmed_at = ?, updated_at = ?
|
||||
WHERE id = ? AND status = 'current' AND frozen_at IS NULL;
|
||||
|
||||
-- name: MarkShoppingListSentToChat :execrows
|
||||
-- Records list id sent to Google Chat at sent_to_chat_at, its send
|
||||
-- confirmed, unless it is frozen into the Supply archive.
|
||||
UPDATE shopping_lists
|
||||
SET sent_to_chat_at = ?, chat_send_unconfirmed_at = NULL, updated_at = ?
|
||||
WHERE id = ? AND status = 'current' AND frozen_at IS NULL;
|
||||
|
||||
-- name: ClearShoppingListChatSend :execrows
|
||||
-- Clears the send of list id begun, once Google Chat refused it.
|
||||
UPDATE shopping_lists
|
||||
SET chat_send_unconfirmed_at = NULL, updated_at = ?
|
||||
WHERE id = ?;
|
||||
|
||||
-- name: FreezeShoppingList :execrows
|
||||
-- Freezes list id into the Supply archive at frozen_at, unless it is
|
||||
-- frozen already.
|
||||
|
||||
@@ -26,6 +26,11 @@ WHERE kind = sqlc.arg(kind)
|
||||
ORDER BY id
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetSuggestionBatch :one
|
||||
-- Batch id. Returns sql.ErrNoRows when there is none.
|
||||
SELECT * FROM suggestion_batches
|
||||
WHERE id = ?;
|
||||
|
||||
-- name: StartSuggestionBatch :execrows
|
||||
-- Marks a pending batch running; a batch in any other status is left as
|
||||
-- it is (0 rows).
|
||||
|
||||
Reference in New Issue
Block a user