Skip to content
Snippets Groups Projects
Commit 5fdef677 authored by Midhun Suresh's avatar Midhun Suresh
Browse files

Implement room join after registration

parent 8e187b5b
No related branches found
No related tags found
No related merge requests found
......@@ -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; }
}
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment