From 90da67aa9521cf95a3f194e4508a138f4293bf8d Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 25 Nov 2025 09:09:11 +0000 Subject: [PATCH] Re-add truthy check on room name/avatar/alias events (#5081) * Re-add truthy check on room name/avatar/alias events Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Add regression test Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- spec/unit/models/room.spec.ts | 23 +++++++++++++++++++++++ src/models/room.ts | 6 +++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/spec/unit/models/room.spec.ts b/spec/unit/models/room.spec.ts index e0d319b2f..b7dd3ced5 100644 --- a/spec/unit/models/room.spec.ts +++ b/spec/unit/models/room.spec.ts @@ -229,4 +229,27 @@ describe("Room", () => { expect(room.getAltAliases()).toEqual(["#foo:bar"]); }); }); + + describe("calculateRoomName()", () => { + it("should ignore empty m.room.name 'name' field", async () => { + const mockClient = createMockClient(); + const room = new Room("!room:example.org", mockClient, CREATOR_USER_ID); + const event = new MatrixEvent({ + type: EventType.RoomName, + content: { + name: "", + }, + state_key: "", + event_id: "$123", + room_id: room.roomId, + sender: CREATOR_USER_ID, + }); + + // Set up the room + room.currentState.setStateEvents([event]); + room.recalculate(); + + expect(room.name).not.toEqual(""); + }); + }); }); diff --git a/src/models/room.ts b/src/models/room.ts index 9974aa0c7..57f636a26 100644 --- a/src/models/room.ts +++ b/src/models/room.ts @@ -1810,7 +1810,7 @@ export class Room extends ReadReceipt { */ public getMxcAvatarUrl(): string | null { const url = this.currentState.getStateEvents(EventType.RoomAvatar, "")?.getContent().url; - return typeof url === "string" ? url : null; + return url && typeof url === "string" ? url : null; } /** @@ -1821,7 +1821,7 @@ export class Room extends ReadReceipt { */ public getCanonicalAlias(): string | null { const canonicalAlias = this.currentState.getStateEvents(EventType.RoomCanonicalAlias, "")?.getContent().alias; - return typeof canonicalAlias === "string" ? canonicalAlias : null; + return canonicalAlias && typeof canonicalAlias === "string" ? canonicalAlias : null; } /** @@ -3638,7 +3638,7 @@ export class Room extends ReadReceipt { private calculateRoomName(userId: string, ignoreRoomNameEvent = false): string { if (!ignoreRoomNameEvent) { const name = this.currentState.getStateEvents(EventType.RoomName, "")?.getContent().name; - if (typeof name === "string") { + if (name && typeof name === "string") { return this.roomNameGenerator({ type: RoomNameType.Actual, name,