From 5fdef677d088684966ada869fc571de31b8d50af Mon Sep 17 00:00:00 2001 From: Midhun Suresh <midhunr@element.io> Date: Thu, 30 Dec 2021 16:30:56 +0530 Subject: [PATCH] Implement room join after registration --- src/Hydrogen.ts | 32 ++++++++++++++++++++++++++++---- src/main.ts | 7 ++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Hydrogen.ts b/src/Hydrogen.ts index 6a6de72..697fcfe 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 8dc447d..08b8d87 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 { -- GitLab