Merge contacts when we discover split or duplicated contacts

This commit is contained in:
Scott Nonnenberg 2020-07-10 11:28:49 -07:00
parent 68e432188b
commit 901179440f
32 changed files with 1199 additions and 824 deletions

View file

@ -193,30 +193,20 @@
// Other top-level prop-generation
getPropsForSearchResult() {
const sourceE164 = this.getSource();
const sourceUuid = this.getSourceUuid();
const fromContact = this.findAndFormatContact(sourceE164 || sourceUuid);
const ourId = ConversationController.getOurConversationId();
const sourceId = this.getContactId();
const fromContact = this.findAndFormatContact(sourceId);
if (
(sourceE164 && sourceE164 === this.OUR_NUMBER) ||
(sourceUuid && sourceUuid === this.OUR_UUID)
) {
if (ourId === sourceId) {
fromContact.isMe = true;
}
const convo = this.getConversation();
let to = convo ? this.findAndFormatContact(convo.get('id')) : {};
const to = convo ? this.findAndFormatContact(convo.get('id')) : {};
if (convo && convo.isMe()) {
if (to && convo && convo.isMe()) {
to.isMe = true;
} else if (
(sourceE164 && convo && sourceE164 === convo.get('e164')) ||
(sourceUuid && convo && sourceUuid === convo.get('uuid'))
) {
to = {
isMe: true,
};
}
return {
@ -237,10 +227,10 @@
const unidentifiedLookup = (
this.get('unidentifiedDeliveries') || []
).reduce((accumulator, uuidOrE164) => {
).reduce((accumulator, identifier) => {
// eslint-disable-next-line no-param-reassign
accumulator[
ConversationController.getConversationId(uuidOrE164)
ConversationController.getConversationId(identifier)
] = true;
return accumulator;
}, Object.create(null));
@ -249,7 +239,7 @@
// Older messages don't have the recipients included on the message, so we fall
// back to the conversation's current recipients
const conversationIds = this.isIncoming()
? [this.getContact().get('id')]
? [this.getContactId()]
: _.union(
(this.get('sent_to') || []).map(id =>
ConversationController.getConversationId(id)
@ -379,11 +369,11 @@
getPropsForUnsupportedMessage() {
const requiredVersion = this.get('requiredProtocolVersion');
const canProcessNow = this.CURRENT_PROTOCOL_VERSION >= requiredVersion;
const phoneNumber = this.getSource();
const sourceId = this.getContactId();
return {
canProcessNow,
contact: this.findAndFormatContact(phoneNumber),
contact: this.findAndFormatContact(sourceId),
};
},
getPropsForTimerNotification() {
@ -396,8 +386,14 @@
const timespan = Whisper.ExpirationTimerOptions.getName(expireTimer || 0);
const disabled = !expireTimer;
const sourceId = ConversationController.ensureContactIds({
e164: source,
uuid: sourceUuid,
});
const ourId = ConversationController.getOurConversationId();
const basicProps = {
...this.findAndFormatContact(source),
...this.findAndFormatContact(sourceId),
type: 'fromOther',
timespan,
disabled,
@ -408,7 +404,7 @@
...basicProps,
type: 'fromSync',
};
} else if (source === this.OUR_NUMBER || sourceUuid === this.OUR_UUID) {
} else if (sourceId && sourceId === ourId) {
return {
...basicProps,
type: 'fromMe',
@ -430,12 +426,12 @@
getPropsForVerificationNotification() {
const type = this.get('verified') ? 'markVerified' : 'markNotVerified';
const isLocal = this.get('local');
const phoneNumber = this.get('verifiedChanged');
const identifier = this.get('verifiedChanged');
return {
type,
isLocal,
contact: this.findAndFormatContact(phoneNumber),
contact: this.findAndFormatContact(identifier),
};
},
getPropsForGroupNotification() {
@ -460,7 +456,7 @@
Array.isArray(groupUpdate.joined)
? groupUpdate.joined
: [groupUpdate.joined],
phoneNumber => this.findAndFormatContact(phoneNumber)
identifier => this.findAndFormatContact(identifier)
),
});
}
@ -477,7 +473,7 @@
Array.isArray(groupUpdate.left)
? groupUpdate.left
: [groupUpdate.left],
phoneNumber => this.findAndFormatContact(phoneNumber)
identifier => this.findAndFormatContact(identifier)
),
});
}
@ -495,9 +491,8 @@
});
}
const sourceE164 = this.getSource();
const sourceUuid = this.getSourceUuid();
const from = this.findAndFormatContact(sourceE164 || sourceUuid);
const sourceId = this.getContactId();
const from = this.findAndFormatContact(sourceId);
return {
from,
@ -537,10 +532,9 @@
.map(attachment => this.getPropsForAttachment(attachment));
},
getPropsForMessage() {
const sourceE164 = this.getSource();
const sourceUuid = this.getSourceUuid();
const contact = this.findAndFormatContact(sourceE164 || sourceUuid);
const contactModel = this.findContact(sourceE164 || sourceUuid);
const sourceId = this.getContactId();
const contact = this.findAndFormatContact(sourceId);
const contactModel = this.findContact(sourceId);
const authorColor = contactModel ? contactModel.getColor() : null;
const authorAvatarPath = contactModel
@ -774,7 +768,13 @@
referencedMessageNotFound,
} = quote;
const contact =
author && ConversationController.get(author || authorUuid);
(author || authorUuid) &&
ConversationController.get(
ConversationController.ensureContactIds({
e164: author,
uuid: authorUuid,
})
);
const authorColor = contact ? contact.getColor() : 'grey';
const authorPhoneNumber = format(author, {
@ -810,17 +810,18 @@
const e164 = conversation.get('e164');
const uuid = conversation.get('uuid');
const conversationId = conversation.get('id');
const readBy = this.get('read_by') || [];
if (includesAny(readBy, identifier, e164, uuid)) {
if (includesAny(readBy, conversationId, e164, uuid)) {
return 'read';
}
const deliveredTo = this.get('delivered_to') || [];
if (includesAny(deliveredTo, identifier, e164, uuid)) {
if (includesAny(deliveredTo, conversationId, e164, uuid)) {
return 'delivered';
}
const sentTo = this.get('sent_to') || [];
if (includesAny(sentTo, identifier, e164, uuid)) {
if (includesAny(sentTo, conversationId, e164, uuid)) {
return 'sent';
}
@ -1220,19 +1221,22 @@
return this.OUR_UUID;
},
getContact() {
getContactId() {
const source = this.getSource();
const sourceUuid = this.getSourceUuid();
if (!source && !sourceUuid) {
return null;
return ConversationController.getOurConversationId();
}
const contactId = ConversationController.ensureContactIds({
return ConversationController.ensureContactIds({
e164: source,
uuid: sourceUuid,
});
return ConversationController.get(contactId, 'private');
},
getContact() {
const id = this.getContactId();
return ConversationController.get(id);
},
isOutgoing() {
return this.get('type') === 'outgoing';
@ -1395,7 +1399,7 @@
let recipients = _.intersection(intendedRecipients, currentRecipients);
recipients = _.without(recipients, successfulRecipients).map(id => {
const c = ConversationController.get(id);
return c.get('uuid') || c.get('e164');
return c.getSendTarget();
});
if (!recipients.length) {
@ -1699,7 +1703,7 @@
try {
this.set({
// These are the same as a normal send()
sent_to: [conv.get('uuid') || conv.get('e164')],
sent_to: [conv.getSendTarget()],
sent: true,
expirationStartTimestamp: Date.now(),
});
@ -1709,8 +1713,8 @@
unidentifiedDeliveries: result ? result.unidentifiedDeliveries : null,
// These are unique to a Note to Self message - immediately read/delivered
delivered_to: [this.OUR_UUID || this.OUR_NUMBER],
read_by: [this.OUR_UUID || this.OUR_NUMBER],
delivered_to: [ConversationController.getOurConversationId()],
read_by: [ConversationController.getOurConversationId()],
});
} catch (result) {
const errors = (result && result.errors) || [
@ -2004,20 +2008,20 @@
return message;
}
const { attachments, id, author } = quote;
const { attachments, id, author, authorUuid } = quote;
const firstAttachment = attachments[0];
const authorConversationId = ConversationController.ensureContactIds({
e164: author,
uuid: authorUuid,
});
const collection = await window.Signal.Data.getMessagesBySentAt(id, {
MessageCollection: Whisper.MessageCollection,
});
const found = collection.find(item => {
const messageAuthor = item.getContact();
const messageAuthorId = item.getContactId();
return (
messageAuthor &&
ConversationController.getConversationId(author) ===
messageAuthor.get('id')
);
return authorConversationId === messageAuthorId;
});
if (!found) {
@ -2119,10 +2123,7 @@
const source = message.get('source');
const sourceUuid = message.get('sourceUuid');
const type = message.get('type');
let conversationId = message.get('conversationId');
if (initialMessage.group) {
conversationId = initialMessage.group.id;
}
const conversationId = message.get('conversationId');
const GROUP_TYPES = textsecure.protobuf.GroupContext.Type;
const conversation = ConversationController.get(conversationId);
@ -2392,10 +2393,13 @@
if (conversation.get('left')) {
window.log.warn('re-added to a left group');
attributes.left = false;
conversation.set({ addedBy: message.getContact().get('id') });
conversation.set({ addedBy: message.getContactId() });
}
} else if (dataMessage.group.type === GROUP_TYPES.QUIT) {
const sender = ConversationController.get(source || sourceUuid);
const sender = ConversationController.ensureContactIds({
e164: source,
uuid: sourceUuid,
});
const inGroup = Boolean(
sender &&
(conversation.get('members') || []).includes(sender.id)
@ -2453,6 +2457,7 @@
message.set({
expirationTimerUpdate: {
source,
sourceUuid,
expireTimer: dataMessage.expireTimer,
},
});
@ -2567,9 +2572,7 @@
e164: source,
uuid: sourceUuid,
});
ConversationController.get(localId, 'private').setProfileKey(
profileKey
);
ConversationController.get(localId).setProfileKey(profileKey);
}
}