index : matrix-js-sdk

My fork of matrix-js-sdk

diff options
context:
space:
mode:
authorTravis Ralston <[email protected]>2020-06-22 14:10:16 -0600
committerGitHub <[email protected]>2020-06-22 14:10:16 -0600
commit7b8102a42c54ab3bbd29bec2791e88d207294fcc (patch)
treee12d645d690d0b916810ae5f317a8bea12f39114
parent8a6cd48b8ec57df2b87deca9ac90a6ad9533db1f (diff)
parentdbe2f5e4db0039666dd8fb26c3dec5f5a58ef654 (diff)
downloadmatrix-js-sdk-7b8102a42c54ab3bbd29bec2791e88d207294fcc.tar.gz
Merge pull request #1410 from matrix-org/travis/room-list/setting-diff
Raise the last known account data / state event for an update
-rw-r--r--src/client.js3
-rw-r--r--src/crypto/EncryptionSetup.js3
-rw-r--r--src/models/room-state.js14
-rw-r--r--src/models/room.js7
-rw-r--r--src/sync.js9
5 files changed, 28 insertions, 8 deletions
diff --git a/src/client.js b/src/client.js
index 0436e94d..a646500a 100644
--- a/src/client.js
+++ b/src/client.js
@@ -5602,8 +5602,9 @@ MatrixClient.prototype.generateClientSecret = function() {
* Fires whenever new user-scoped account_data is added.
* @event module:client~MatrixClient#"accountData"
* @param {MatrixEvent} event The event describing the account_data just added
+ * @param {MatrixEvent} event The previous account data, if known.
* @example
- * matrixClient.on("accountData", function(event){
+ * matrixClient.on("accountData", function(event, oldEvent){
* myAccountData[event.type] = event.content;
* });
*/
diff --git a/src/crypto/EncryptionSetup.js b/src/crypto/EncryptionSetup.js
index 549ec5e3..38aee333 100644
--- a/src/crypto/EncryptionSetup.js
+++ b/src/crypto/EncryptionSetup.js
@@ -259,13 +259,14 @@ class AccountDataClientAdapter extends EventEmitter {
* @return {Promise}
*/
setAccountData(type, content) {
+ const lastEvent = this._values.get(type);
this._values.set(type, content);
// ensure accountData is emitted on the next tick,
// as SecretStorage listens for it while calling this method
// and it seems to rely on this.
return Promise.resolve().then(() => {
const event = new MatrixEvent({type, content});
- this.emit("accountData", event);
+ this.emit("accountData", event, lastEvent);
});
}
}
diff --git a/src/models/room-state.js b/src/models/room-state.js
index 70590b46..a7d1072e 100644
--- a/src/models/room-state.js
+++ b/src/models/room-state.js
@@ -306,6 +306,7 @@ RoomState.prototype.setStateEvents = function(stateEvents) {
return;
}
+ const lastStateEvent = self._getStateEventMatching(event);
self._setStateEvent(event);
if (event.getType() === "m.room.member") {
_updateDisplayNameCache(
@@ -313,7 +314,7 @@ RoomState.prototype.setStateEvents = function(stateEvents) {
);
_updateThirdPartyTokenCache(self, event);
}
- self.emit("RoomState.events", event, self);
+ self.emit("RoomState.events", event, self, lastStateEvent);
});
// update higher level data structures. This needs to be done AFTER the
@@ -391,6 +392,11 @@ RoomState.prototype._setStateEvent = function(event) {
this.events[event.getType()][event.getStateKey()] = event;
};
+RoomState.prototype._getStateEventMatching = function(event) {
+ if (!this.events[event.getType()]) return null;
+ return this.events[event.getType()][event.getStateKey()];
+};
+
RoomState.prototype._updateMember = function(member) {
// this member may have a power level already, so set it.
const pwrLvlEvent = this.getStateEvents("m.room.power_levels", "");
@@ -769,8 +775,12 @@ function _updateDisplayNameCache(roomState, userId, displayName) {
* @param {MatrixEvent} event The matrix event which caused this event to fire.
* @param {RoomState} state The room state whose RoomState.events dictionary
* was updated.
+ * @param {MatrixEvent} prevEvent The event being replaced by the new state, if
+ * known. Note that this can differ from `getPrevContent()` on the new state event
+ * as this is the store's view of the last state, not the previous state provided
+ * by the server.
* @example
- * matrixClient.on("RoomState.events", function(event, state){
+ * matrixClient.on("RoomState.events", function(event, state, prevEvent){
* var newStateEvent = event;
* });
*/
diff --git a/src/models/room.js b/src/models/room.js
index abed7252..850542c9 100644
--- a/src/models/room.js
+++ b/src/models/room.js
@@ -1793,8 +1793,9 @@ Room.prototype.addAccountData = function(events) {
if (event.getType() === "m.tag") {
this.addTags(event);
}
+ const lastEvent = this.accountData[event.getType()];
this.accountData[event.getType()] = event;
- this.emit("Room.accountData", event, this);
+ this.emit("Room.accountData", event, this, lastEvent);
}
};
@@ -1991,8 +1992,10 @@ function memberNamesToRoomName(names, count = (names.length + 1)) {
* @event module:client~MatrixClient#"Room.accountData"
* @param {event} event The account_data event
* @param {Room} room The room whose account_data was updated.
+ * @param {MatrixEvent} prevEvent The event being replaced by
+ * the new account data, if known.
* @example
- * matrixClient.on("Room.accountData", function(event, room){
+ * matrixClient.on("Room.accountData", function(event, room, oldEvent){
* if (event.getType() === "m.room.colorscheme") {
* applyColorScheme(event.getContents());
* }
diff --git a/src/sync.js b/src/sync.js
index 142e85fb..015144a4 100644
--- a/src/sync.js
+++ b/src/sync.js
@@ -1023,18 +1023,23 @@ SyncApi.prototype._processSyncResponse = async function(
// handle non-room account_data
if (data.account_data && utils.isArray(data.account_data.events)) {
const events = data.account_data.events.map(client.getEventMapper());
+ const prevEventsMap = events.reduce((m, c) => {
+ m[c.getId()] = client.store.getAccountData(c.getType());
+ return m;
+ }, {});
client.store.storeAccountDataEvents(events);
events.forEach(
function(accountDataEvent) {
// Honour push rules that come down the sync stream but also
// honour push rules that were previously cached. Base rules
- // will be updated when we recieve push rules via getPushRules
+ // will be updated when we receive push rules via getPushRules
// (see SyncApi.prototype.sync) before syncing over the network.
if (accountDataEvent.getType() === 'm.push_rules') {
const rules = accountDataEvent.getContent();
client.pushRules = PushProcessor.rewriteDefaultRules(rules);
}
- client.emit("accountData", accountDataEvent);
+ const prevEvent = prevEventsMap[accountDataEvent.getId()];
+ client.emit("accountData", accountDataEvent, prevEvent);
return accountDataEvent;
},
);