fix: prevent duplicate outreach and follow-ups

This commit is contained in:
jjbrunton committed 2026-08-09 07:48:39 +01:00
1 parent acc610c2bf
commit 5c01d7d9aa
9 files changed
+274 -14

No files matched your search

+34
View File
@@ -217,6 +217,40 @@ export function runMigrations(db: Database.Database): void {
);
`);
},
() => db.exec(`
CREATE INDEX IF NOT EXISTS idx_reddit_conversations_global_username
ON reddit_conversations(username COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS idx_sent_messages_global_recipient
ON sent_messages(recipient COLLATE NOCASE);
INSERT OR IGNORE INTO recipient_suppressions (recipient, reason, created_at)
SELECT recipient, 'sent-message-history', MIN(sent_at)
FROM sent_messages
WHERE trim(recipient) <> ''
GROUP BY recipient COLLATE NOCASE;
INSERT OR IGNORE INTO recipient_suppressions (recipient, reason, created_at)
SELECT rc.username, 'historical-conversation', MIN(rc.updated_at)
FROM reddit_conversations rc
WHERE trim(rc.username) <> ''
AND lower(trim(rc.username)) NOT IN ('unknown', '[deleted]')
AND lower(trim(rc.username)) NOT LIKE 't2\\_%' ESCAPE '\\'
AND trim(rc.username) NOT LIKE '@%'
GROUP BY rc.username COLLATE NOCASE;
CREATE TABLE conversation_automation_claims (
account_id TEXT NOT NULL,
conversation_id TEXT NOT NULL,
incoming_key TEXT NOT NULL,
status TEXT NOT NULL CHECK(status IN ('pending', 'sent')),
claimed_at INTEGER NOT NULL,
sent_at INTEGER,
PRIMARY KEY (account_id, conversation_id, incoming_key),
FOREIGN KEY (account_id) REFERENCES reddit_accounts(account_id) ON DELETE CASCADE
);
CREATE INDEX idx_conversation_automation_claims_status
ON conversation_automation_claims(account_id, status, claimed_at);
`),
];
let currentVersion = db.pragma('user_version', { simple: true }) as number;