From e3ad0a309a6fb06d583756a928ad47e25c596a99 Mon Sep 17 00:00:00 2001 From: Ashfame Date: Wed, 1 Mar 2023 16:01:41 +0400 Subject: [PATCH] make resolveRoomAlias() work without a session --- frontend/iframe/viewmodels/RootViewModel.ts | 28 ++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/frontend/iframe/viewmodels/RootViewModel.ts b/frontend/iframe/viewmodels/RootViewModel.ts index dda5f70..4e61ae3 100644 --- a/frontend/iframe/viewmodels/RootViewModel.ts +++ b/frontend/iframe/viewmodels/RootViewModel.ts @@ -8,6 +8,7 @@ import { Options as BaseOptions, ViewModel } from "hydrogen-web/src/domain/ViewM import { Client } from "hydrogen-web/src/matrix/Client.js"; import { HomeServerApi } from "hydrogen-web/src/matrix/net/HomeServerApi"; import { allSections, Section } from "../platform/Navigation"; +import { lookupHomeserver } from "hydrogen-web/src/matrix/well-known"; import { Platform } from "../platform/Platform"; import { SessionViewModel } from "./SessionViewModel"; @@ -120,7 +121,7 @@ export class RootViewModel extends ViewModel { if (this._singleRoomIdOrAlias && !this._resolvedSingleRoomId) { // We're in single-room mode but haven't resolved the room alias yet. try { - this._resolvedSingleRoomId = await this.resolveRoomAlias(sessionId, this._singleRoomIdOrAlias); + this._resolvedSingleRoomId = await this.resolveRoomAlias(this._singleRoomIdOrAlias, sessionId); } catch (error) { // Something went wrong when navigating to the room. // We swallow the error and fallback to non-single-room mode. @@ -173,20 +174,29 @@ export class RootViewModel extends ViewModel { } } - private async resolveRoomAlias(sessionId: string, roomIdOrAlias: string): Promise { - const sessionInfo = await this.platform.sessionInfoStorage.get(sessionId); - if (!sessionInfo) { - throw new Error(`Could not find session for id ${sessionId}`); - } - + private async resolveRoomAlias(roomIdOrAlias: string, sessionId?: string): Promise { if (roomIdOrAlias.startsWith('!')) { return roomIdOrAlias; } + let sessionInfo; + if (sessionId) { + sessionInfo = await this.platform.sessionInfoStorage.get(sessionId); + } + let homeserver: string; + let accessToken: string; + if (sessionInfo) { + homeserver = sessionInfo.homeserver; + accessToken = sessionInfo.accessToken; + } else { + homeserver = await lookupHomeserver(roomIdOrAlias.split(':')[1], this.platform.request); + accessToken = ''; + } + const homeserverApi = new HomeServerApi({ - homeserver: sessionInfo.homeserver, + homeserver: homeserver, request: this.platform.request, - accessToken: sessionInfo.accessToken, + accessToken: accessToken, reconnector: this.platform.reconnector, });