mirror of
				https://github.com/vector-im/element-web.git
				synced 2025-11-04 02:02:14 +01:00 
			
		
		
		
	patch self-chats lazily in favor of awaiting sync state
This commit is contained in:
		
							parent
							
								
									abd9d3a11e
								
							
						
					
					
						commit
						e171296d51
					
				@ -37,11 +37,8 @@ export default class DMRoomMap {
 | 
				
			|||||||
        this._onAccountData = this._onAccountData.bind(this);
 | 
					        this._onAccountData = this._onAccountData.bind(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const mDirectEvent = matrixClient.getAccountData('m.direct');
 | 
					        const mDirectEvent = matrixClient.getAccountData('m.direct');
 | 
				
			||||||
        if (!mDirectEvent) {
 | 
					        this.mDirectEvent = mDirectEvent ? mDirectEvent.getContent() : {};
 | 
				
			||||||
            this.userToRooms = {};
 | 
					        this.userToRooms = null;
 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            this.userToRooms = mDirectEvent.getContent();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
@ -71,25 +68,11 @@ export default class DMRoomMap {
 | 
				
			|||||||
        this.matrixClient.removeListener("accountData", this._onAccountData);
 | 
					        this.matrixClient.removeListener("accountData", this._onAccountData);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async _onAccountData(ev) {
 | 
					    _onAccountData(ev) {
 | 
				
			||||||
        if (ev.getType() == 'm.direct') {
 | 
					        if (ev.getType() == 'm.direct') {
 | 
				
			||||||
            const userToRooms = this.matrixClient.getAccountData('m.direct').getContent() || {};
 | 
					            this.mDirectEvent = this.matrixClient.getAccountData('m.direct').getContent() || {};
 | 
				
			||||||
            const myUserId = this.matrixClient.getUserId();
 | 
					            this.userToRooms = null;
 | 
				
			||||||
            const selfDMs = userToRooms[myUserId];
 | 
					            this.roomToUser = null;
 | 
				
			||||||
            if (selfDMs && selfDMs.length) {
 | 
					 | 
				
			||||||
                const neededPatching = await this._patchUpSelfDMs(userToRooms);
 | 
					 | 
				
			||||||
                // to avoid multiple devices fighting to correct
 | 
					 | 
				
			||||||
                // the account data, only try to send the corrected
 | 
					 | 
				
			||||||
                // version once.
 | 
					 | 
				
			||||||
                console.warn(`Invalid m.direct account data detected ` +
 | 
					 | 
				
			||||||
                    `(self-chats that shouldn't be), patching it up.`);
 | 
					 | 
				
			||||||
                if (neededPatching && !this._hasSentOutPatchDirectAccountDataPatch) {
 | 
					 | 
				
			||||||
                    this._hasSentOutPatchDirectAccountDataPatch = true;
 | 
					 | 
				
			||||||
                    this.matrixClient.setAccountData('m.direct', userToRooms);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            this.userToRooms = userToRooms;
 | 
					 | 
				
			||||||
            this._populateRoomToUser();
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
@ -97,13 +80,10 @@ export default class DMRoomMap {
 | 
				
			|||||||
     * with ourself, not the other user. Fix it by guessing the other user and
 | 
					     * with ourself, not the other user. Fix it by guessing the other user and
 | 
				
			||||||
     * modifying userToRooms
 | 
					     * modifying userToRooms
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    async _patchUpSelfDMs(userToRooms) {
 | 
					    _patchUpSelfDMs(userToRooms) {
 | 
				
			||||||
        const myUserId = this.matrixClient.getUserId();
 | 
					        const myUserId = this.matrixClient.getUserId();
 | 
				
			||||||
        const selfRoomIds = userToRooms[myUserId];
 | 
					        const selfRoomIds = userToRooms[myUserId];
 | 
				
			||||||
        if (selfRoomIds) {
 | 
					        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?
 | 
					            // any self-chats that should not be self-chats?
 | 
				
			||||||
            const guessedUserIdsThatChanged = selfRoomIds.map((roomId) => {
 | 
					            const guessedUserIdsThatChanged = selfRoomIds.map((roomId) => {
 | 
				
			||||||
                const room = this.matrixClient.getRoom(roomId);
 | 
					                const room = this.matrixClient.getRoom(roomId);
 | 
				
			||||||
@ -136,19 +116,6 @@ 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) {
 | 
					    getDMRoomsForUserId(userId) {
 | 
				
			||||||
        // Here, we return the empty list if there are no rooms,
 | 
					        // Here, we return the empty list if there are no rooms,
 | 
				
			||||||
        // since the number of conversations you have with this user is zero.
 | 
					        // since the number of conversations you have with this user is zero.
 | 
				
			||||||
@ -177,9 +144,31 @@ export default class DMRoomMap {
 | 
				
			|||||||
        return this.roomToUser[roomId];
 | 
					        return this.roomToUser[roomId];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _getUserToRooms() {
 | 
				
			||||||
 | 
					        if (!this.userToRooms) {
 | 
				
			||||||
 | 
					            const userToRooms = this.mDirectEvent;
 | 
				
			||||||
 | 
					            const myUserId = this.matrixClient.getUserId();
 | 
				
			||||||
 | 
					            const selfDMs = userToRooms[myUserId];
 | 
				
			||||||
 | 
					            if (selfDMs && selfDMs.length) {
 | 
				
			||||||
 | 
					                const neededPatching = this._patchUpSelfDMs(userToRooms);
 | 
				
			||||||
 | 
					                // to avoid multiple devices fighting to correct
 | 
				
			||||||
 | 
					                // the account data, only try to send the corrected
 | 
				
			||||||
 | 
					                // version once.
 | 
				
			||||||
 | 
					                console.warn(`Invalid m.direct account data detected ` +
 | 
				
			||||||
 | 
					                    `(self-chats that shouldn't be), patching it up.`);
 | 
				
			||||||
 | 
					                if (neededPatching && !this._hasSentOutPatchDirectAccountDataPatch) {
 | 
				
			||||||
 | 
					                    this._hasSentOutPatchDirectAccountDataPatch = true;
 | 
				
			||||||
 | 
					                    this.matrixClient.setAccountData('m.direct', userToRooms);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            this.userToRooms = userToRooms;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return this.userToRooms;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _populateRoomToUser() {
 | 
					    _populateRoomToUser() {
 | 
				
			||||||
        this.roomToUser = {};
 | 
					        this.roomToUser = {};
 | 
				
			||||||
        for (const user of Object.keys(this.userToRooms)) {
 | 
					        for (const user of Object.keys(this._getUserToRooms())) {
 | 
				
			||||||
            for (const roomId of this.userToRooms[user]) {
 | 
					            for (const roomId of this.userToRooms[user]) {
 | 
				
			||||||
                this.roomToUser[roomId] = user;
 | 
					                this.roomToUser[roomId] = user;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user