diff --git a/lib/models/room-state.js b/lib/models/room-state.js index 0044ef65a..765c96c86 100644 --- a/lib/models/room-state.js +++ b/lib/models/room-state.js @@ -206,15 +206,19 @@ function _updateDisplayNameCache(roomState, userId, displayName) { var oldName = roomState._userIdsToDisplayNames[userId]; delete roomState._userIdsToDisplayNames[userId]; if (oldName) { - var existing = roomState._displayNameToUserIds[oldName] || []; - for (var i = 0; i < existing.length; i++) { - if (existing[i] === userId) { + // Remove the old name from the cache. + // We clobber the user_id > name lookup but the name -> [user_id] lookup + // means we need to remove that user ID from that array rather than nuking + // the lot. + var existingUserIds = roomState._displayNameToUserIds[oldName] || []; + for (var i = 0; i < existingUserIds.length; i++) { + if (existingUserIds[i] === userId) { // remove this user ID from this array - existing.splice(i, 1); + existingUserIds.splice(i, 1); i--; } } - roomState._displayNameToUserIds[oldName] = existing; + roomState._displayNameToUserIds[oldName] = existingUserIds; } roomState._userIdsToDisplayNames[userId] = displayName;