From e827fb2eb280b83286a99cdd1530d150de882225 Mon Sep 17 00:00:00 2001 From: Ajay Bura Date: Mon, 17 Jan 2022 12:20:18 +0530 Subject: [PATCH] Fix live read recipt count (#227) Signed-off-by: Ajay Bura --- src/client/state/RoomTimeline.js | 64 +++++++++++++++++++------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/client/state/RoomTimeline.js b/src/client/state/RoomTimeline.js index 1a988d6..4f5c803 100644 --- a/src/client/state/RoomTimeline.js +++ b/src/client/state/RoomTimeline.js @@ -12,6 +12,20 @@ function isReaction(mEvent) { return mEvent.getType() === 'm.reaction'; } +function hideMemberEvents(mEvent) { + const content = mEvent.getContent(); + const prevContent = mEvent.getPrevContent(); + const { membership } = content; + if (settings.hideMembershipEvents) { + if (membership === 'invite' || membership === 'ban' || membership === 'leave') return true; + if (prevContent.membership !== 'join') return true; + } + if (settings.hideNickAvatarEvents) { + if (membership === 'join' && prevContent.membership === 'join') return true; + } + return false; +} + function getRelateToId(mEvent) { const relation = mEvent.getRelation(); return relation && relation.event_id; @@ -112,18 +126,8 @@ class RoomTimeline extends EventEmitter { } addToTimeline(mEvent) { - if (mEvent.getType() === 'm.room.member' && (settings.hideMembershipEvents || settings.hideNickAvatarEvents)) { - const content = mEvent.getContent(); - const prevContent = mEvent.getPrevContent(); - const { membership } = content; - - if (settings.hideMembershipEvents) { - if (membership === 'invite' || membership === 'ban' || membership === 'leave') return; - if (prevContent.membership !== 'join') return; - } - if (settings.hideNickAvatarEvents) { - if (membership === 'join' && prevContent.membership === 'join') return; - } + if (mEvent.getType() === 'm.room.member' && hideMemberEvents(mEvent)) { + return; } if (mEvent.isRedacted()) return; if (isReaction(mEvent)) { @@ -244,23 +248,10 @@ class RoomTimeline extends EventEmitter { return this.room.getUnfilteredTimelineSet(); } - getLiveReaders() { - const lastEvent = this.timeline[this.timeline.length - 1]; - const liveEvents = this.liveTimeline.getEvents(); - - const readers = []; - - for (let i = liveEvents.length - 1; i >= 0; i -= 1) { - readers.splice(readers.length, 0, ...this.room.getUsersReadUpTo(liveEvents[i])); - if (lastEvent === liveEvents[i]) break; - } - - return [...new Set(readers)]; - } - getEventReaders(mEvent) { const liveEvents = this.liveTimeline.getEvents(); const readers = []; + if (!mEvent) return []; for (let i = liveEvents.length - 1; i >= 0; i -= 1) { readers.splice(readers.length, 0, ...this.room.getUsersReadUpTo(liveEvents[i])); @@ -270,6 +261,27 @@ class RoomTimeline extends EventEmitter { return [...new Set(readers)]; } + getLiveReaders() { + const liveEvents = this.liveTimeline.getEvents(); + const getLatestVisibleEvent = () => { + for (let i = liveEvents.length - 1; i >= 0; i -= 1) { + const mEvent = liveEvents[i]; + if (mEvent.getType() === 'm.room.member' && hideMemberEvents(mEvent)) { + // eslint-disable-next-line no-continue + continue; + } + if (!mEvent.isRedacted() + && !isReaction(mEvent) + && !isEdited(mEvent) + && cons.supportEventTypes.includes(mEvent.getType()) + ) return mEvent; + } + return liveEvents[liveEvents.length - 1]; + }; + + return this.getEventReaders(getLatestVisibleEvent()); + } + getUnreadEventIndex(readUpToEventId) { if (!this.hasEventInTimeline(readUpToEventId)) return -1;