From fa2ec7f6c98ec01b5f594285743369d68a019af9 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 20 Sep 2022 18:00:31 +0100 Subject: [PATCH] Fix soft crash around unknown room pills (#9301) * Fix soft crash around unknown room pills * Add tests * Fix types --- src/editor/parts.ts | 10 +++++----- test/editor/parts-test.ts | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/editor/parts.ts b/src/editor/parts.ts index 8d98c398b5..7dbdfcdda3 100644 --- a/src/editor/parts.ts +++ b/src/editor/parts.ts @@ -411,7 +411,7 @@ export class EmojiPart extends BasePart implements IBasePart { } class RoomPillPart extends PillPart { - constructor(resourceId: string, label: string, private room: Room) { + constructor(resourceId: string, label: string, private room?: Room) { super(resourceId, label); } @@ -419,8 +419,8 @@ class RoomPillPart extends PillPart { let initialLetter = ""; let avatarUrl = Avatar.avatarUrlForRoom(this.room, 16, 16, "crop"); if (!avatarUrl) { - initialLetter = Avatar.getInitialLetter(this.room ? this.room.name : this.resourceId); - avatarUrl = Avatar.defaultAvatarUrlForString(this.room ? this.room.roomId : this.resourceId); + initialLetter = Avatar.getInitialLetter(this.room?.name || this.resourceId); + avatarUrl = Avatar.defaultAvatarUrlForString(this.room?.roomId ?? this.resourceId); } this.setAvatarVars(node, avatarUrl, initialLetter); } @@ -430,7 +430,7 @@ class RoomPillPart extends PillPart { } protected get className() { - return "mx_Pill " + (this.room.isSpaceRoom() ? "mx_SpacePill" : "mx_RoomPill"); + return "mx_Pill " + (this.room?.isSpaceRoom() ? "mx_SpacePill" : "mx_RoomPill"); } } @@ -610,7 +610,7 @@ export class PartCreator { } public roomPill(alias: string, roomId?: string): RoomPillPart { - let room; + let room: Room | undefined; if (roomId || alias[0] !== "#") { room = this.client.getRoom(roomId || alias); } else { diff --git a/test/editor/parts-test.ts b/test/editor/parts-test.ts index b77971c2aa..534221ece3 100644 --- a/test/editor/parts-test.ts +++ b/test/editor/parts-test.ts @@ -15,6 +15,7 @@ limitations under the License. */ import { EmojiPart, PlainPart } from "../../src/editor/parts"; +import { createPartCreator } from "./mock"; describe("editor/parts", () => { describe("appendUntilRejected", () => { @@ -32,4 +33,10 @@ describe("editor/parts", () => { expect(part.text).toEqual(femaleFacepalmEmoji); }); }); + + it("should not explode on room pills for unknown rooms", () => { + const pc = createPartCreator(); + const part = pc.roomPill("#room:server"); + expect(() => part.toDOMNode()).not.toThrow(); + }); });