From a64d49a8a2f1336f8b241ec44ff93992d14bb8b9 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 21 Aug 2020 19:03:21 +0200 Subject: [PATCH] prevent an empty array being passed to Heroes --- src/matrix/room/Room.js | 16 ++++++++-------- src/matrix/room/RoomSummary.js | 8 ++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index b7a70db5..1bb8371b 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -15,7 +15,7 @@ limitations under the License. */ import {EventEmitter} from "../../utils/EventEmitter.js"; -import {RoomSummary} from "./RoomSummary.js"; +import {RoomSummary, needsHeroes} from "./RoomSummary.js"; import {SyncWriter} from "./timeline/persistence/SyncWriter.js"; import {GapWriter} from "./timeline/persistence/GapWriter.js"; import {Timeline} from "./timeline/Timeline.js"; @@ -51,14 +51,14 @@ export class Room extends EventEmitter { isInitialSync, isTimelineOpen, txn); const {entries, newLiveKey, changedMembers} = await this._syncWriter.writeSync(roomResponse, txn); - // room name disappeared, open heroes - if (!summaryChanges.name && summaryChanges.heroes && !this._heroes) { - this._heroes = new Heroes(this._roomId); - } // fetch new members while we have txn open, // but don't make any in-memory changes yet let heroChanges; - if (summaryChanges.heroes && this._heroes) { + if (needsHeroes(summaryChanges)) { + // room name disappeared, open heroes + if (!this._heroes) { + this._heroes = new Heroes(this._roomId); + } heroChanges = await this._heroes.calculateChanges(summaryChanges.heroes, changedMembers, txn); } let removedPendingEvents; @@ -91,7 +91,7 @@ export class Room extends EventEmitter { let emitChange = false; if (summaryChanges) { this._summary.applyChanges(summaryChanges); - if (this._summary.name && this._heroes) { + if (!this._summary.needsHeroes) { this._heroes = null; } emitChange = true; @@ -125,7 +125,7 @@ export class Room extends EventEmitter { try { this._summary.load(summary); // need to load members for name? - if (!this._summary.name && this._summary.heroes) { + if (this._summary.needsHeroes) { this._heroes = new Heroes(this._roomId); const changes = await this._heroes.calculateChanges(this._summary.heroes, [], txn); this._heroes.applyChanges(changes, this._summary); diff --git a/src/matrix/room/RoomSummary.js b/src/matrix/room/RoomSummary.js index e924f6fe..36c7d203 100644 --- a/src/matrix/room/RoomSummary.js +++ b/src/matrix/room/RoomSummary.js @@ -150,6 +150,10 @@ class SummaryData { } } +export function needsHeroes(data) { + return !data.name && !data.canonicalAlias && data.heroes && data.heroes.length > 0; +} + export class RoomSummary { constructor(roomId, ownUserId) { this._ownUserId = ownUserId; @@ -170,6 +174,10 @@ export class RoomSummary { return this._data.heroes; } + get needsHeroes() { + return needsHeroes(this._data); + } + get isUnread() { return this._data.isUnread; }