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._memberWriter = memberWriter;
this._timelineEvents = timelineEvents; this._timelineEvents = timelineEvents;
this._hasFetchedMembers = hasFetchedMembers; this._hasFetchedMembers = hasFetchedMembers;
this._newStateMembers = stateEvents && this._stateEventsToMembers(stateEvents); this._newStateMembers = null;
if (stateEvents) {
this._newStateMembers = this._stateEventsToMembers(stateEvents);
}
} }
get _roomId() { get _roomId() {
@ -84,11 +87,11 @@ class MemberSync {
return members; return members;
} }
_timelineEventsToMembers() { _timelineEventsToMembers(timelineEvents) {
let members; let members;
// iterate backwards to only add the last member in the timeline // iterate backwards to only add the last member in the timeline
for (let i = this._timelineEvents.length - 1; i >= 0; i--) { for (let i = timelineEvents.length - 1; i >= 0; i--) {
const e = this._timelineEvents[i]; const e = timelineEvents[i];
const userId = e.state_key; const userId = e.state_key;
if (e.type === MEMBER_EVENT_TYPE && !members?.has(userId)) { if (e.type === MEMBER_EVENT_TYPE && !members?.has(userId)) {
const member = RoomMember.fromMemberEvent(this._roomId, e); const member = RoomMember.fromMemberEvent(this._roomId, e);
@ -104,10 +107,13 @@ class MemberSync {
} }
async lookupMemberAtEvent(userId, event, txn) { async lookupMemberAtEvent(userId, event, txn) {
let member = this._findPrecedingMemberEventInTimeline(userId, event); let member;
if (this._timelineEvents) {
member = this._findPrecedingMemberEventInTimeline(userId, event);
if (member) { if (member) {
return member; return member;
} }
}
member = this._newStateMembers?.get(userId); member = this._newStateMembers?.get(userId);
if (member) { if (member) {
return member; return member;
@ -117,7 +123,10 @@ class MemberSync {
async write(txn) { async write(txn) {
const memberChanges = new Map(); const memberChanges = new Map();
const newTimelineMembers = this._timelineEventsToMembers(); let newTimelineMembers;
if (this._timelineEvents) {
newTimelineMembers = this._timelineEventsToMembers(this._timelineEvents);
}
if (this._newStateMembers) { if (this._newStateMembers) {
for (const member of this._newStateMembers.values()) { for (const member of this._newStateMembers.values()) {
if (!newTimelineMembers?.has(member.userId)) { if (!newTimelineMembers?.has(member.userId)) {
@ -217,14 +226,6 @@ export function tests() {
const alice = "@alice:hs.tld"; const alice = "@alice:hs.tld";
const avatar = "mxc://hs.tld/def"; 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 { return {
"new join": async assert => { "new join": async assert => {
const writer = new MemberWriter(roomId); const writer = new MemberWriter(roomId);
@ -409,5 +410,12 @@ export function tests() {
const change = changes.get(alice); const change = changes.get(alice);
assert(change.hasJoined); 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);
},
}; };
} }