From 6a6762f03682f6dfbc5f656158e60f3a702fced6 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 27 Aug 2021 20:05:34 +0200 Subject: [PATCH] ensure memberwriter works with undefined for state/timeline events array --- .../room/timeline/persistence/MemberWriter.js | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/matrix/room/timeline/persistence/MemberWriter.js b/src/matrix/room/timeline/persistence/MemberWriter.js index fba43ddf..b21a4461 100644 --- a/src/matrix/room/timeline/persistence/MemberWriter.js +++ b/src/matrix/room/timeline/persistence/MemberWriter.js @@ -61,7 +61,10 @@ class MemberSync { this._memberWriter = memberWriter; this._timelineEvents = timelineEvents; this._hasFetchedMembers = hasFetchedMembers; - this._newStateMembers = stateEvents && this._stateEventsToMembers(stateEvents); + this._newStateMembers = null; + if (stateEvents) { + this._newStateMembers = this._stateEventsToMembers(stateEvents); + } } get _roomId() { @@ -84,11 +87,11 @@ class MemberSync { return members; } - _timelineEventsToMembers() { + _timelineEventsToMembers(timelineEvents) { let members; // iterate backwards to only add the last member in the timeline - for (let i = this._timelineEvents.length - 1; i >= 0; i--) { - const e = this._timelineEvents[i]; + for (let i = timelineEvents.length - 1; i >= 0; i--) { + const e = timelineEvents[i]; const userId = e.state_key; if (e.type === MEMBER_EVENT_TYPE && !members?.has(userId)) { const member = RoomMember.fromMemberEvent(this._roomId, e); @@ -104,9 +107,12 @@ class MemberSync { } async lookupMemberAtEvent(userId, event, txn) { - let member = this._findPrecedingMemberEventInTimeline(userId, event); - if (member) { - return member; + let member; + if (this._timelineEvents) { + member = this._findPrecedingMemberEventInTimeline(userId, event); + if (member) { + return member; + } } member = this._newStateMembers?.get(userId); if (member) { @@ -117,7 +123,10 @@ class MemberSync { async write(txn) { const memberChanges = new Map(); - const newTimelineMembers = this._timelineEventsToMembers(); + let newTimelineMembers; + if (this._timelineEvents) { + newTimelineMembers = this._timelineEventsToMembers(this._timelineEvents); + } if (this._newStateMembers) { for (const member of this._newStateMembers.values()) { if (!newTimelineMembers?.has(member.userId)) { @@ -217,14 +226,6 @@ export function tests() { const alice = "@alice:hs.tld"; const avatar = "mxc://hs.tld/def"; -/* - join without previous membership - join during gap with hasFetchedMembers=false - join during gap with hasFetchedMembers=true - join after invite - -*/ - return { "new join": async assert => { const writer = new MemberWriter(roomId); @@ -409,5 +410,12 @@ export function tests() { const change = changes.get(alice); assert(change.hasJoined); }, + "write works without event arrays": async assert => { + const writer = new MemberWriter(roomId); + const txn = createStorage(); + const memberSync = await writer.prepareMemberSync(undefined, undefined, false); + const changes = await memberSync.write(txn); + assert.equal(changes.size, 0); + }, }; }