prevent an empty array being passed to Heroes

This commit is contained in:
Bruno Windels 2020-08-21 19:03:21 +02:00
parent 7a66f2fc8e
commit a64d49a8a2
2 changed files with 16 additions and 8 deletions

View File

@ -15,7 +15,7 @@ limitations under the License.
*/ */
import {EventEmitter} from "../../utils/EventEmitter.js"; 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 {SyncWriter} from "./timeline/persistence/SyncWriter.js";
import {GapWriter} from "./timeline/persistence/GapWriter.js"; import {GapWriter} from "./timeline/persistence/GapWriter.js";
import {Timeline} from "./timeline/Timeline.js"; import {Timeline} from "./timeline/Timeline.js";
@ -51,14 +51,14 @@ export class Room extends EventEmitter {
isInitialSync, isTimelineOpen, isInitialSync, isTimelineOpen,
txn); txn);
const {entries, newLiveKey, changedMembers} = await this._syncWriter.writeSync(roomResponse, 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, // fetch new members while we have txn open,
// but don't make any in-memory changes yet // but don't make any in-memory changes yet
let heroChanges; 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); heroChanges = await this._heroes.calculateChanges(summaryChanges.heroes, changedMembers, txn);
} }
let removedPendingEvents; let removedPendingEvents;
@ -91,7 +91,7 @@ export class Room extends EventEmitter {
let emitChange = false; let emitChange = false;
if (summaryChanges) { if (summaryChanges) {
this._summary.applyChanges(summaryChanges); this._summary.applyChanges(summaryChanges);
if (this._summary.name && this._heroes) { if (!this._summary.needsHeroes) {
this._heroes = null; this._heroes = null;
} }
emitChange = true; emitChange = true;
@ -125,7 +125,7 @@ export class Room extends EventEmitter {
try { try {
this._summary.load(summary); this._summary.load(summary);
// need to load members for name? // need to load members for name?
if (!this._summary.name && this._summary.heroes) { if (this._summary.needsHeroes) {
this._heroes = new Heroes(this._roomId); this._heroes = new Heroes(this._roomId);
const changes = await this._heroes.calculateChanges(this._summary.heroes, [], txn); const changes = await this._heroes.calculateChanges(this._summary.heroes, [], txn);
this._heroes.applyChanges(changes, this._summary); this._heroes.applyChanges(changes, this._summary);

View File

@ -150,6 +150,10 @@ class SummaryData {
} }
} }
export function needsHeroes(data) {
return !data.name && !data.canonicalAlias && data.heroes && data.heroes.length > 0;
}
export class RoomSummary { export class RoomSummary {
constructor(roomId, ownUserId) { constructor(roomId, ownUserId) {
this._ownUserId = ownUserId; this._ownUserId = ownUserId;
@ -170,6 +174,10 @@ export class RoomSummary {
return this._data.heroes; return this._data.heroes;
} }
get needsHeroes() {
return needsHeroes(this._data);
}
get isUnread() { get isUnread() {
return this._data.isUnread; return this._data.isUnread;
} }