Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ See [`docs/versioning.md`](docs/versioning.md) for what the numbers mean and

Use this section for changes that are merged but not released yet.

**Operators: apply migrations `013` and `015`.** Nothing needs restarting. Apply
`015` before enabling the group archive in Vido.
**Operators: apply migrations `013`, `014` and `015`.** Nothing needs restarting.
Apply `015` before enabling the group archive in Vido. `vido_test` is not built
by these migrations: run `014` there by hand, granting to `vido_test` instead of
`vido_core`, before the test bot sends a broadcast.

### Added

Expand Down
88 changes: 88 additions & 0 deletions migrations/014_vido_broadcasts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
-- core :: 014 - Vido broadcasts: one durable outbox row per destination chat
-- The central migrator owns the transaction; do not add BEGIN/COMMIT here.
--
-- A broadcast goes to every private chat and group Vido has seen. Private chats
-- last seen before July 2026 were recorded with presence chat_id 0 and have no
-- core.chat row, so chat_id is a bare Telegram id here, not a foreign key; the
-- sign check below keeps private and group ids apart instead.
--
-- Same delivery contract as 007: a row handed to Telegram without an answer
-- becomes delivery_unknown and is never replayed. outcome records why a chat
-- was or was not reached, which is what the operator report counts. The
-- outcome checks spell out IS NOT NULL: a NULL inside IN (...) would make the
-- whole CHECK NULL, and PostgreSQL accepts a NULL check.

SET LOCAL lock_timeout = '5s';
SET LOCAL statement_timeout = '30s';

CREATE TABLE IF NOT EXISTS vido.broadcasts (
broadcast_key text PRIMARY KEY,
manifest_sha256 text NOT NULL,
photo_sha256 text,
photo_file_id text,
prepared_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CHECK (broadcast_key <> ''),
CHECK (manifest_sha256 ~ '^[0-9a-f]{64}$'),
CHECK (photo_sha256 IS NULL OR photo_sha256 ~ '^[0-9a-f]{64}$'),
CHECK (photo_file_id IS NULL OR photo_sha256 IS NOT NULL)
);

CREATE TABLE IF NOT EXISTS vido.broadcast_deliveries (
broadcast_key text NOT NULL REFERENCES vido.broadcasts(broadcast_key),
chat_id bigint NOT NULL,
chat_type text NOT NULL,
language text NOT NULL,
message_html text NOT NULL,
status text NOT NULL DEFAULT 'pending',
outcome text,
attempts integer NOT NULL DEFAULT 0,
next_attempt_at timestamptz NOT NULL DEFAULT now(),
lease_owner text,
lease_expires_at timestamptz,
last_error text,
delivered_chat_id bigint,
telegram_message_id bigint,
prepared_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
last_attempt_at timestamptz,
sent_at timestamptz,
PRIMARY KEY (broadcast_key, chat_id),
CHECK (chat_type IN ('private', 'group', 'supergroup')),
CHECK ((chat_type = 'private' AND chat_id > 0) OR (chat_type <> 'private' AND chat_id < 0)),
CHECK (language <> ''),
CHECK (message_html <> ''),
CHECK (status IN (
'pending', 'sending', 'sent', 'failed', 'unreachable', 'delivery_unknown'
)),
CHECK (attempts >= 0),
CHECK (
(status = 'sending' AND lease_owner IS NOT NULL AND lease_expires_at IS NOT NULL)
OR status <> 'sending'
),
CHECK (
(status = 'sent'
AND outcome IS NOT NULL
AND outcome IN ('delivered', 'text_only')
AND telegram_message_id IS NOT NULL
AND delivered_chat_id IS NOT NULL
AND sent_at IS NOT NULL)
OR status <> 'sent'
),
CHECK (
(status = 'unreachable' AND outcome IS NOT NULL AND outcome IN (
'blocked', 'deactivated', 'not_started', 'removed', 'no_rights', 'not_found'
))
OR status <> 'unreachable'
),
CHECK (status IN ('sent', 'unreachable') OR outcome IS NULL)
);

CREATE INDEX IF NOT EXISTS ix_broadcast_deliveries_ready
ON vido.broadcast_deliveries (broadcast_key, next_attempt_at, prepared_at)
WHERE status = 'pending';

CREATE INDEX IF NOT EXISTS ix_broadcast_deliveries_status
ON vido.broadcast_deliveries (broadcast_key, status);

GRANT SELECT, INSERT, UPDATE ON vido.broadcasts, vido.broadcast_deliveries TO vido_core;
135 changes: 135 additions & 0 deletions tests/014_vido_broadcasts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
\set ON_ERROR_STOP on

SET ROLE vido_core;
INSERT INTO vido.broadcasts(broadcast_key, manifest_sha256, photo_sha256)
VALUES ('test-broadcast', repeat('a', 64), repeat('b', 64));

-- A private chat with no core.chat row: the outbox must still accept it.
INSERT INTO vido.broadcast_deliveries(
broadcast_key, chat_id, chat_type, language, message_html
) VALUES
('test-broadcast', 7101, 'private', 'en', '<b>Test</b>'),
('test-broadcast', 7102, 'private', 'ru', '<b>Тест</b>'),
('test-broadcast', -1007103, 'supergroup', 'en', '<b>Test</b>');

UPDATE vido.broadcast_deliveries
SET status = 'sending',
attempts = attempts + 1,
lease_owner = 'test-worker',
lease_expires_at = now() + interval '1 minute',
last_attempt_at = now()
WHERE broadcast_key = 'test-broadcast' AND chat_id = 7101;

UPDATE vido.broadcast_deliveries
SET status = 'sent',
outcome = 'delivered',
delivered_chat_id = 7101,
telegram_message_id = 71001,
sent_at = now(),
lease_owner = NULL,
lease_expires_at = NULL
WHERE broadcast_key = 'test-broadcast' AND chat_id = 7101;

UPDATE vido.broadcast_deliveries
SET status = 'unreachable',
outcome = 'blocked',
attempts = 1,
last_error = 'Forbidden: bot was blocked by the user'
WHERE broadcast_key = 'test-broadcast' AND chat_id = 7102;

UPDATE vido.broadcasts
SET photo_file_id = 'photo-file-id'
WHERE broadcast_key = 'test-broadcast';

SELECT (
SELECT count(*) FROM vido.broadcast_deliveries
WHERE broadcast_key = 'test-broadcast'
AND ((chat_id = 7101 AND status = 'sent' AND outcome = 'delivered')
OR (chat_id = 7102 AND status = 'unreachable' AND outcome = 'blocked')
OR (chat_id = -1007103 AND status = 'pending' AND outcome IS NULL))
) = 3 AS lifecycle_recorded \gset
\if :lifecycle_recorded
\else
DO $$ BEGIN RAISE EXCEPTION 'contract failed: 014_vido_broadcasts.sql: expected lifecycle_recorded to be true'; END $$;
\endif

DO $$
BEGIN
INSERT INTO vido.broadcast_deliveries(
broadcast_key, chat_id, chat_type, language, message_html, status, outcome
) VALUES (
'test-broadcast', 7104, 'private', 'en', '<b>Invalid</b>', 'sent', 'delivered'
);
RAISE EXCEPTION 'sent broadcast without Telegram evidence was accepted';
EXCEPTION WHEN check_violation THEN
NULL;
END $$;

DO $$
BEGIN
INSERT INTO vido.broadcast_deliveries(
broadcast_key, chat_id, chat_type, language, message_html, status
) VALUES (
'test-broadcast', 7105, 'private', 'en', '<b>Invalid</b>', 'unreachable'
);
RAISE EXCEPTION 'unreachable broadcast without an outcome was accepted';
EXCEPTION WHEN check_violation THEN
NULL;
END $$;

DO $$
BEGIN
INSERT INTO vido.broadcast_deliveries(
broadcast_key, chat_id, chat_type, language, message_html, status,
delivered_chat_id, telegram_message_id, sent_at
) VALUES (
'test-broadcast', 7107, 'private', 'en', '<b>Invalid</b>', 'sent',
7107, 71007, now()
);
RAISE EXCEPTION 'sent broadcast without an outcome was accepted';
EXCEPTION WHEN check_violation THEN
NULL;
END $$;

DO $$
BEGIN
INSERT INTO vido.broadcast_deliveries(
broadcast_key, chat_id, chat_type, language, message_html
) VALUES (
'test-broadcast', -7106, 'private', 'en', '<b>Invalid</b>'
);
RAISE EXCEPTION 'private broadcast target with a group id was accepted';
EXCEPTION WHEN check_violation THEN
NULL;
END $$;
RESET ROLE;

SELECT has_table_privilege('vido_core', 'vido.broadcasts', 'SELECT,INSERT,UPDATE')
AND has_table_privilege('vido_core', 'vido.broadcast_deliveries', 'SELECT,INSERT,UPDATE')
AS vido_can_use_outbox \gset
\if :vido_can_use_outbox
\else
DO $$ BEGIN RAISE EXCEPTION 'contract failed: 014_vido_broadcasts.sql: expected vido_can_use_outbox to be true'; END $$;
\endif

SELECT NOT has_table_privilege('vido_core', 'vido.broadcast_deliveries', 'DELETE')
AS vido_cannot_erase_evidence \gset
\if :vido_cannot_erase_evidence
\else
DO $$ BEGIN RAISE EXCEPTION 'contract failed: 014_vido_broadcasts.sql: expected vido_cannot_erase_evidence to be true'; END $$;
\endif

SELECT NOT has_table_privilege('searchy_core', 'vido.broadcast_deliveries', 'SELECT')
AND NOT has_table_privilege('searchy_core', 'vido.broadcasts', 'SELECT')
AS searchy_isolated \gset
\if :searchy_isolated
\else
DO $$ BEGIN RAISE EXCEPTION 'contract failed: 014_vido_broadcasts.sql: expected searchy_isolated to be true'; END $$;
\endif

SELECT NOT has_table_privilege('public', 'vido.broadcast_deliveries', 'SELECT')
AS public_isolated \gset
\if :public_isolated
\else
DO $$ BEGIN RAISE EXCEPTION 'contract failed: 014_vido_broadcasts.sql: expected public_isolated to be true'; END $$;
\endif
Loading