From 071dbd441439cfedc74eaf7f3e7bc79ba730d374 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 7 Nov 2017 14:52:31 -0500 Subject: [PATCH] Respond to CR. // FREEBIE --- .../src/Messages/OWSMessageSender.m | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 7ba8024a5..95051e372 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -954,11 +954,29 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; NSString *localNumber = [TSAccountManager localNumber]; if ([localNumber isEqualToString:recipient.uniqueId]) { + OWSAssert([message isKindOfClass:[OWSOutgoingSyncMessage class]]); + // Messages send to the "local number" should be sync messages. + // + // We can skip sending sync messages if we know that we have no linked + // devices. However, we need to be sure to handle the case where the + // linked device list has just changed. + // + // The linked device list is reflected in two separate pieces of state: + // + // * OWSDevice's state is updated when you link or unlink a device. + // * SignalRecipient's state is updated by 409 "Mismatched devices" + // responses from the service. + // + // If _both_ of these pieces of state agree that there are no linked + // devices, then can safely skip sending sync message. + + // 1. Check OWSDevice's state. __block BOOL hasSecondaryDevices; [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { hasSecondaryDevices = [OWSDevice hasSecondaryDevicesWithTransaction:transaction]; }]; + // 2. Check SignalRecipient's state. BOOL hasDeviceMessages = deviceMessages.count > 0; if (!hasSecondaryDevices && !hasDeviceMessages) { @@ -975,7 +993,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; } else if (hasSecondaryDevices) { // We may have just linked a new secondary device which is not yet reflected in // the SignalRecipient that corresponds to ourself. Proceed. Client should learn - // of new secondary devices when this message send fails. + // of new secondary devices via 409 "Mismatched devices" response. DDLogWarn(@"%@ sync message has no device messages but account has secondary devices.", self.logTag); } else if (hasDeviceMessages) { OWSFail(@"%@ sync message has device messages for unknown secondary devices.", self.logTag);