Skip to content

Commit 90da67a

Browse files
authored
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 <[email protected]> * Add regression test Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 0bf2702 commit 90da67a

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

spec/unit/models/room.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,27 @@ describe("Room", () => {
229229
expect(room.getAltAliases()).toEqual(["#foo:bar"]);
230230
});
231231
});
232+
233+
describe("calculateRoomName()", () => {
234+
it("should ignore empty m.room.name 'name' field", async () => {
235+
const mockClient = createMockClient();
236+
const room = new Room("!room:example.org", mockClient, CREATOR_USER_ID);
237+
const event = new MatrixEvent({
238+
type: EventType.RoomName,
239+
content: {
240+
name: "",
241+
},
242+
state_key: "",
243+
event_id: "$123",
244+
room_id: room.roomId,
245+
sender: CREATOR_USER_ID,
246+
});
247+
248+
// Set up the room
249+
room.currentState.setStateEvents([event]);
250+
room.recalculate();
251+
252+
expect(room.name).not.toEqual("");
253+
});
254+
});
232255
});

src/models/room.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
18101810
*/
18111811
public getMxcAvatarUrl(): string | null {
18121812
const url = this.currentState.getStateEvents(EventType.RoomAvatar, "")?.getContent().url;
1813-
return typeof url === "string" ? url : null;
1813+
return url && typeof url === "string" ? url : null;
18141814
}
18151815

18161816
/**
@@ -1821,7 +1821,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
18211821
*/
18221822
public getCanonicalAlias(): string | null {
18231823
const canonicalAlias = this.currentState.getStateEvents(EventType.RoomCanonicalAlias, "")?.getContent().alias;
1824-
return typeof canonicalAlias === "string" ? canonicalAlias : null;
1824+
return canonicalAlias && typeof canonicalAlias === "string" ? canonicalAlias : null;
18251825
}
18261826

18271827
/**
@@ -3638,7 +3638,7 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
36383638
private calculateRoomName(userId: string, ignoreRoomNameEvent = false): string {
36393639
if (!ignoreRoomNameEvent) {
36403640
const name = this.currentState.getStateEvents(EventType.RoomName, "")?.getContent().name;
3641-
if (typeof name === "string") {
3641+
if (name && typeof name === "string") {
36423642
return this.roomNameGenerator({
36433643
type: RoomNameType.Actual,
36443644
name,

0 commit comments

Comments
 (0)