From e1fbd1242e24c00fca042cb30715620659adfcf1 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 7 Feb 2022 16:30:44 +0100 Subject: [PATCH] WIP 4 --- .../rightpanel/MemberDetailsViewModel.js | 4 +- src/matrix/Session.js | 23 ++++++----- src/matrix/room/create.ts | 41 ++++++++----------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/domain/session/rightpanel/MemberDetailsViewModel.js b/src/domain/session/rightpanel/MemberDetailsViewModel.js index 6effb90c..b0573bfa 100644 --- a/src/domain/session/rightpanel/MemberDetailsViewModel.js +++ b/src/domain/session/rightpanel/MemberDetailsViewModel.js @@ -83,7 +83,7 @@ export class MemberDetailsViewModel extends ViewModel { } async openDirectMessage() { - const localId = await this._session.createRoom(RoomType.DirectMessage, undefined, undefined, undefined, [this.userId]); - this.navigation.push("room", localId); + const roomBeingCreated = await this._session.createRoom(RoomType.DirectMessage, undefined, undefined, undefined, [this.userId]); + this.navigation.push("room", roomBeingCreated.localId); } } diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 4a9b0bad..02635855 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -594,13 +594,13 @@ export class Session { createRoom(type, isEncrypted, explicitName, topic, invites, log = undefined) { return this._platform.logger.wrapOrRun(log, "create room", log => { - const localId = `room-being-created-${this._platform.random()}`; + const localId = `local-${Math.round(this._platform.random() * Math.MAX_SAFE_INTEGER)}`; const roomBeingCreated = new RoomBeingCreated(localId, type, isEncrypted, explicitName, topic, invites, this._roomsBeingCreatedUpdateCallback, this._mediaRepository, log); this._roomsBeingCreated.set(localId, roomBeingCreated); - log.wrapDetached("create room network", async log => { - roomBeingCreated.start(this._hsApi, log); + log.wrapDetached("create room network", log => { + return roomBeingCreated.start(this._hsApi, log); }); - return localId; + return roomBeingCreated; }); } @@ -691,10 +691,9 @@ export class Session { if (roomBeingCreated.roomId === roomId) { const observableStatus = this._observedRoomStatus.get(roomBeingCreated.localId); if (observableStatus) { - this._platform.logger.wrapOrRun(log, `replacing room being created`, log => { - log.set("localId", roomBeingCreated.localId) - .set("roomId", roomBeingCreated.roomId); - }); + log.log(`replacing room being created`) + .set("localId", roomBeingCreated.localId) + .set("roomId", roomBeingCreated.roomId); observableStatus.set(observableStatus.get() | RoomStatus.Replaced); } this._roomsBeingCreated.remove(roomBeingCreated.localId); @@ -737,10 +736,12 @@ export class Session { for (const is of inviteStates) { const statusObservable = this._observedRoomStatus.get(is.id); if (statusObservable) { + const withInvited = statusObservable.get() | RoomStatus.Invited; if (is.shouldAdd) { - statusObservable.set(statusObservable.get().withInvited()); + statusObservable.set(withInvited); } else if (is.shouldRemove) { - statusObservable.set(statusObservable.get().withoutInvited()); + const withoutInvited = withInvited ^ RoomStatus.Invited; + statusObservable.set(withoutInvited); } } } @@ -750,7 +751,7 @@ export class Session { _forgetArchivedRoom(roomId) { const statusObservable = this._observedRoomStatus.get(roomId); if (statusObservable) { - statusObservable.set(statusObservable.get() ^ RoomStatus.Archived); + statusObservable.set((statusObservable.get() | RoomStatus.Archived) ^ RoomStatus.Archived); } } diff --git a/src/matrix/room/create.ts b/src/matrix/room/create.ts index c42da436..83b68d3f 100644 --- a/src/matrix/room/create.ts +++ b/src/matrix/room/create.ts @@ -65,6 +65,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { public readonly isEncrypted: boolean; private _name: string; + private _error?: Error; constructor( public readonly localId: string, @@ -115,12 +116,12 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { if (this.isEncrypted) { options.initial_state = [createRoomEncryptionEvent()]; } - console.log("going to create the room now"); - const response = await hsApi.createRoom(options, {log}).response(); - this._roomId = response["room_id"]; - console.log("done creating the room now", this._roomId); - // TODO: somehow check in Session if we need to replace this with a joined room - // in case the room appears first in sync, and this request returns later + try { + const response = await hsApi.createRoom(options, {log}).response(); + this._roomId = response["room_id"]; + } catch (err) { + this._error = err; + } this.emitChange(undefined, log); } @@ -128,39 +129,30 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { // only load profiles if we need it for the room name and avatar if (!this.explicitName && this.inviteUserIds) { this.profiles = await loadProfiles(this.inviteUserIds, hsApi, log); - console.log("loaded the profiles", this.profiles); const summaryData = { joinCount: 1, // ourselves inviteCount: this.inviteUserIds.length }; this._name = calculateRoomName(this.profiles, summaryData, log); - console.log("loaded the profiles and the new name", this.name); this.emitChange(); } } - private emitChange(params?, log?) { + private emitChange(params?, log?: ILogItem) { this.updateCallback(this, params, log); this.emit("change"); } - get avatarColorId(): string { - return this.inviteUserIds?.[0] ?? this._roomId ?? this.localId; - } - - get avatarUrl(): string | undefined { - const result = this.profiles[0]?.avatarUrl; - console.log("RoomBeingCreated.avatarUrl", this.profiles, result); - return result; - } - - get roomId(): string | undefined { - return this._roomId; - } - + get avatarColorId(): string { return this.inviteUserIds?.[0] ?? this._roomId ?? this.localId; } + get avatarUrl(): string | undefined { return this.profiles[0]?.avatarUrl; } + get roomId(): string | undefined { return this._roomId; } get name() { return this._name; } - get isBeingCreated(): boolean { return true; } + get error(): Error | undefined { return this._error; } + + cancel() { + // remove from collection somehow + } } export async function loadProfiles(userIds: string[], hsApi: HomeServerApi, log: ILogItem): Promise { @@ -194,5 +186,4 @@ class UserIdProfile implements IProfile { get displayName() { return undefined; } get name() { return this.userId; } get avatarUrl() { return undefined; } - }