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

Handle relations in encrypted rooms #969

Merged
merged 3 commits into from
Jun 27, 2019
Merged
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
32 changes: 29 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,21 @@ function _encryptEventIfNeeded(client, event, room) {

return client._crypto.encryptEvent(event, room);
}
/**
* Returns the eventType that should be used taking encryption into account
* for a given eventType.
* @param {MatrixClient} client the client
* @param {string} roomId the room for the events `eventType` relates to
* @param {string} eventType the event type
* @return {string} the event type taking encryption into account
*/
function _getEncryptedIfNeededEventType(client, roomId, eventType) {
if (eventType === "m.reaction") {
return eventType;
}
const isEncrypted = client.isRoomEncrypted(roomId);
return isEncrypted ? "m.room.encrypted" : eventType;
}

function _updatePendingEventStatus(room, event, newStatus) {
if (room) {
Expand Down Expand Up @@ -3980,7 +3995,9 @@ MatrixClient.prototype.getCanResetTimelineCallback = function() {
};

/**
* Returns relations for a given event
* Returns relations for a given event. Handles encryption transparently,
* with the caveat that the amount of events returned might be 0, even though you get a nextBatch.
* When the returned promise resolves, all messages should have finished trying to decrypt.
* @param {string} roomId the room of the event
* @param {string} eventId the id of the event
* @param {string} relationType the rel_type of the relations requested
Expand All @@ -3991,14 +4008,23 @@ MatrixClient.prototype.getCanResetTimelineCallback = function() {
*/
MatrixClient.prototype.relations =
async function(roomId, eventId, relationType, eventType, opts = {}) {
const fetchedEventType = _getEncryptedIfNeededEventType(this, roomId, eventType);
const result = await this.fetchRelations(
roomId,
eventId,
relationType,
eventType,
fetchedEventType,
opts);

let events = result.chunk.map(this.getEventMapper());
if (fetchedEventType === "m.room.encrypted") {
await Promise.all(events.map(e => {
return new Promise(resolve => e.once("Event.decrypted", resolve));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Reactions in an encrypted room won't be encrypted events themselves, so we can't use isEncrypted for the whole room to decide whether to await Event.decrypted.

Since the event mapper will invoke attemptDecryption, I think you only want to wait for Event.decrypted if event.isBeingDecrypted().

}));
events = events.filter(e => e.getType() === eventType);
}
return {
events: result.chunk.map(this.getEventMapper()),
events,
nextBatch: result.next_batch,
};
};
Expand Down