| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
ALTER TABLE lists DROP CONSTRAINT IF EXISTS lists_kind_check; |
| 12 |
ALTER TABLE lists ADD CONSTRAINT lists_kind_check CHECK (kind IN ( |
| 13 |
'content', 'devlog', 'patches', 'releases', |
| 14 |
'issues', 'announce', 'marketing', |
| 15 |
'sale', 'follower', 'login', 'status', 'tip', |
| 16 |
'invite' |
| 17 |
)); |
| 18 |
|
| 19 |
INSERT INTO lists (scope, scope_id, kind, title, required, owner_id) VALUES |
| 20 |
('platform', NULL, 'invite', 'Invite codes', FALSE, NULL) |
| 21 |
ON CONFLICT DO NOTHING; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
INSERT INTO list_subscriptions (list_id, user_id, state, source, created_at) |
| 28 |
SELECT l.id, u.id, 'confirmed', 'import', u.created_at |
| 29 |
FROM users u |
| 30 |
CROSS JOIN lists l |
| 31 |
WHERE l.scope = 'platform' AND l.kind = 'invite' |
| 32 |
ON CONFLICT DO NOTHING; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
INSERT INTO consent_events (subscription_id, event, at, evidence) |
| 37 |
SELECT ls.id, |
| 38 |
'import', |
| 39 |
ls.created_at, |
| 40 |
'Carried over when invite-redeemed notices stopped being operational mail. ' |
| 41 |
|| 'The account was already receiving these and could not decline them, so ' |
| 42 |
|| 'the subscription starts confirmed. No opt-in was recorded because none ' |
| 43 |
|| 'was ever asked for.' |
| 44 |
FROM list_subscriptions ls |
| 45 |
JOIN lists l ON l.id = ls.list_id |
| 46 |
WHERE l.scope = 'platform' |
| 47 |
AND l.kind = 'invite' |
| 48 |
AND ls.source = 'import' |
| 49 |
AND NOT EXISTS ( |
| 50 |
SELECT 1 FROM consent_events ce |
| 51 |
WHERE ce.subscription_id = ls.id AND ce.event = 'import' |
| 52 |
); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
CREATE OR REPLACE FUNCTION seed_notification_subscriptions() RETURNS TRIGGER AS $$ |
| 61 |
BEGIN |
| 62 |
WITH prefs(kind, enabled) AS ( |
| 63 |
VALUES |
| 64 |
('sale', TRUE), |
| 65 |
('follower', TRUE), |
| 66 |
('releases', TRUE), |
| 67 |
('issues', TRUE), |
| 68 |
|
| 69 |
('status', FALSE), |
| 70 |
('tip', TRUE), |
| 71 |
('login', TRUE), |
| 72 |
|
| 73 |
|
| 74 |
('invite', TRUE) |
| 75 |
), |
| 76 |
inserted AS ( |
| 77 |
INSERT INTO list_subscriptions (list_id, user_id, state, source, unsubscribed_at) |
| 78 |
SELECT l.id, |
| 79 |
NEW.id, |
| 80 |
CASE WHEN prefs.enabled THEN 'confirmed' ELSE 'unsubscribed' END, |
| 81 |
'admin', |
| 82 |
CASE WHEN prefs.enabled THEN NULL ELSE NOW() END |
| 83 |
FROM prefs |
| 84 |
JOIN lists l ON l.scope = 'platform' AND l.kind = prefs.kind |
| 85 |
ON CONFLICT DO NOTHING |
| 86 |
RETURNING id, state |
| 87 |
) |
| 88 |
INSERT INTO consent_events (subscription_id, event, evidence) |
| 89 |
SELECT inserted.id, |
| 90 |
CASE WHEN inserted.state = 'confirmed' THEN 'opt_in' ELSE 'opt_out' END, |
| 91 |
'Account default at signup, not an explicit choice by the account holder.' |
| 92 |
FROM inserted; |
| 93 |
|
| 94 |
RETURN NEW; |
| 95 |
END; |
| 96 |
$$ LANGUAGE plpgsql; |
| 97 |
|