1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Do /keys/changes before second /sync

This will avoid races between /keys/changes and /syncs.
This commit is contained in:
Richard van der Hoff
2017-09-08 14:20:34 +01:00
parent 5e8e56caf9
commit ae8fc64394
2 changed files with 26 additions and 37 deletions

View File

@@ -69,7 +69,6 @@ function Crypto(baseApis, sessionStore, userId, deviceId,
this._olmDevice = new OlmDevice(sessionStore);
this._deviceList = new DeviceList(baseApis, sessionStore, this._olmDevice);
this._initialDeviceListInvalidationPending = false;
// the last time we did a check for the number of one-time-keys on the
// server.
@@ -150,15 +149,6 @@ Crypto.prototype.init = async function() {
*/
Crypto.prototype.registerEventHandlers = function(eventEmitter) {
const crypto = this;
eventEmitter.on("sync", function(syncState, oldState, data) {
try {
if (syncState === "SYNCING") {
crypto._onSyncCompleted(data);
}
} catch (e) {
console.error("Error handling sync", e);
}
});
eventEmitter.on("RoomMember.membership", function(event, member, oldMembership) {
try {
@@ -248,7 +238,7 @@ Crypto.prototype.uploadDeviceKeys = function() {
/**
* Stores the current one_time_key count which will be handled later (in a call of
* _onSyncCompleted). The count is e.g. coming from a /sync response.
* onSyncCompleted). The count is e.g. coming from a /sync response.
*
* @param {Number} currentCount The current count of one_time_keys to be stored
*/
@@ -837,7 +827,7 @@ Crypto.prototype.onCryptoEvent = async function(event) {
try {
// inhibit the device list refresh for now - it will happen once we've
// finished processing the sync, in _onSyncCompleted.
// finished processing the sync, in onSyncCompleted.
await this.setRoomEncryption(roomId, content, true);
} catch (e) {
console.error("Error configuring encryption in room " + roomId +
@@ -853,7 +843,7 @@ Crypto.prototype.onCryptoEvent = async function(event) {
*
* @param {Object} syncData the data from the 'MatrixClient.sync' event
*/
Crypto.prototype._onSyncCompleted = function(syncData) {
Crypto.prototype.onSyncCompleted = async function(syncData) {
const nextSyncToken = syncData.nextSyncToken;
if (!syncData.oldSyncToken) {
@@ -863,18 +853,15 @@ Crypto.prototype._onSyncCompleted = function(syncData) {
// invalidate devices which have changed since then.
const oldSyncToken = this._sessionStore.getEndToEndDeviceSyncToken();
if (oldSyncToken !== null) {
this._initialDeviceListInvalidationPending = true;
this._invalidateDeviceListsSince(
oldSyncToken, nextSyncToken,
).catch((e) => {
try {
await this._invalidateDeviceListsSince(
oldSyncToken, nextSyncToken,
);
} catch (e) {
// if that failed, we fall back to invalidating everyone.
console.warn("Error fetching changed device list", e);
this._deviceList.invalidateAllDeviceLists();
}).done(() => {
this._initialDeviceListInvalidationPending = false;
this._deviceList.lastKnownSyncToken = nextSyncToken;
this._deviceList.refreshOutdatedDeviceLists();
});
}
} else {
// otherwise, we have to invalidate all devices for all users we
// are tracking.
@@ -884,14 +871,12 @@ Crypto.prototype._onSyncCompleted = function(syncData) {
}
}
if (!this._initialDeviceListInvalidationPending) {
// we can now store our sync token so that we can get an update on
// restart rather than having to invalidate everyone.
//
// (we don't really need to do this on every sync - we could just
// do it periodically)
this._sessionStore.storeEndToEndDeviceSyncToken(nextSyncToken);
}
// we can now store our sync token so that we can get an update on
// restart rather than having to invalidate everyone.
//
// (we don't really need to do this on every sync - we could just
// do it periodically)
this._sessionStore.storeEndToEndDeviceSyncToken(nextSyncToken);
// catch up on any new devices we got told about during the sync.
this._deviceList.lastKnownSyncToken = nextSyncToken;
@@ -925,13 +910,11 @@ Crypto.prototype._invalidateDeviceListsSince = function(
).then((r) => {
console.log("got key changes since", oldSyncToken, ":", r.changed);
if (!r.changed || !Array.isArray(r.changed)) {
return;
if (r.changed && Array.isArray(r.changed)) {
r.changed.forEach((u) => {
this._deviceList.invalidateUserDeviceList(u);
});
}
r.changed.forEach((u) => {
this._deviceList.invalidateUserDeviceList(u);
});
});
};

View File

@@ -634,8 +634,14 @@ SyncApi.prototype._sync = async function(syncOptions) {
syncOptions.hasSyncedBefore = true;
}
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates
if (!isCachedResponse) {
// tell the crypto module to do its processing. It may block (to do a
// /keys/changes request).
if (this.opts.crypto) {
await this.opts.crypto.onSyncCompleted(syncEventData);
}
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates
this._updateSyncState("SYNCING", syncEventData);
// tell databases that everything is now in a consistent state and can be