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); let member = this._cache.get(userId);
if (!member) { if (!member) {
const memberData = await txn.roomMembers.get(this._roomId, userId); const memberData = await txn.roomMembers.get(this._roomId, userId);
@ -83,12 +84,21 @@ export class MemberWriter {
} }
} }
if (!member) { if (!member) {
let memberEvent;
// sometimes the member event isn't included in state, but rather in the timeline, // 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 // even if it is not the first event in the timeline. In this case, go look for
// first occurence // the last one before the event
const memberEvent = timelineEvents.find(e => { let foundEvent = false;
return e.type === MEMBER_EVENT_TYPE && e.state_key === userId; 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) { if (memberEvent) {
member = RoomMember.fromMemberEvent(this._roomId, memberEvent); member = RoomMember.fromMemberEvent(this._roomId, memberEvent);
} }
@ -228,5 +238,13 @@ export function tests() {
const change = await writer.writeTimelineMemberEvent(event, txn); const change = await writer.writeTimelineMemberEvent(event, txn);
assert(change); 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 // store event in timeline
currentKey = currentKey.nextKey(); currentKey = currentKey.nextKey();
const entry = createEventEntry(currentKey, this._roomId, event); 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) { if (member) {
entry.displayName = member.displayName; entry.displayName = member.displayName;
entry.avatarUrl = member.avatarUrl; entry.avatarUrl = member.avatarUrl;