Skip to content

Commit

Permalink
Fix soft crash around unknown room pills (#9301)
Browse files Browse the repository at this point in the history
* Fix soft crash around unknown room pills

* Add tests

* Fix types
  • Loading branch information
t3chguy committed Sep 20, 2022
1 parent 7e435ee commit fa2ec7f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/editor/parts.ts
Expand Up @@ -411,16 +411,16 @@ 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);
}

protected setAvatar(node: HTMLElement): void {
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);
}
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions test/editor/parts-test.ts
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

import { EmojiPart, PlainPart } from "../../src/editor/parts";
import { createPartCreator } from "./mock";

describe("editor/parts", () => {
describe("appendUntilRejected", () => {
Expand All @@ -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();
});
});

0 comments on commit fa2ec7f

Please sign in to comment.