ensure memberwriter works with undefined for state/timeline events array

This commit is contained in:
Bruno Windels 2021-08-27 20:05:34 +02:00
parent 8e6bd6a7a1
commit 6a6762f036

View file

@ -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);
},
};
}