Merge pull request #4929 from matrix-org/travis/room-list/protect-lost-rooms

Protect rooms from getting lost due to complex transitions
This commit is contained in:
Travis Ralston 2020-07-08 11:58:13 -06:00 committed by GitHub
commit d95e3101c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -655,18 +655,35 @@ export class Algorithm extends EventEmitter {
cause = RoomUpdateCause.PossibleTagChange; cause = RoomUpdateCause.PossibleTagChange;
} }
// If we have tags for a room and don't have the room referenced, the room reference // Check to see if the room is known first
// probably changed. We need to swap out the problematic reference. let knownRoomRef = this.rooms.includes(room);
if (hasTags && !this.rooms.includes(room) && !isSticky) { if (hasTags && !knownRoomRef) {
console.warn(`${room.roomId} is missing from room array but is known - trying to find duplicate`); console.warn(`${room.roomId} might be a reference change - attempting to update reference`);
this.rooms = this.rooms.map(r => r.roomId === room.roomId ? room : r); this.rooms = this.rooms.map(r => r.roomId === room.roomId ? room : r);
knownRoomRef = this.rooms.includes(room);
// Sanity check if (!knownRoomRef) {
if (!this.rooms.includes(room)) { console.warn(`${room.roomId} is still not referenced. It may be sticky.`);
throw new Error(`Failed to replace ${room.roomId} with an updated reference`);
} }
} }
if (hasTags && isForLastSticky && !knownRoomRef) {
// we have a fairly good chance at losing a room right now. Under some circumstances,
// we can end up with a room which transitions references and tag changes, then gets
// lost when the sticky room changes. To counter this, we try and add the room to the
// list manually as the condition below to update the reference will fail.
//
// Other conditions *should* result in the room being sorted into the right place.
console.warn(`${room.roomId} was about to be lost - inserting at end of room list`);
this.rooms.push(room);
knownRoomRef = true;
}
// If we have tags for a room and don't have the room referenced, something went horribly
// wrong - the reference should have been updated above.
if (hasTags && !knownRoomRef && !isSticky) {
throw new Error(`${room.roomId} is missing from room array but is known - trying to find duplicate`);
}
// Like above, update the reference to the sticky room if we need to // Like above, update the reference to the sticky room if we need to
if (hasTags && isSticky) { if (hasTags && isSticky) {
// Go directly in and set the sticky room's new reference, being careful not // Go directly in and set the sticky room's new reference, being careful not