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)
This commit is contained in:
Bruno Windels 2018-09-04 13:03:55 +02:00
parent 727082b659
commit c12abab52d

View File

@ -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.