take most recent member rather than first in timeline for inline lookup

noticed this while inspecting the code, looks related to #269
This commit is contained in:
Bruno Windels 2021-04-08 12:57:10 +02:00
parent 6f37c232f7
commit f67ccc18f4
2 changed files with 25 additions and 7 deletions

View file

@ -73,7 +73,8 @@ export class MemberWriter {
}
}
async lookupMember(userId, timelineEvents, txn) {
async lookupSenderMember(event, timelineEvents, txn) {
const userId = event.sender;
let member = this._cache.get(userId);
if (!member) {
const memberData = await txn.roomMembers.get(this._roomId, userId);
@ -83,12 +84,21 @@ export class MemberWriter {
}
}
if (!member) {
let memberEvent;
// sometimes the member event isn't included in state, but rather in the timeline,
// even if it is not the first event in the timeline. In this case, go look for the
// first occurence
const memberEvent = timelineEvents.find(e => {
return e.type === MEMBER_EVENT_TYPE && e.state_key === userId;
});
// even if it is not the first event in the timeline. In this case, go look for
// the last one before the event
let foundEvent = false;
for (let i = timelineEvents.length - 1; i >= 0; i -= 1) {
const e = timelineEvents[i];
if (!foundEvent && e.event_id === event.event_id) {
foundEvent = true;
}
if (foundEvent && e.type === MEMBER_EVENT_TYPE && e.state_key === userId) {
memberEvent = e;
break;
}
}
if (memberEvent) {
member = RoomMember.fromMemberEvent(this._roomId, memberEvent);
}
@ -228,5 +238,13 @@ export function tests() {
const change = await writer.writeTimelineMemberEvent(event, txn);
assert(change);
},
"lookupSenderMember returns closest member in the past": async assert => {
const event1 = createMemberEvent("join", alice, "Alice");
const event2 = createMemberEvent("join", alice, "Alies");
const writer = new MemberWriter(roomId);
const txn = createStorage();
const member = await writer.lookupSenderMember(event2, [event1, event2], txn);
assert.equal(member.displayName, "Alies");
},
};
}

View file

@ -162,7 +162,7 @@ export class SyncWriter {
// store event in timeline
currentKey = currentKey.nextKey();
const entry = createEventEntry(currentKey, this._roomId, event);
let member = await this._memberWriter.lookupMember(event.sender, events, txn);
let member = await this._memberWriter.lookupSenderMember(event, events, txn);
if (member) {
entry.displayName = member.displayName;
entry.avatarUrl = member.avatarUrl;