Move logic into handleDataMessage for proper queuing

This commit is contained in:
Scott Nonnenberg 2020-02-14 13:28:35 -08:00 committed by GitHub
parent afddc852cc
commit 7ca0dfdfbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 116 additions and 128 deletions

View file

@ -1406,11 +1406,7 @@
// This is used by sendSyncMessage, then set to null
if (result.dataMessage) {
// When we're not sending recipient updates, we won't save the dataMessage
// unless it's the first time we attempt to send the message.
if (!this.get('synced') || window.SEND_RECIPIENT_UPDATES) {
this.set({ dataMessage: result.dataMessage });
}
this.set({ dataMessage: result.dataMessage });
}
const sentTo = this.get('sent_to') || [];
@ -1547,11 +1543,6 @@
}
const isUpdate = Boolean(this.get('synced'));
// Until isRecipientUpdate sync messages are widely supported, will not send them
if (isUpdate && !window.SEND_RECIPIENT_UPDATES) {
return Promise.resolve();
}
return wrap(
textsecure.messaging.sendSyncMessage(
dataMessage,
@ -1872,7 +1863,9 @@
return message;
},
handleDataMessage(initialMessage, confirm) {
handleDataMessage(initialMessage, confirm, options = {}) {
const { data } = options;
// This function is called from the background script in a few scenarios:
// 1. on an incoming message
// 2. on a sent message sync'd from another device
@ -1897,11 +1890,85 @@
const existingMessage = await getMessageBySender(this.attributes, {
Message: Whisper.Message,
});
if (existingMessage) {
const isUpdate = Boolean(data && data.isRecipientUpdate);
if (existingMessage && type === 'incoming') {
window.log.warn('Received duplicate message', this.idForLogging());
confirm();
return;
}
if (type === 'outgoing') {
if (isUpdate && existingMessage) {
window.log.info(
`handleDataMessage: Updating message ${message.idForLogging()} with received transcript`
);
let sentTo = [];
let unidentifiedDeliveries = [];
if (Array.isArray(data.unidentifiedStatus)) {
sentTo = data.unidentifiedStatus.map(item => item.destination);
const unidentified = _.filter(data.unidentifiedStatus, item =>
Boolean(item.unidentified)
);
unidentifiedDeliveries = unidentified.map(
item => item.destination
);
}
const toUpdate = MessageController.register(
existingMessage.id,
existingMessage
);
toUpdate.set({
sent_to: _.union(toUpdate.get('sent_to'), sentTo),
unidentifiedDeliveries: _.union(
toUpdate.get('unidentifiedDeliveries'),
unidentifiedDeliveries
),
});
await window.Signal.Data.saveMessage(toUpdate.attributes, {
Message: Whisper.Message,
});
confirm();
return;
} else if (isUpdate) {
window.log.warn(
`handleDataMessage: Received update transcript, but no existing entry for message ${message.idForLogging()}. Dropping.`
);
confirm();
return;
} else if (existingMessage) {
window.log.warn(
`handleDataMessage: Received duplicate transcript for message ${message.idForLogging()}, but it was not an update transcript. Dropping.`
);
confirm();
return;
}
}
// We drop incoming messages for groups we already know about, which we're not a
// part of, except for group updates.
const ourNumber = textsecure.storage.user.getNumber();
const isGroupUpdate =
initialMessage.group &&
initialMessage.group.type !==
textsecure.protobuf.GroupContext.Type.DELIVER;
if (
type === 'incoming' &&
!conversation.isPrivate() &&
!conversation.hasMember(ourNumber) &&
!isGroupUpdate
) {
window.log.warn(
`Received message destined for group ${conversation.idForLogging()}, which we're not a part of. Dropping.`
);
confirm();
return;
}
// Send delivery receipts, but only for incoming sealed sender messages
if (