From fafdf669db16a6bcbbc1bb946795371ccaeede34 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 20 Aug 2020 15:23:56 +0200 Subject: [PATCH] use prev_content from later events before state --- src/matrix/room/members/RoomMember.js | 13 +++++++++- .../room/timeline/persistence/GapWriter.js | 26 +++++++++---------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/matrix/room/members/RoomMember.js b/src/matrix/room/members/RoomMember.js index b794369e..4c38f66b 100644 --- a/src/matrix/room/members/RoomMember.js +++ b/src/matrix/room/members/RoomMember.js @@ -27,7 +27,18 @@ export class RoomMember { if (typeof userId !== "string") { return; } - const {content} = memberEvent; + return this._fromMemberEventContent(roomId, userId, memberEvent.content); + } + + static fromReplacingMemberEvent(roomId, memberEvent) { + const userId = memberEvent && memberEvent.state_key; + if (typeof userId !== "string") { + return; + } + return this._fromMemberEventContent(roomId, userId, memberEvent.prev_content); + } + + static _fromMemberEventContent(roomId, userId, content) { const membership = content?.membership; const avatarUrl = content?.avatar_url; const displayName = content?.displayname; diff --git a/src/matrix/room/timeline/persistence/GapWriter.js b/src/matrix/room/timeline/persistence/GapWriter.js index f64b1690..7a7ab4f5 100644 --- a/src/matrix/room/timeline/persistence/GapWriter.js +++ b/src/matrix/room/timeline/persistence/GapWriter.js @@ -108,9 +108,8 @@ export class GapWriter { const event = events[i]; key = key.nextKeyForDirection(direction); const eventStorageEntry = createEventEntry(key, this._roomId, event); - const memberEvent = this._findMemberEvent(event.sender, state, events, i, direction); - if (memberEvent) { - const memberData = RoomMember.fromMemberEvent(memberEvent)?.serialize(); + const memberData = this._findMemberData(event.sender, state, events, i, direction); + if (memberData) { eventStorageEntry.displayName = memberData?.displayName; eventStorageEntry.avatarUrl = memberData?.avatarUrl; } @@ -121,29 +120,30 @@ export class GapWriter { return entries; } - _findMemberEvent(userId, state, events, index, direction) { + _findMemberData(userId, state, events, index, direction) { function isOurUser(event) { return event.type === MEMBER_EVENT_TYPE && event.state_key === userId; } - // older messages are further in the array when going backwards + // older messages are at a higher index in the array when going backwards const inc = direction.isBackward ? 1 : -1; for (let i = index + inc; i >= 0 && i < events.length; i += inc) { const event = events[i]; if (isOurUser(event)) { - return event; + return RoomMember.fromMemberEvent(this._roomId, event)?.serialize(); } } - const stateMemberEvent = state.find(isOurUser); - if (stateMemberEvent) { - return stateMemberEvent; - } - // look into newer events as a fallback, even though it is techically not correct - for (let i = index - inc; i >= 0 && i < events.length; i -= inc) { + // look into newer events, but using prev_content if found + for (let i = index; i >= 0 && i < events.length; i -= inc) { const event = events[i]; if (isOurUser(event)) { - return event; + return RoomMember.fromReplacingMemberEvent(this._roomId, event)?.serialize(); } } + // assuming the member hasn't changed within the chunk, just take it from state if it's there + const stateMemberEvent = state.find(isOurUser); + if (stateMemberEvent) { + return RoomMember.fromMemberEvent(this._roomId, stateMemberEvent)?.serialize(); + } } async _updateFragments(fragmentEntry, neighbourFragmentEntry, end, entries, txn) {