-
-
Notifications
You must be signed in to change notification settings - Fork 583
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
// 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--) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop feels kind of wasteful given that |
||
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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.