Fix soft crash around unknown room pills (#9301)

* Fix soft crash around unknown room pills

* Add tests

* Fix types
This commit is contained in:
Michael Telatynski 2022-09-20 18:00:31 +01:00 committed by GitHub
parent 7e435eef13
commit fa2ec7f6c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -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 {

View file

@ -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();
});
});