From f12841b2d37cad9c306d7e71820a2e60fe7905f8 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 10 Feb 2022 11:06:20 +0100 Subject: [PATCH] better error handling in RoomBeingCreated --- src/matrix/room/create.ts | 96 ++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/src/matrix/room/create.ts b/src/matrix/room/create.ts index cc65892e..1127c7ac 100644 --- a/src/matrix/room/create.ts +++ b/src/matrix/room/create.ts @@ -117,44 +117,44 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { /** @internal */ async create(hsApi: HomeServerApi, log: ILogItem): Promise { - let avatarEventContent; - if (this.options.avatar) { - const {avatar} = this.options; - const attachment = new AttachmentUpload({filename: avatar.name, blob: avatar.blob, platform: this.platform}); - await attachment.upload(hsApi, () => {}, log); - avatarEventContent = { - info: avatar.info - }; - attachment.applyToContent("url", avatarEventContent); - } - const createOptions: CreateRoomPayload = { - is_direct: this.options.type === RoomType.DirectMessage, - preset: presetForType(this.options.type), - initial_state: [] - }; - if (this.options.name) { - createOptions.name = this.options.name; - } - if (this.options.topic) { - createOptions.topic = this.options.topic; - } - if (this.options.invites) { - createOptions.invite = this.options.invites; - } - if (this.options.alias) { - createOptions.room_alias_name = this.options.alias; - } - if (this.isEncrypted) { - createOptions.initial_state.push(createRoomEncryptionEvent()); - } - if (avatarEventContent) { - createOptions.initial_state.push({ - type: "m.room.avatar", - state_key: "", - content: avatarEventContent - }); - } try { + let avatarEventContent; + if (this.options.avatar) { + const {avatar} = this.options; + const attachment = new AttachmentUpload({filename: avatar.name, blob: avatar.blob, platform: this.platform}); + await attachment.upload(hsApi, () => {}, log); + avatarEventContent = { + info: avatar.info + }; + attachment.applyToContent("url", avatarEventContent); + } + const createOptions: CreateRoomPayload = { + is_direct: this.options.type === RoomType.DirectMessage, + preset: presetForType(this.options.type), + initial_state: [] + }; + if (this.options.name) { + createOptions.name = this.options.name; + } + if (this.options.topic) { + createOptions.topic = this.options.topic; + } + if (this.options.invites) { + createOptions.invite = this.options.invites; + } + if (this.options.alias) { + createOptions.room_alias_name = this.options.alias; + } + if (this.isEncrypted) { + createOptions.initial_state.push(createRoomEncryptionEvent()); + } + if (avatarEventContent) { + createOptions.initial_state.push({ + type: "m.room.avatar", + state_key: "", + content: avatarEventContent + }); + } const response = await hsApi.createRoom(createOptions, {log}).response(); this._roomId = response["room_id"]; } catch (err) { @@ -170,16 +170,18 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { * 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.options.name && this.options.invites) { - this.profiles = await loadProfiles(this.options.invites, hsApi, log); - const summaryData = { - joinCount: 1, // ourselves - inviteCount: this.options.invites.length - }; - this._calculatedName = calculateRoomName(this.profiles, summaryData, log); - this.emitChange(); - } + try { + // only load profiles if we need it for the room name and avatar + if (!this.options.name && this.options.invites) { + this.profiles = await loadProfiles(this.options.invites, hsApi, log); + const summaryData = { + joinCount: 1, // ourselves + inviteCount: this.options.invites.length + }; + this._calculatedName = calculateRoomName(this.profiles, summaryData, log); + this.emitChange(); + } + } catch (err) {} // swallow error, loading profiles is not essential } private emitChange(params?: string) {