Gate bot logic on full account authentication

This commit is contained in:
jjbrunton committed 2026-08-08 16:01:06 +01:00
1 parent 4db6de8bf5
commit 2fd92bf82c
22 files changed
+635 -65

No files matched your search

+22 -2
View File
@@ -12,6 +12,7 @@ import type { ActiveChat, ChatMessage, ScrapedChatMessage, InvitationCooldown }
export type { InvitationCooldown } from './types.js';
import { fetchWithTimeout } from '../../utils/fetch-with-timeout.js';
import type { AccountAuthGate } from '../account-auth-context.js';
const ACTIVE_WINDOW_MS = 96 * 60 * 60 * 1000;
const ROOM_URL_PREFIX = 'https://chat.reddit.com/room/';
@@ -129,6 +130,7 @@ export class ChatService {
cache?: ChatCache,
private messagesRepo?: MessagesRepository,
private redditUsersRepo?: RedditUsersRepository,
private accountAuth?: AccountAuthGate,
) {
this.cache = cache ?? new ChatCache(logger);
if (this.redditUsersRepo) {
@@ -176,6 +178,11 @@ export class ChatService {
* refresh itself fails, we propagate.
*/
private async withAuth<T>(fn: () => Promise<T>): Promise<T> {
if (!this.accountAuth?.isFullyAuthenticated()) {
throw new MatrixAuthenticationUnavailableError(
new Error('Full account authentication is required'),
);
}
try {
return await fn();
} catch (err) {
@@ -184,9 +191,15 @@ export class ChatService {
try {
await this.tokenRefresher.refresh();
} catch (refreshError) {
this.accountAuth?.invalidate('Matrix authentication could not be restored');
throw new MatrixAuthenticationUnavailableError(refreshError);
}
return await fn();
try {
return await fn();
} catch (retryError) {
this.accountAuth?.invalidate('Matrix authentication was rejected after refresh');
throw retryError;
}
}
throw err;
}
@@ -459,6 +472,7 @@ export class ChatService {
* `chat.messages`.
*/
async loadConversationDetails(chat: ActiveChat): Promise<void> {
if (!this.accountAuth?.isFullyAuthenticated()) return;
return this.loadDetails(chat);
}
@@ -468,6 +482,7 @@ export class ChatService {
* a raw Matrix room ID like `!abc:reddit.com`.
*/
async sendChatMessage(chatUrlOrRoomId: string, message: string): Promise<boolean> {
if (!this.accountAuth?.isFullyAuthenticated()) return false;
const active = this.getInvitationCooldown();
if (active) {
const secondsLeft = Math.ceil((active.until - Date.now()) / 1000);
@@ -508,6 +523,7 @@ export class ChatService {
* line. Returns the new chat (or null on failure).
*/
async startConversation(username: string, message: string): Promise<ActiveChat | null> {
if (!this.accountAuth?.isFullyAuthenticated()) return null;
const active = this.getInvitationCooldown();
if (active) {
const secondsLeft = Math.ceil((active.until - Date.now()) / 1000);
@@ -571,7 +587,7 @@ export class ChatService {
* old Playwright sender; always returns true.
*/
async verifyMessageSent(_recipient: string, _messageText: string): Promise<boolean> {
return true;
return this.accountAuth?.isFullyAuthenticated() ?? false;
}
/**
@@ -1026,6 +1042,10 @@ export class ChatService {
* cache. Best-effort; safe to await or fire-and-forget.
*/
async enrichUsernames(): Promise<void> {
if (!this.accountAuth?.isFullyAuthenticated()) {
this.logger.debug('[chat] username enrichment skipped until full account authentication');
return;
}
if (!this.usernameResolver) return;
let dirty = false;