diff --git a/src/base-apis.js b/src/base-apis.js
index d928c406e..a6465c701 100644
--- a/src/base-apis.js
+++ b/src/base-apis.js
@@ -1018,20 +1018,35 @@ MatrixBaseApis.prototype.uploadKeysRequest = function(content, opts, callback) {
*
* @param {string[]} userIds list of users to get keys for
*
- * @param {module:client.callback=} callback
+ * @param {Object=} opts
+ *
+ * @param {string=} opts.token sync token to pass in the query request, to help
+ * the HS give the most recent results
*
* @return {module:client.Promise} Resolves: result object. Rejects: with
* an error response ({@link module:http-api.MatrixError}).
*/
-MatrixBaseApis.prototype.downloadKeysForUsers = function(userIds, callback) {
- const downloadQuery = {};
-
- for (let i = 0; i < userIds.length; ++i) {
- downloadQuery[userIds[i]] = {};
+MatrixBaseApis.prototype.downloadKeysForUsers = function(userIds, opts) {
+ if (utils.isFunction(opts)) {
+ // opts used to be 'callback'.
+ throw new Error(
+ 'downloadKeysForUsers no longer accepts a callback parameter',
+ );
}
- const content = {device_keys: downloadQuery};
+ opts = opts || {};
+
+ const content = {
+ device_keys: {},
+ };
+ if ('token' in opts) {
+ content.token = opts.token;
+ }
+ userIds.forEach((u) => {
+ content.device_keys[u] = {};
+ });
+
return this._http.authedRequestWithPrefix(
- callback, "POST", "/keys/query", undefined, content,
+ undefined, "POST", "/keys/query", undefined, content,
httpApi.PREFIX_UNSTABLE,
);
};
diff --git a/src/client.js b/src/client.js
index a1ba21209..def7e5cd9 100644
--- a/src/client.js
+++ b/src/client.js
@@ -2638,8 +2638,6 @@ MatrixClient.prototype.startClient = function(opts) {
};
}
- this._clientOpts = opts;
-
if (this._crypto) {
this._crypto.uploadKeys(5).done();
const tenMinutes = 1000 * 60 * 10;
@@ -2657,6 +2655,13 @@ MatrixClient.prototype.startClient = function(opts) {
console.error("Still have sync object whilst not running: stopping old one");
this._syncApi.stop();
}
+
+ // shallow-copy the opts dict before modifying and storing it
+ opts = Object.assign({}, opts);
+
+ opts.crypto = this._crypto;
+ this._clientOpts = opts;
+
this._syncApi = new SyncApi(this, opts);
this._syncApi.sync();
};
@@ -3040,12 +3045,26 @@ module.exports.CRYPTO_ENABLED = CRYPTO_ENABLED;
*
*
* @event module:client~MatrixClient#"sync"
+ *
* @param {string} state An enum representing the syncing state. One of "PREPARED",
* "SYNCING", "ERROR", "STOPPED".
+ *
* @param {?string} prevState An enum representing the previous syncing state.
* One of "PREPARED", "SYNCING", "ERROR", "STOPPED" or null.
+ *
* @param {?Object} data Data about this transition.
+ *
* @param {MatrixError} data.err The matrix error if state=ERROR
.
+ *
+ * @param {String} data.oldSyncToken The 'since' token passed to /sync.
+ * null
for the first successful sync since this client was
+ * started. Only present if state=PREPARED
or
+ * state=SYNCING
.
+ *
+ * @param {String} data.nextSyncToken The 'next_batch' result from /sync, which
+ * will become the 'since' token for the next call to /sync. Only present if
+ * state=PREPARED
or state=SYNCING
.
+ *
* @example
* matrixClient.on("sync", function(state, prevState, data) {
* switch (state) {
diff --git a/src/crypto/DeviceList.js b/src/crypto/DeviceList.js
index 770938b17..342ff029a 100644
--- a/src/crypto/DeviceList.js
+++ b/src/crypto/DeviceList.js
@@ -41,6 +41,8 @@ export default class DeviceList {
// userId -> promise
this._keyDownloadsInProgressByUser = {};
+
+ this.lastKnownSyncToken = null;
}
/**
@@ -288,8 +290,13 @@ export default class DeviceList {
_doKeyDownloadForUsers(downloadUsers) {
console.log('Starting key download for ' + downloadUsers);
+ const token = this.lastKnownSyncToken;
+ const opts = {};
+ if (token) {
+ opts.token = token;
+ }
return this._baseApis.downloadKeysForUsers(
- downloadUsers,
+ downloadUsers, opts,
).then((res) => {
const dk = res.device_keys || {};
diff --git a/src/crypto/index.js b/src/crypto/index.js
index 6a326c328..3f3fc131a 100644
--- a/src/crypto/index.js
+++ b/src/crypto/index.js
@@ -55,8 +55,6 @@ function Crypto(baseApis, eventEmitter, sessionStore, userId, deviceId) {
this._userId = userId;
this._deviceId = deviceId;
- this._initialSyncCompleted = false;
-
this._olmDevice = new OlmDevice(sessionStore);
this._deviceList = new DeviceList(baseApis, sessionStore, this._olmDevice);
@@ -115,6 +113,9 @@ function _registerEventHandlers(crypto, eventEmitter) {
const rooms = eventEmitter.getRooms();
crypto._onInitialSyncCompleted(rooms);
}
+ if (syncState === "SYNCING") {
+ crypto._onSyncCompleted(data);
+ }
} catch (e) {
console.error("Error handling sync", e);
}
@@ -689,6 +690,18 @@ Crypto.prototype.decryptEvent = function(event) {
alg.decryptEvent(event);
};
+/**
+ * Handle the notification from /sync that a user has updated their device list.
+ *
+ * @param {String} userId
+ */
+Crypto.prototype.userDeviceListChanged = function(userId) {
+ this._deviceList.invalidateUserDeviceList(userId);
+
+ // don't flush the outdated device list yet - we do it once we finish
+ // processing the sync.
+};
+
/**
* handle an m.room.encryption event
*
@@ -716,11 +729,6 @@ Crypto.prototype._onCryptoEvent = function(event) {
* @param {module:models/room[]} rooms list of rooms the client knows about
*/
Crypto.prototype._onInitialSyncCompleted = function(rooms) {
- this._initialSyncCompleted = true;
-
- // catch up on any m.new_device events which arrived during the initial sync.
- this._deviceList.refreshOutdatedDeviceLists().done();
-
if (this._sessionStore.getDeviceAnnounced()) {
return;
}
@@ -779,6 +787,12 @@ Crypto.prototype._onInitialSyncCompleted = function(rooms) {
});
};
+Crypto.prototype._onSyncCompleted = function(syncData) {
+ // catch up on any new devices we got told about during the sync.
+ this._deviceList.lastKnownSyncToken = syncData.nextSyncToken;
+ this._deviceList.refreshOutdatedDeviceLists().done();
+};
+
/**
* Handle a key event
*
@@ -852,12 +866,6 @@ Crypto.prototype._onNewDeviceEvent = function(event) {
}
this._deviceList.invalidateUserDeviceList(userId);
-
- // we delay handling these until the intialsync has completed, so that we
- // can do all of them together.
- if (this._initialSyncCompleted) {
- this._deviceList.refreshOutdatedDeviceLists().done();
- }
};
diff --git a/src/sync.js b/src/sync.js
index 90ba408d3..facf5d6ed 100644
--- a/src/sync.js
+++ b/src/sync.js
@@ -58,6 +58,7 @@ function debuglog() {
* @constructor
* @param {MatrixClient} client The matrix client instance to use.
* @param {Object} opts Config options
+ * @param {module:crypto=} opts.crypto Crypto manager
*/
function SyncApi(client, opts) {
this.client = client;
@@ -529,13 +530,18 @@ SyncApi.prototype._sync = function(syncOptions) {
}
// emit synced events
+ const syncEventData = {
+ oldSyncToken: syncToken,
+ nextSyncToken: data.next_batch,
+ };
+
if (!syncOptions.hasSyncedBefore) {
- self._updateSyncState("PREPARED");
+ self._updateSyncState("PREPARED", syncEventData);
syncOptions.hasSyncedBefore = true;
}
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates
- self._updateSyncState("SYNCING");
+ self._updateSyncState("SYNCING", syncEventData);
self._sync(syncOptions);
}, function(err) {
@@ -584,6 +590,7 @@ SyncApi.prototype._processSyncResponse = function(syncToken, data) {
// next_batch: $token,
// presence: { events: [] },
// account_data: { events: [] },
+ // device_lists: { changed: ["@user:server", ... ]},
// to_device: { events: [] },
// rooms: {
// invite: {
@@ -859,6 +866,13 @@ SyncApi.prototype._processSyncResponse = function(syncToken, data) {
client.getNotifTimelineSet().addLiveEvent(event);
});
}
+
+ // Handle device list updates
+ if (this.opts.crypto && data.device_lists && data.device_lists.changed) {
+ data.device_lists.changed.forEach((u) => {
+ this.opts.crypto.userDeviceListChanged(u);
+ });
+ }
};
/**