cleanup logging

This commit is contained in:
Bruno Windels 2022-02-08 14:58:29 +01:00
parent d7b024eac1
commit 5325b0b466
3 changed files with 31 additions and 25 deletions

View File

@ -86,8 +86,10 @@ export class MemberDetailsViewModel extends ViewModel {
const room = this._session.findDirectMessageForUserId(this.userId); const room = this._session.findDirectMessageForUserId(this.userId);
let roomId = room?.id; let roomId = room?.id;
if (!roomId) { if (!roomId) {
const roomBeingCreated = await this._session.createRoom( const roomBeingCreated = await this._session.createRoom({
RoomType.DirectMessage, undefined, undefined, undefined, [this.userId], {loadProfiles: true}); type: RoomType.DirectMessage,
invites: [this.userId]
});
roomId = roomBeingCreated.localId; roomId = roomBeingCreated.localId;
} }
this.navigation.push("room", roomId); this.navigation.push("room", roomId);

View File

@ -64,12 +64,7 @@ export class Session {
this._activeArchivedRooms = new Map(); this._activeArchivedRooms = new Map();
this._invites = new ObservableMap(); this._invites = new ObservableMap();
this._inviteUpdateCallback = (invite, params) => this._invites.update(invite.id, params); this._inviteUpdateCallback = (invite, params) => this._invites.update(invite.id, params);
this._roomsBeingCreatedUpdateCallback = (rbc, params, log) => { this._roomsBeingCreatedUpdateCallback = (rbc, params) => this._roomsBeingCreated.update(rbc.localId, params);
this._roomsBeingCreated.update(rbc.localId, params);
if (rbc.roomId && !!this.rooms.get(rbc.roomId)) {
this._tryReplaceRoomBeingCreated(rbc.roomId, log);
}
};
this._roomsBeingCreated = new ObservableMap(); this._roomsBeingCreated = new ObservableMap();
this._user = new User(sessionInfo.userId); this._user = new User(sessionInfo.userId);
this._deviceMessageHandler = new DeviceMessageHandler({storage}); this._deviceMessageHandler = new DeviceMessageHandler({storage});
@ -605,20 +600,27 @@ export class Session {
return this._roomsBeingCreated; return this._roomsBeingCreated;
} }
createRoom(type, isEncrypted, explicitName, topic, invites, options = undefined, log = undefined) { createRoom({type, isEncrypted, explicitName, topic, invites, loadProfiles = true}, log = undefined) {
return this._platform.logger.wrapOrRun(log, "create room", log => { let roomBeingCreated;
const localId = `local-${Math.round(this._platform.random() * Math.MAX_SAFE_INTEGER)}`; this._platform.logger.runDetached("create room", async log => {
const roomBeingCreated = new RoomBeingCreated(localId, type, isEncrypted, explicitName, topic, invites, this._roomsBeingCreatedUpdateCallback, this._mediaRepository, 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); this._roomsBeingCreated.set(localId, roomBeingCreated);
log.wrapDetached("create room network", log => { const promises = [roomBeingCreated.create(this._hsApi, log)];
const promises = [roomBeingCreated.create(this._hsApi, log)]; if (loadProfiles) {
if (options?.loadProfiles) { promises.push(roomBeingCreated.loadProfiles(this._hsApi, log));
promises.push(roomBeingCreated.loadProfiles(this._hsApi, log)); }
} await Promise.all(promises);
return 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.
return roomBeingCreated; 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) { async obtainSyncLock(syncResponse) {

View File

@ -74,7 +74,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
private readonly explicitName: string | undefined, private readonly explicitName: string | undefined,
private readonly topic: string | undefined, private readonly topic: string | undefined,
private readonly inviteUserIds: string[] | undefined, private readonly inviteUserIds: string[] | undefined,
private readonly updateCallback, private readonly updateCallback: (self: RoomBeingCreated, params: string | undefined) => void,
public readonly mediaRepository: MediaRepository, public readonly mediaRepository: MediaRepository,
log: ILogItem log: ILogItem
) { ) {
@ -92,6 +92,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
} }
} }
/** @internal */
async create(hsApi: HomeServerApi, log: ILogItem): Promise<void> { async create(hsApi: HomeServerApi, log: ILogItem): Promise<void> {
const options: CreateRoomPayload = { const options: CreateRoomPayload = {
is_direct: this.type === RoomType.DirectMessage, is_direct: this.type === RoomType.DirectMessage,
@ -115,7 +116,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
} catch (err) { } catch (err) {
this._error = err; this._error = err;
} }
this.emitChange(undefined, log); this.emitChange();
} }
/** requests the profiles of the invitees if needed to give an accurate /** 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 * 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 * or not, and this just gives a more accurate name while that request
* is running. */ * is running. */
/** @internal */
async loadProfiles(hsApi: HomeServerApi, log: ILogItem): Promise<void> { async loadProfiles(hsApi: HomeServerApi, log: ILogItem): Promise<void> {
// only load profiles if we need it for the room name and avatar // only load profiles if we need it for the room name and avatar
if (!this.explicitName && this.inviteUserIds) { if (!this.explicitName && this.inviteUserIds) {
@ -136,8 +138,8 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
} }
} }
private emitChange(params?, log?: ILogItem) { private emitChange(params?: string) {
this.updateCallback(this, params, log); this.updateCallback(this, params);
this.emit("change"); this.emit("change");
} }
@ -149,7 +151,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
get error(): Error | undefined { return this._error; } get error(): Error | undefined { return this._error; }
cancel() { cancel() {
// remove from collection somehow // TODO: remove from collection somehow
} }
} }