From 5325b0b466bdb400b41ad940b9afa2b8c04482e9 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 8 Feb 2022 14:58:29 +0100 Subject: [PATCH] cleanup logging --- .../rightpanel/MemberDetailsViewModel.js | 6 ++- src/matrix/Session.js | 38 ++++++++++--------- src/matrix/room/create.ts | 12 +++--- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/domain/session/rightpanel/MemberDetailsViewModel.js b/src/domain/session/rightpanel/MemberDetailsViewModel.js index 7ea2ead5..68045eba 100644 --- a/src/domain/session/rightpanel/MemberDetailsViewModel.js +++ b/src/domain/session/rightpanel/MemberDetailsViewModel.js @@ -86,8 +86,10 @@ export class MemberDetailsViewModel extends ViewModel { const room = this._session.findDirectMessageForUserId(this.userId); let roomId = room?.id; if (!roomId) { - const roomBeingCreated = await this._session.createRoom( - RoomType.DirectMessage, undefined, undefined, undefined, [this.userId], {loadProfiles: true}); + const roomBeingCreated = await this._session.createRoom({ + type: RoomType.DirectMessage, + invites: [this.userId] + }); roomId = roomBeingCreated.localId; } this.navigation.push("room", roomId); diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 45cd9b19..8d1c8613 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -64,12 +64,7 @@ export class Session { this._activeArchivedRooms = new Map(); this._invites = new ObservableMap(); this._inviteUpdateCallback = (invite, params) => this._invites.update(invite.id, params); - this._roomsBeingCreatedUpdateCallback = (rbc, params, log) => { - this._roomsBeingCreated.update(rbc.localId, params); - if (rbc.roomId && !!this.rooms.get(rbc.roomId)) { - this._tryReplaceRoomBeingCreated(rbc.roomId, log); - } - }; + this._roomsBeingCreatedUpdateCallback = (rbc, params) => this._roomsBeingCreated.update(rbc.localId, params); this._roomsBeingCreated = new ObservableMap(); this._user = new User(sessionInfo.userId); this._deviceMessageHandler = new DeviceMessageHandler({storage}); @@ -605,20 +600,27 @@ export class Session { return this._roomsBeingCreated; } - createRoom(type, isEncrypted, explicitName, topic, invites, options = undefined, log = undefined) { - return this._platform.logger.wrapOrRun(log, "create room", log => { - 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); + createRoom({type, isEncrypted, explicitName, topic, invites, loadProfiles = true}, log = undefined) { + let roomBeingCreated; + this._platform.logger.runDetached("create room", async log => { + const localId = `local-${Math.floor(this._platform.random() * Number.MAX_SAFE_INTEGER)}`; + roomBeingCreated = new RoomBeingCreated(localId, type, isEncrypted, + explicitName, topic, invites, this._roomsBeingCreatedUpdateCallback, + this._mediaRepository, log); this._roomsBeingCreated.set(localId, roomBeingCreated); - log.wrapDetached("create room network", log => { - const promises = [roomBeingCreated.create(this._hsApi, log)]; - if (options?.loadProfiles) { - promises.push(roomBeingCreated.loadProfiles(this._hsApi, log)); - } - return Promise.all(promises); - }); - return roomBeingCreated; + const promises = [roomBeingCreated.create(this._hsApi, log)]; + if (loadProfiles) { + promises.push(roomBeingCreated.loadProfiles(this._hsApi, log)); + } + await Promise.all(promises); + // we should now know the roomId, check if the room was synced before we received + // the room id. Replace the room being created with the synced room. + if (roomBeingCreated.roomId && !!this.rooms.get(roomBeingCreated.roomId)) { + this._tryReplaceRoomBeingCreated(roomBeingCreated.roomId, log); + } + // TODO: if type is DM, then adjust the m.direct account data }); + return roomBeingCreated; } async obtainSyncLock(syncResponse) { diff --git a/src/matrix/room/create.ts b/src/matrix/room/create.ts index 66714d72..4bdae474 100644 --- a/src/matrix/room/create.ts +++ b/src/matrix/room/create.ts @@ -74,7 +74,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { private readonly explicitName: string | undefined, private readonly topic: string | undefined, private readonly inviteUserIds: string[] | undefined, - private readonly updateCallback, + private readonly updateCallback: (self: RoomBeingCreated, params: string | undefined) => void, public readonly mediaRepository: MediaRepository, log: ILogItem ) { @@ -92,6 +92,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { } } + /** @internal */ async create(hsApi: HomeServerApi, log: ILogItem): Promise { const options: CreateRoomPayload = { is_direct: this.type === RoomType.DirectMessage, @@ -115,7 +116,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { } catch (err) { this._error = err; } - this.emitChange(undefined, log); + this.emitChange(); } /** requests the profiles of the invitees if needed to give an accurate @@ -123,6 +124,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { * The room is being created in the background whether this is called * or not, and this just gives a more accurate name while that request * is running. */ + /** @internal */ async loadProfiles(hsApi: HomeServerApi, log: ILogItem): Promise { // only load profiles if we need it for the room name and avatar if (!this.explicitName && this.inviteUserIds) { @@ -136,8 +138,8 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { } } - private emitChange(params?, log?: ILogItem) { - this.updateCallback(this, params, log); + private emitChange(params?: string) { + this.updateCallback(this, params); this.emit("change"); } @@ -149,7 +151,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { get error(): Error | undefined { return this._error; } cancel() { - // remove from collection somehow + // TODO: remove from collection somehow } }