From c12abab52d6b4f1be95084fbb4b80578395f480a Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 4 Sep 2018 13:03:55 +0200 Subject: [PATCH] wait until rooms are available as accountData get processed before rooms, during initial sync or loading sync from cache, accountData gets emitted before any room is available, hence our patching wasn't doing anything. Just as well, because it would have failed (see next commits) --- src/utils/DMRoomMap.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/utils/DMRoomMap.js b/src/utils/DMRoomMap.js index 0c5e696c5f..789970c744 100644 --- a/src/utils/DMRoomMap.js +++ b/src/utils/DMRoomMap.js @@ -70,13 +70,13 @@ export default class DMRoomMap { this.matrixClient.removeListener("accountData", this._onAccountData); } - _onAccountData(ev) { + async _onAccountData(ev) { if (ev.getType() == 'm.direct') { const userToRooms = this.matrixClient.getAccountData('m.direct').getContent() || {}; const myUserId = this.matrixClient.getUserId(); const selfDMs = userToRooms[myUserId]; if (selfDMs && selfDMs.length) { - const neededPatching = this._patchUpSelfDMs(userToRooms); + const neededPatching = await this._patchUpSelfDMs(userToRooms); // to avoid multiple devices fighting to correct // the account data, only try to send the corrected // version once. @@ -94,10 +94,13 @@ export default class DMRoomMap { * with ourself, not the other user. Fix it by guessing the other user and * modifying userToRooms */ - _patchUpSelfDMs(userToRooms) { + async _patchUpSelfDMs(userToRooms) { const myUserId = this.matrixClient.getUserId(); const selfRoomIds = userToRooms[myUserId]; if (selfRoomIds) { + // account data gets emitted before the rooms are available + // so wait for the sync to be ready and then read the rooms. + await this._waitForSyncReady(); // any self-chats that should not be self-chats? const guessedUserIdsThatChanged = selfRoomIds.map((roomId) => { const room = this.matrixClient.getRoom(roomId); @@ -135,6 +138,19 @@ export default class DMRoomMap { } } + _waitForSyncReady() { + return new Promise((resolve) => { + const syncState = this.matrixClient.getSyncState(); + if (syncState === 'PREPARED' || syncState === 'SYNCING') { + resolve(); + } else { + // if we already got an accountData event, + // next sync should not be ERROR, so just resolve + this.matrixClient.once('sync', () => resolve()); + } + }); + } + getDMRoomsForUserId(userId) { // Here, we return the empty list if there are no rooms, // since the number of conversations you have with this user is zero.