Early preparations for PNP Contact Merging

This commit is contained in:
Scott Nonnenberg 2022-08-09 14:39:00 -07:00 committed by GitHub
parent 2f5dd73e58
commit faf6c41332
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 1572 additions and 447 deletions

View file

@ -1275,11 +1275,11 @@ export class ConversationModel extends window.Backbone
const e164 = message.get('source');
const sourceDevice = message.get('sourceDevice');
const sourceId = window.ConversationController.ensureContactIds({
const source = window.ConversationController.lookupOrCreate({
uuid,
e164,
});
const typingToken = `${sourceId}.${sourceDevice}`;
const typingToken = `${source?.id}.${sourceDevice}`;
// Clear typing indicator for a given contact if we receive a message from them
this.clearContactTypingTimer(typingToken);
@ -1869,10 +1869,10 @@ export class ConversationModel extends window.Backbone
updateE164(e164?: string | null): void {
const oldValue = this.get('e164');
if (e164 && e164 !== oldValue) {
this.set('e164', e164);
if (e164 !== oldValue) {
this.set('e164', e164 || undefined);
if (oldValue) {
if (oldValue && e164) {
this.addChangeNumberNotification(oldValue, e164);
}
@ -1883,13 +1883,32 @@ export class ConversationModel extends window.Backbone
updateUuid(uuid?: string): void {
const oldValue = this.get('uuid');
if (uuid && uuid !== oldValue) {
this.set('uuid', UUID.cast(uuid.toLowerCase()));
if (uuid !== oldValue) {
this.set('uuid', uuid ? UUID.cast(uuid.toLowerCase()) : undefined);
window.Signal.Data.updateConversation(this.attributes);
this.trigger('idUpdated', this, 'uuid', oldValue);
}
}
updatePni(pni?: string): void {
const oldValue = this.get('pni');
if (pni !== oldValue) {
this.set('pni', pni ? UUID.cast(pni.toLowerCase()) : undefined);
if (
oldValue &&
pni &&
(!this.get('uuid') || this.get('uuid') === oldValue)
) {
// TODO: DESKTOP-3974
this.addKeyChange(UUID.checkedLookup(oldValue));
}
window.Signal.Data.updateConversation(this.attributes);
this.trigger('idUpdated', this, 'pni', oldValue);
}
}
updateGroupId(groupId?: string): void {
const oldValue = this.get('groupId');
if (groupId && groupId !== oldValue) {
@ -5389,6 +5408,9 @@ window.Whisper.ConversationCollection = window.Backbone.Collection.extend({
if (idProp === 'uuid') {
delete this._byUuid[oldValue];
}
if (idProp === 'pni') {
delete this._byPni[oldValue];
}
if (idProp === 'groupId') {
delete this._byGroupId[oldValue];
}
@ -5401,6 +5423,10 @@ window.Whisper.ConversationCollection = window.Backbone.Collection.extend({
if (uuid) {
this._byUuid[uuid] = model;
}
const pni = model.get('pni');
if (pni) {
this._byPni[pni] = model;
}
const groupId = model.get('groupId');
if (groupId) {
this._byGroupId[groupId] = model;
@ -5441,6 +5467,16 @@ window.Whisper.ConversationCollection = window.Backbone.Collection.extend({
}
}
const pni = model.get('pni');
if (pni) {
const existing = this._byPni[pni];
// Prefer the contact with both uuid and pni
if (!existing || (existing && !existing.get('uuid'))) {
this._byPni[pni] = model;
}
}
const groupId = model.get('groupId');
if (groupId) {
this._byGroupId[groupId] = model;
@ -5451,6 +5487,7 @@ window.Whisper.ConversationCollection = window.Backbone.Collection.extend({
eraseLookups() {
this._byE164 = Object.create(null);
this._byUuid = Object.create(null);
this._byPni = Object.create(null);
this._byGroupId = Object.create(null);
},
@ -5510,6 +5547,7 @@ window.Whisper.ConversationCollection = window.Backbone.Collection.extend({
this._byE164[id] ||
this._byE164[`+${id}`] ||
this._byUuid[id] ||
this._byPni[id] ||
this._byGroupId[id] ||
window.Backbone.Collection.prototype.get.call(this, id)
);

View file

@ -291,12 +291,12 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
const sourceDevice = this.get('sourceDevice');
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const sourceId = window.ConversationController.ensureContactIds({
const conversation = window.ConversationController.lookupOrCreate({
e164: source,
uuid: sourceUuid,
})!;
return `${sourceId}.${sourceDevice}-${sentAt}`;
return `${conversation?.id}.${sourceDevice}-${sentAt}`;
}
getReceivedAt(): number {
@ -2137,14 +2137,13 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
return;
}
const destinationConversationId =
window.ConversationController.ensureContactIds({
uuid: destinationUuid,
e164: destination,
highTrust: true,
const destinationConversation =
window.ConversationController.maybeMergeContacts({
aci: destinationUuid,
e164: destination || undefined,
reason: `handleDataMessage(${initialMessage.timestamp})`,
});
if (!destinationConversationId) {
if (!destinationConversation) {
return;
}
@ -2155,9 +2154,9 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
const previousSendState = getOwn(
sendStateByConversationId,
destinationConversationId
destinationConversation.id
);
sendStateByConversationId[destinationConversationId] =
sendStateByConversationId[destinationConversation.id] =
previousSendState
? sendStateReducer(previousSendState, {
type: SendActionType.Sent,
@ -2274,7 +2273,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
UUIDKind.ACI
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const senderId = window.ConversationController.ensureContactIds({
const sender = window.ConversationController.lookupOrCreate({
e164: source,
uuid: sourceUuid,
})!;
@ -2348,7 +2347,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
// Drop incoming messages to announcement only groups where sender is not admin
if (
conversation.get('announcementsOnly') &&
!conversation.isAdmin(UUID.checkedLookup(senderId))
!conversation.isAdmin(UUID.checkedLookup(sender?.id))
) {
confirm();
return;
@ -2565,8 +2564,6 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
conversation.set({ addedBy: getContactId(message.attributes) });
}
} else if (initialMessage.group.type === GROUP_TYPES.QUIT) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const sender = window.ConversationController.get(senderId)!;
const inGroup = Boolean(
sender &&
(conversation.get('members') || []).includes(sender.id)
@ -2682,14 +2679,11 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
} else if (isDirectConversation(conversation.attributes)) {
conversation.setProfileKey(profileKey);
} else {
const localId = window.ConversationController.ensureContactIds({
const local = window.ConversationController.lookupOrCreate({
e164: source,
uuid: sourceUuid,
});
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
window.ConversationController.get(localId)!.setProfileKey(
profileKey
);
local?.setProfileKey(profileKey);
}
}