From 5c6f9ec088b1935a0d9b8dc85a622afcb39b3541 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 26 Apr 2018 14:12:34 -0400 Subject: [PATCH 1/2] Fix issues in outgoing messages status changes. --- .../Messages/Interactions/TSOutgoingMessage.m | 17 +++++++++++++- .../src/Messages/OWSMessageSender.m | 23 ++++++++++--------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index deb82e03e..13ee3b170 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -10,6 +10,7 @@ #import "OWSSignalServiceProtos.pb.h" #import "ProtoBuf+OWS.h" #import "SignalRecipient.h" +#import "TSAccountManager.h" #import "TSAttachmentStream.h" #import "TSContactThread.h" #import "TSGroupThread.h" @@ -271,7 +272,16 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec // New outgoing messages should immediately determine their // recipient list from current thread state. NSMutableDictionary *recipientStateMap = [NSMutableDictionary new]; - NSArray *recipientIds = [thread recipientIdentifiers]; + NSArray *recipientIds; + if ([self isKindOfClass:[OWSOutgoingSyncMessage class]]) { + NSString *_Nullable localNumber = [TSAccountManager localNumber]; + OWSAssert(localNumber); + recipientIds = @[ + localNumber, + ]; + } else { + recipientIds = [thread recipientIdentifiers]; + } for (NSString *recipientId in recipientIds) { TSOutgoingMessageRecipientState *recipientState = [TSOutgoingMessageRecipientState new]; recipientState.state = OWSOutgoingMessageRecipientStateSending; @@ -553,6 +563,11 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec OWSAssert(recipientId.length > 0); OWSAssert(transaction); + // If delivery notification doesn't include timestamp, use "now" as an estimate. + if (!deliveryTimestamp) { + deliveryTimestamp = @([NSDate ows_millisecondTimeStamp]); + } + [self applyChangeToSelfAndLatestCopy:transaction changeBlock:^(TSOutgoingMessage *message) { TSOutgoingMessageRecipientState *_Nullable recipientState diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 342105321..22ee85b17 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -456,7 +456,14 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; dispatch_async([OWSDispatch sendingQueue], ^{ TSThread *thread = message.thread; - if ([thread isKindOfClass:[TSGroupThread class]]) { + // TODO: It would be nice to combine the "contact" and "group" send logic here. + if ([thread isKindOfClass:[TSContactThread class]] && + [((TSContactThread *)thread).contactIdentifier isEqualToString:[TSAccountManager localNumber]]) { + // Send to self. + [self handleSendToMyself:message]; + successHandler(); + return; + } else if ([thread isKindOfClass:[TSGroupThread class]]) { TSGroupThread *gThread = (TSGroupThread *)thread; @@ -483,6 +490,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; NSMutableSet *sendingRecipientIds = [NSMutableSet setWithArray:message.sendingRecipientIds]; [sendingRecipientIds intersectSet:[NSSet setWithArray:gThread.groupModel.groupMemberIds]]; + [sendingRecipientIds minusSet:[NSSet setWithArray:self.blockingManager.blockedPhoneNumbers]]; NSError *error; NSArray *recipients = @@ -505,17 +513,10 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; || [message isKindOfClass:[OWSOutgoingSyncMessage class]]) { TSContactThread *contactThread = (TSContactThread *)thread; - if ([contactThread.contactIdentifier isEqualToString:[TSAccountManager localNumber]] - && ![message isKindOfClass:[OWSOutgoingSyncMessage class]]) { - - [self handleSendToMyself:message]; - successHandler(); - return; - } - NSString *recipientContactId = [message isKindOfClass:[OWSOutgoingSyncMessage class]] - ? [TSAccountManager localNumber] - : contactThread.contactIdentifier; + NSString *recipientContactId + = ([message isKindOfClass:[OWSOutgoingSyncMessage class]] ? [TSAccountManager localNumber] + : contactThread.contactIdentifier); // If we block a user, don't send 1:1 messages to them. The UI // should prevent this from occurring, but in some edge cases From f16e9a2920f5858aaa88bc736c12a6e1358e5215 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 26 Apr 2018 14:17:41 -0400 Subject: [PATCH 2/2] Fix issues in outgoing messages status changes. --- .../src/Messages/OWSMessageSender.m | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 22ee85b17..5f160b2c9 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -477,8 +477,19 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; // * The recipient was in the group when the message was first tried to be sent. // * The recipient is still in the group. // * The recipient is in the "sending" state. + + NSMutableSet *sendingRecipientIds = [NSMutableSet setWithArray:message.sendingRecipientIds]; + [sendingRecipientIds intersectSet:[NSSet setWithArray:gThread.groupModel.groupMemberIds]]; + [sendingRecipientIds minusSet:[NSSet setWithArray:self.blockingManager.blockedPhoneNumbers]]; + + // Mark skipped recipients as such. We skip because: + // + // * Recipient is no longer in the group. + // * Recipient is blocked. + // + // Elsewhere, we skip recipient if their Signal account has been deactivated. NSMutableSet *obsoleteRecipientIds = [NSMutableSet setWithArray:message.sendingRecipientIds]; - [obsoleteRecipientIds minusSet:[NSSet setWithArray:gThread.groupModel.groupMemberIds]]; + [obsoleteRecipientIds minusSet:sendingRecipientIds]; if (obsoleteRecipientIds.count > 0) { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { for (NSString *recipientId in obsoleteRecipientIds) { @@ -488,10 +499,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; }]; } - NSMutableSet *sendingRecipientIds = [NSMutableSet setWithArray:message.sendingRecipientIds]; - [sendingRecipientIds intersectSet:[NSSet setWithArray:gThread.groupModel.groupMemberIds]]; - [sendingRecipientIds minusSet:[NSSet setWithArray:self.blockingManager.blockedPhoneNumbers]]; - NSError *error; NSArray *recipients = [self getRecipientsForRecipientIds:sendingRecipientIds.allObjects error:&error];