Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to determine who has read at least a given event #1625

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,43 @@ Room.prototype.recalculate = function() {
}
};

/**
* Get a list of user IDs who have read up to and including the given
* event. This will look at the most recent maxEvents to identify the
* given event, and any users who have read events leading up to that
* event. If the function exceeds the given limit, an empty array will
* be returned. Otherwise, an array with at least the event sender will
* be returned.
* @param {MatrixEvent} event The event to search for.
* @param {number} maxEvents The maximum number of events to search within.
* @returns {string[]} A list of user IDs who have read the event.
*/
Room.prototype.getUsersWhoHaveRead = function(event, maxEvents) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you want this API to collect all users who have read the event? In the one caller so far, you actually only care if someone has read it (other than yourself), but the actual list of senders goes unused... Is there a plan to use it in the future?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's largely the balance of usefulness and API scope. We could add a hasAnyoneRead function, but the react SDK would be the only consumer. In this approach, we're adding something theoretically useful for other applications, and maybe even ourselves if we decide to add a "who has read this" API.

Ideally this whole problem is solved by matrix rather than a loop in our code, but this is the first time we've needed a different kind of receipt calculation.

// First, determine if the event is a pending event. If yes, don't bother searching
if (this.getPendingEvents().some(e => e.getId() === event.getId())) {
return [event.getSender()];
}

// Now try to find where the event is in the timeline
const events = this.getLiveTimeline().getEvents(); // timelines are most recent last
let foundEvent = false;
const receiptIds = new Set();
for (let i = events.length - 1; i >= Math.max(0, events.length - maxEvents); i--) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop feels kind of wasteful given that TimelinePanel / MessagePanel already did a very similar scan to construct the EventTile... Anyway, mostly just an idle thought, as I guess you'd need to restructure lots of things for it to be more efficient.

const ev = events[i];
this.getUsersReadUpTo(ev).forEach(u => receiptIds.add(u));
if (ev.getId() === event.getId()) {
foundEvent = true;
break; // we don't need to search any further
}
}
if (!foundEvent) {
return []; // per our contract, return nothing
} else {
receiptIds.add(event.getSender()); // also per the contract, return with the sender
}
return Array.from(receiptIds);
};

/**
* Get a list of user IDs who have <b>read up to</b> the given event.
* @param {MatrixEvent} event the event to get read receipts for.
Expand Down