From 6ce84e7f9bd52dbc5255aa76b75726a3427ea82f Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 20 Feb 2019 09:52:19 -0500 Subject: [PATCH] Process 'sent update' transcripts. --- .../src/Devices/OWSRecordTranscriptJob.m | 9 +++++++++ .../Messages/Interactions/TSOutgoingMessage.h | 1 + .../Messages/Interactions/TSOutgoingMessage.m | 17 ++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/SignalServiceKit/src/Devices/OWSRecordTranscriptJob.m b/SignalServiceKit/src/Devices/OWSRecordTranscriptJob.m index 0be9fefc7..d8c3425ef 100644 --- a/SignalServiceKit/src/Devices/OWSRecordTranscriptJob.m +++ b/SignalServiceKit/src/Devices/OWSRecordTranscriptJob.m @@ -161,6 +161,7 @@ NS_ASSUME_NONNULL_BEGIN [outgoingMessage saveWithTransaction:transaction]; [outgoingMessage updateWithWasSentFromLinkedDeviceWithUDRecipientIds:transcript.udRecipientIds nonUdRecipientIds:transcript.nonUdRecipientIds + isSentUpdate:NO transaction:transaction]; [[OWSDisappearingMessagesJob sharedJob] startAnyExpirationForMessage:outgoingMessage expirationStartedAt:transcript.expirationStartedAt @@ -239,6 +240,13 @@ NS_ASSUME_NONNULL_BEGIN BOOL messageFound = NO; for (TSOutgoingMessage *message in messages) { + if (!message.isFromLinkedDevice) { + // isFromLinkedDevice isn't always set for very old linked messages, but: + // + // a) We should never receive a "sent update" for a very old message. + // b) It's safe to discard suspicious "sent updates." + continue; + } TSThread *thread = [message threadWithTransaction:transaction]; if (!thread.isGroupThread) { continue; @@ -257,6 +265,7 @@ NS_ASSUME_NONNULL_BEGIN [message updateWithWasSentFromLinkedDeviceWithUDRecipientIds:udRecipientIds nonUdRecipientIds:nonUdRecipientIds + isSentUpdate:YES transaction:transaction]; messageFound = YES; } diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h index b007d7fc4..f75a997cc 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h @@ -220,6 +220,7 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { - (void)updateWithWasSentFromLinkedDeviceWithUDRecipientIds:(nullable NSArray *)udRecipientIds nonUdRecipientIds:(nullable NSArray *)nonUdRecipientIds + isSentUpdate:(BOOL)isSentUpdate transaction:(YapDatabaseReadWriteTransaction *)transaction; // This method is used to rewrite the recipient list with a single recipient. diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index 208f6035a..c2768391e 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -753,7 +753,9 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt - (void)updateWithWasSentFromLinkedDeviceWithUDRecipientIds:(nullable NSArray *)udRecipientIds nonUdRecipientIds:(nullable NSArray *)nonUdRecipientIds - transaction:(YapDatabaseReadWriteTransaction *)transaction { + isSentUpdate:(BOOL)isSentUpdate + transaction:(YapDatabaseReadWriteTransaction *)transaction +{ OWSAssertDebug(transaction); [self @@ -788,6 +790,19 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt recipientState.wasSentByUD = NO; recipientStateMap[recipientId] = recipientState; } + + if (isSentUpdate) { + // If this is a "sent update", make sure that: + // + // a) "Sent updates" should never remove any recipients. We end up with the + // union of the existing and new recipients. + // b) "Sent updates" should never downgrade the "recipient state" for any + // recipients. Prefer existing "recipient state"; "sent updates" only + // add new recipients at the "sent" state. + [recipientStateMap removeObjectsForKeys:self.recipientStateMap.allKeys]; + [recipientStateMap addEntriesFromDictionary:self.recipientStateMap]; + } + [message setRecipientStateMap:recipientStateMap]; } else { // Otherwise assume this is a legacy message before UD was introduced, and mark