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

Prioritise and reduce the amount of events decrypted on application startup #1684

Merged
merged 16 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 7 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5554,8 +5554,9 @@ function _resolve(callback, resolve, res) {
resolve(res);
}

function _PojoToMatrixEventMapper(client, options) {
const preventReEmit = Boolean(options && options.preventReEmit);
function _PojoToMatrixEventMapper(client, options = {}) {
const preventReEmit = Boolean(options.preventReEmit);
const decrypt = options.decrypt === true;
function mapper(plainOldJsObject) {
const event = new MatrixEvent(plainOldJsObject);
if (event.isEncrypted()) {
Expand All @@ -5564,7 +5565,9 @@ function _PojoToMatrixEventMapper(client, options) {
"Event.decrypted",
]);
}
event.attemptDecryption(client._crypto);
if (decrypt) {
event.attemptDecryption(client._crypto);
}
}
if (!preventReEmit) {
client.reEmitter.reEmit(event, ["Event.replaced"]);
Expand All @@ -5577,6 +5580,7 @@ function _PojoToMatrixEventMapper(client, options) {
/**
* @param {object} [options]
* @param {bool} options.preventReEmit don't reemit events emitted on an event mapped by this mapper on the client
* @param {bool} options.decrypt decrypt event proactively
* @return {Function}
*/
MatrixClient.prototype.getEventMapper = function(options = undefined) {
Expand Down
14 changes: 14 additions & 0 deletions src/models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ function pendingEventsKey(roomId) {

utils.inherits(Room, EventEmitter);

Room.prototype.lazyDecryptEvents = async function() {
germain-gg marked this conversation as resolved.
Show resolved Hide resolved
return Promise.allSettled(this
.getUnfilteredTimelineSet()
.getTimelines()
.reduce((decryptionPromises, timeline) => {
return decryptionPromises.concat(
timeline
.getEvents()
.filter(event => event.isEncrypted() && !event.isBeingDecrypted())
.map(event => event.attemptDecryption(this._client._crypto, true))
);
}, []));
}

/**
* Gets the version of the room
* @returns {string} The version of the room, or null if it could not be determined
Expand Down
33 changes: 29 additions & 4 deletions src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,31 @@ SyncApi.prototype._syncFromCache = async function(savedSync) {
};

try {
await this._processSyncResponse(syncEventData, data);
await this._processSyncResponse(syncEventData, data, false);

const breadcrumbs = this.client.store.getAccountData("im.vector.setting.breadcrumbs");
germain-gg marked this conversation as resolved.
Show resolved Hide resolved
const breadcrumbsRooms = breadcrumbs?.getContent().recent_rooms || [];

this.client.getRooms().forEach(room => {
const readReceiptEventId = room.getEventReadUpTo(this.client.getUserId(), true);

const events = room.getLiveTimeline().getEvents();
const isInBreadcrumb = breadcrumbsRooms.includes(room.roomId);
const readReceiptTimelineIndex = events.findIndex(matrixEvent => {
return matrixEvent.event.event_id === readReceiptEventId
});
const decryptFromIndex = isInBreadcrumb
? 0
: readReceiptTimelineIndex

events
.slice(decryptFromIndex)
.forEach(event => {
if (event.isEncrypted() && !event.isBeingDecrypted()) {
event.attemptDecryption(this.client._crypto, true);
}
});
});
} catch (e) {
logger.error("Error processing cached sync", e.stack || e);
}
Expand Down Expand Up @@ -942,7 +966,7 @@ SyncApi.prototype._onSyncError = function(err, syncOptions) {
* @param {Object} data The response from /sync
*/
SyncApi.prototype._processSyncResponse = async function(
syncEventData, data,
syncEventData, data, decrypt = true
) {
const client = this.client;
const self = this;
Expand Down Expand Up @@ -1158,7 +1182,7 @@ SyncApi.prototype._processSyncResponse = async function(
await utils.promiseMapSeries(joinRooms, async function(joinObj) {
const room = joinObj.room;
const stateEvents = self._mapSyncEventsFormat(joinObj.state, room);
const timelineEvents = self._mapSyncEventsFormat(joinObj.timeline, room);
const timelineEvents = self._mapSyncEventsFormat(joinObj.timeline, room, decrypt);
const ephemeralEvents = self._mapSyncEventsFormat(joinObj.ephemeral);
const accountDataEvents = self._mapSyncEventsFormat(joinObj.account_data);

Expand Down Expand Up @@ -1518,11 +1542,12 @@ SyncApi.prototype._mapSyncResponseToRoomArray = function(obj) {
* @param {Room} room
* @return {MatrixEvent[]}
*/
SyncApi.prototype._mapSyncEventsFormat = function(obj, room) {
SyncApi.prototype._mapSyncEventsFormat = function(obj, room, decrypt = true) {
if (!obj || !utils.isArray(obj.events)) {
return [];
}
const mapper = this.client.getEventMapper();
const mapper = this.client.getEventMapper({ decrypt });
return obj.events.map(function(e) {
if (room) {
e.room_id = room.roomId;
Expand Down