Retry Matrix capture after chat login redirect
This commit is contained in:
1 parent
bffda95251
commit
d2b3a1b31d
2 files changed
+55
-7
No files matched your search
@@ -2,7 +2,10 @@ import type { BrowserContext } from 'playwright';
|
||||
import type { Logger } from '../../../utils/logger.js';
|
||||
import type { TokenRefresher } from '../service.js';
|
||||
import type { MatrixCredentials } from './types.js';
|
||||
import { extractMatrixCredentials } from './token-extractor.js';
|
||||
import {
|
||||
extractMatrixCredentials,
|
||||
MatrixBrowserLoginRequiredError,
|
||||
} from './token-extractor.js';
|
||||
|
||||
export interface MatrixSessionBrowser {
|
||||
getContext(): BrowserContext | null;
|
||||
@@ -50,12 +53,10 @@ export class MatrixTokenRefresher implements TokenRefresher {
|
||||
|
||||
private async performRefresh(): Promise<void> {
|
||||
const sessionValid = await this.browser.isSessionValid();
|
||||
let sessionRestored = false;
|
||||
if (!sessionValid) {
|
||||
this.logger.warn('[matrix] Reddit browser session expired; attempting login recovery');
|
||||
if (!(await this.browser.login())) {
|
||||
throw new MatrixRemoteLoginRequiredError();
|
||||
}
|
||||
this.logger.info('[matrix] Reddit browser session restored');
|
||||
await this.restoreSession();
|
||||
sessionRestored = true;
|
||||
}
|
||||
|
||||
const context = this.browser.getContext();
|
||||
@@ -63,7 +64,34 @@ export class MatrixTokenRefresher implements TokenRefresher {
|
||||
throw new Error('Matrix token refresh requires an initialized browser context');
|
||||
}
|
||||
|
||||
const credentials = await this.extract(context, this.logger);
|
||||
let credentials: MatrixCredentials;
|
||||
try {
|
||||
credentials = await this.extract(context, this.logger);
|
||||
} catch (error) {
|
||||
if (!(error instanceof MatrixBrowserLoginRequiredError)) throw error;
|
||||
if (sessionRestored) throw new MatrixRemoteLoginRequiredError();
|
||||
|
||||
this.logger.warn(
|
||||
'[matrix] Reddit API session looked valid but chat requires login; attempting recovery',
|
||||
);
|
||||
await this.restoreSession();
|
||||
try {
|
||||
credentials = await this.extract(context, this.logger);
|
||||
} catch (retryError) {
|
||||
if (retryError instanceof MatrixBrowserLoginRequiredError) {
|
||||
throw new MatrixRemoteLoginRequiredError();
|
||||
}
|
||||
throw retryError;
|
||||
}
|
||||
}
|
||||
this.applyCredentials(credentials);
|
||||
}
|
||||
|
||||
private async restoreSession(): Promise<void> {
|
||||
this.logger.warn('[matrix] Reddit browser session expired; attempting login recovery');
|
||||
if (!(await this.browser.login())) {
|
||||
throw new MatrixRemoteLoginRequiredError();
|
||||
}
|
||||
this.logger.info('[matrix] Reddit browser session restored');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user