diff --git a/src/Hydrogen.ts b/src/Hydrogen.ts index 6a6de72d304e2e243ff55a1e544fb08bff3753e2..697fcfe7f4e3398a4f67d165d601a4fc6a6e5548 100644 --- a/src/Hydrogen.ts +++ b/src/Hydrogen.ts @@ -46,12 +46,11 @@ export class Hydrogen { } } - async mountRoom(roomId: string) { - const session = this._client.session; - const room = session.rooms.get(roomId); + async showRoom(roomId: string) { + const room = this._session.rooms.get(roomId) ?? await this._joinRoom(roomId); const vm = new RoomViewModel({ room, - ownUserId: session.userId, + ownUserId: this._session.userId, platform: this._platform, urlCreator: this._urlRouter, navigation: this._navigation, @@ -60,4 +59,29 @@ export class Hydrogen { const view = new TimelineView(vm.timelineViewModel); this._container.appendChild(view.mount()); } + + private async _joinRoom(roomId: string): Promise<any> { + await this._session.joinRoom(roomId); + // even though we've joined the room, we need to wait till the next sync for the actual room + await this._waitForRoomFromSync(roomId); + return this._session.rooms.get(roomId); + + } + + private _waitForRoomFromSync(roomId: string): Promise<void> { + let resolve: () => void; + const promise: Promise<void> = new Promise(r => { resolve = r; }) + const subscription = { + onAdd: (_: string, value: {id: string}) => { + if (value.id === roomId) { + this._session.rooms.unsubscribe(subscription); + resolve(); + } + }, + }; + this._session.rooms.subscribe(subscription); + return promise; + } + + private get _session() { return this._client.session; } } diff --git a/src/main.ts b/src/main.ts index 8dc447d64e47e2612107e3b16de688ec84c1f357..08b8d87cab0592f9ca9e14ba84127be97f3d0323 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,7 +18,7 @@ async function fetchConfig(): Promise<IChatterboxConfig> { async function main() { const root = document.querySelector("#chatterbox") as HTMLDivElement; - const { homeserver } = await fetchConfig(); + const { homeserver, auto_join_room } = await fetchConfig(); const hydrogen = new Hydrogen(homeserver, root); const username = generateRandomString(7); const password = generateRandomString(10); @@ -28,8 +28,9 @@ async function main() { console.log("Attempting to login with same credentials"); await hydrogen.login(username, password); console.log("Login successful"); - - + console.log("Attempting to mount Timeline"); + await hydrogen.showRoom(auto_join_room); + console.log("Mounted Timeline"); } function generateRandomString(length: number): string {