diff --git a/js/models/conversations.js b/js/models/conversations.js index 00fdbee59..02b9c8abd 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -2860,9 +2860,7 @@ * than just their id. */ initialize() { - this._byE164 = Object.create(null); - this._byUuid = Object.create(null); - this._byGroupId = Object.create(null); + this.eraseLookups(); this.on('idUpdated', (model, idProp, oldValue) => { if (oldValue) { if (idProp === 'e164') { @@ -2889,14 +2887,16 @@ reset(...args) { Backbone.Collection.prototype.reset.apply(this, args); - this._byE164 = Object.create(null); - this._byUuid = Object.create(null); - this._byGroupId = Object.create(null); + this.resetLookups(); }, - add(...models) { - const res = Backbone.Collection.prototype.add.apply(this, models); - [].concat(res).forEach(model => { + resetLookups() { + this.eraseLookups(); + this.generateLookups(this.models); + }, + + generateLookups(models) { + models.forEach(model => { const e164 = model.get('e164'); if (e164) { const existing = this._byE164[e164]; @@ -2922,7 +2922,20 @@ this._byGroupId[groupId] = model; } }); - return res; + }, + + eraseLookups() { + this._byE164 = Object.create(null); + this._byUuid = Object.create(null); + this._byGroupId = Object.create(null); + }, + + add(...models) { + const result = Backbone.Collection.prototype.add.apply(this, models); + + this.generateLookups(Array.isArray(result) ? result.slice(0) : [result]); + + return result; }, /** diff --git a/ts/ConversationController.ts b/ts/ConversationController.ts index b8728948c..f2e6e166a 100644 --- a/ts/ConversationController.ts +++ b/ts/ConversationController.ts @@ -366,6 +366,7 @@ export class ConversationController { // Conflict: If e164 match has no UUID, we merge. We prefer the UUID match. // Note: no await here, we want to keep this function synchronous + convoUuid.updateE164(e164); this.combineContacts(convoUuid, convoE164) .then(() => { // If the old conversation was currently displayed, we load the new one @@ -537,21 +538,13 @@ export class ConversationController { 'combineContacts: Eliminate old conversation from ConversationController lookups' ); this._conversations.remove(obsolete); - this.regenerateLookups(); + this._conversations.resetLookups(); window.log.warn('combineContacts: Complete!', { obsolete: obsoleteId, current: currentId, }); } - regenerateLookups() { - const models = [...this._conversations.models]; - this.reset(); - this._conversations.add(models); - - // We force the initial fetch to be true - this._initialFetchComplete = true; - } /** * Given a groupId and optional additional initialization properties, * ensures the existence of a group conversation and returns a string