Bind Reddit and Matrix session identities

This commit is contained in:
jjbrunton committed 2026-08-08 19:43:35 +01:00
1 parent 34ae532cdd
commit 492efe0f8a
12 files changed
+234 -55

No files matched your search

@@ -1,5 +1,26 @@
const REDDITBOT_SESSION_FILENAME = 'redditbot-session.json';
export function normalizeRedditAccountId(accountId) {
const trimmed = String(accountId ?? '').trim();
const normalized = trimmed.startsWith('t2_') ? trimmed : `t2_${trimmed}`;
if (!/^t2_[a-z0-9]+$/i.test(normalized)) {
throw new Error('Reddit returned an invalid account ID.');
}
return normalized;
}
export function assertMatchingSessionIdentity(redditIdentity, matrixUserId) {
const accountId = normalizeRedditAccountId(redditIdentity?.accountId);
const username = String(redditIdentity?.username ?? '').trim();
if (!username) throw new Error('Reddit did not return an authenticated username.');
const expectedMatrixUserId = `@${accountId}:reddit.com`;
if (matrixUserId !== expectedMatrixUserId) {
throw new Error(
`Reddit is logged in as u/${username} (${accountId}), but Chat is authenticated as ${matrixUserId}. Reload Reddit Chat after switching accounts, then export again.`,
);
}
}
function isRedditDomain(domain) {
const normalised = domain.toLowerCase().replace(/^\./, '');
return normalised === 'reddit.com' || normalised.endsWith('.reddit.com');