From 8d10d19f8fb0a7c590342997c2af7907c0ccb8a2 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 12 May 2017 11:38:24 -0400 Subject: [PATCH] Only reply to group info requester. // FREEBIE --- src/Messages/Interactions/TSOutgoingMessage.h | 5 +++++ src/Messages/Interactions/TSOutgoingMessage.m | 13 +++++++++++++ src/Messages/OWSMessageSender.m | 11 +++++++++-- src/Messages/TSMessagesManager.m | 9 ++++++--- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/Messages/Interactions/TSOutgoingMessage.h b/src/Messages/Interactions/TSOutgoingMessage.h index 79fcfb520..80fc2874f 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.h +++ b/src/Messages/Interactions/TSOutgoingMessage.h @@ -94,6 +94,9 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { @property (atomic, readonly) TSGroupMetaMessage groupMetaMessage; +// If set, this group message should only be sent to a single recipient. +@property (atomic, readonly) NSString *singleGroupRecipient; + /** * Whether the message should be serialized as a modern aka Content, or the old style legacy message. * Sync and Call messsages must be sent as Content, but other old style DataMessage payloads should be @@ -168,6 +171,8 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { - (void)updateWithWasDeliveredWithTransaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)updateWithWasDelivered; - (void)updateWithWasSentAndDelivered; +- (void)updateWithSingleGroupRecipient:(NSString *)singleGroupRecipient + transaction:(YapDatabaseReadWriteTransaction *)transaction; #pragma mark - Sent Recipients diff --git a/src/Messages/Interactions/TSOutgoingMessage.m b/src/Messages/Interactions/TSOutgoingMessage.m index 471f63071..8765ceee6 100644 --- a/src/Messages/Interactions/TSOutgoingMessage.m +++ b/src/Messages/Interactions/TSOutgoingMessage.m @@ -22,6 +22,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec @property (atomic) NSString *customMessage; @property (atomic) NSString *mostRecentFailureText; @property (atomic) BOOL wasDelivered; +@property (atomic) NSString *singleGroupRecipient; // For outgoing, non-legacy group messages sent from this client, this // contains the list of recipients to whom the message has been sent. @@ -321,6 +322,18 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec }]; } +- (void)updateWithSingleGroupRecipient:(NSString *)singleGroupRecipient + transaction:(YapDatabaseReadWriteTransaction *)transaction +{ + OWSAssert(transaction); + OWSAssert(singleGroupRecipient.length > 0); + + [self applyChangeToSelfAndLatestOutgoingMessage:transaction + changeBlock:^(TSOutgoingMessage *message) { + [message setSingleGroupRecipient:singleGroupRecipient]; + }]; +} + #pragma mark - Sent Recipients - (NSArray *)sentRecipients diff --git a/src/Messages/OWSMessageSender.m b/src/Messages/OWSMessageSender.m index cad76760f..dbe48de18 100644 --- a/src/Messages/OWSMessageSender.m +++ b/src/Messages/OWSMessageSender.m @@ -735,11 +735,18 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; NSMutableArray *futures = [NSMutableArray array]; for (SignalRecipient *recipient in recipients) { + NSString *recipientId = recipient.recipientId; + // We don't need to send the message to ourselves... - if ([recipient.uniqueId isEqualToString:[TSStorageManager localNumber]]) { + if ([recipientId isEqualToString:[TSStorageManager localNumber]]) { + continue; + } + // We don't need to sent the message to all group members if + // it has a "single group recipient". + if (message.singleGroupRecipient && ![message.singleGroupRecipient isEqualToString:recipientId]) { continue; } - if ([message wasSentToRecipient:recipient.uniqueId]) { + if ([message wasSentToRecipient:recipientId]) { // Skip recipients we have already sent this message to (on an // earlier retry, perhaps). DDLogInfo(@"%@ Skipping group message recipient; already sent: %@", self.tag, recipient.uniqueId); diff --git a/src/Messages/TSMessagesManager.m b/src/Messages/TSMessagesManager.m index 3f0aea574..7b651cfbe 100644 --- a/src/Messages/TSMessagesManager.m +++ b/src/Messages/TSMessagesManager.m @@ -775,7 +775,8 @@ NS_ASSUME_NONNULL_BEGIN } } -- (void)handleGroupInfoRequest:(OWSSignalServiceProtosDataMessage *)dataMessage +- (void)handleGroupInfoRequest:(OWSSignalServiceProtosEnvelope *)envelope + dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage { OWSAssert([NSThread isMainThread]); OWSAssert(dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeRequestInfo); @@ -786,7 +787,7 @@ NS_ASSUME_NONNULL_BEGIN return; } - DDLogInfo(@"%@ Received 'Request Group Info' message for group: %@", self.tag, groupId); + DDLogInfo(@"%@ Received 'Request Group Info' message for group: %@ from: %@", self.tag, groupId, envelope.source); [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { TSGroupModel *emptyModelToFillOutId = @@ -803,6 +804,8 @@ NS_ASSUME_NONNULL_BEGIN inThread:gThread groupMetaMessage:TSGroupMessageUpdate]; [message updateWithCustomMessage:updateGroupInfo transaction:transaction]; + // Only send this group update to the requester. + [message updateWithSingleGroupRecipient:envelope.source transaction:transaction]; dispatch_async(dispatch_get_main_queue(), ^{ [self sendGroupUpdateForThread:gThread message:message]; @@ -827,7 +830,7 @@ NS_ASSUME_NONNULL_BEGIN NSString *localNumber = [TSAccountManager localNumber]; if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeRequestInfo) { - [self handleGroupInfoRequest:dataMessage]; + [self handleGroupInfoRequest:envelope dataMessage:dataMessage]; return nil; }