From cba8c6798362b3e1df169671ba80ac832705ce05 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 10 Oct 2018 10:17:58 -0400 Subject: [PATCH 1/3] Add 'is ud' property to incoming messages. --- .../ColorPickerViewController.swift | 3 ++- .../src/ViewControllers/DebugUI/DebugUIMessages.m | 15 ++++++++++----- .../src/Messages/Interactions/TSIncomingMessage.h | 5 ++++- .../src/Messages/Interactions/TSIncomingMessage.m | 3 ++- SignalServiceKit/src/Messages/OWSMessageManager.m | 7 +++++-- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Signal/src/ViewControllers/ColorPickerViewController.swift b/Signal/src/ViewControllers/ColorPickerViewController.swift index 6756447b8..6d39d2337 100644 --- a/Signal/src/ViewControllers/ColorPickerViewController.swift +++ b/Signal/src/ViewControllers/ColorPickerViewController.swift @@ -391,7 +391,8 @@ private class MockIncomingMessage: TSIncomingMessage { expiresInSeconds: 0, quotedMessage: nil, contactShare: nil, - serverTimestamp: nil) + serverTimestamp: nil, + wasReceivedByUD: false) } required init(coder: NSCoder) { diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index 10c8959c0..385a18603 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -3727,7 +3727,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac expiresInSeconds:0 quotedMessage:nil contactShare:nil - serverTimestamp:nil]; + serverTimestamp:nil + wasReceivedByUD:NO]; [message markAsReadNowWithSendReadReceipt:NO transaction:transaction]; break; } @@ -3767,7 +3768,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac expiresInSeconds:0 quotedMessage:nil contactShare:nil - serverTimestamp:nil]; + serverTimestamp:nil + wasReceivedByUD:NO]; [message markAsReadNowWithSendReadReceipt:NO transaction:transaction]; break; } @@ -4229,7 +4231,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac expiresInSeconds:0 quotedMessage:nil contactShare:nil - serverTimestamp:nil]; + serverTimestamp:nil + wasReceivedByUD:NO]; [message markAsReadNowWithSendReadReceipt:NO transaction:transaction]; } { @@ -4270,7 +4273,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac expiresInSeconds:60 quotedMessage:nil contactShare:nil - serverTimestamp:nil]; + serverTimestamp:nil + wasReceivedByUD:NO]; // private setter to avoid starting expire machinery. message.read = YES; [message save]; @@ -4592,7 +4596,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac expiresInSeconds:0 quotedMessage:quotedMessage contactShare:nil - serverTimestamp:nil]; + serverTimestamp:nil + wasReceivedByUD:NO]; [message markAsReadNowWithSendReadReceipt:NO transaction:transaction]; return message; } diff --git a/SignalServiceKit/src/Messages/Interactions/TSIncomingMessage.h b/SignalServiceKit/src/Messages/Interactions/TSIncomingMessage.h index 203921d1d..9ad4fcd23 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSIncomingMessage.h +++ b/SignalServiceKit/src/Messages/Interactions/TSIncomingMessage.h @@ -14,6 +14,8 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, readonly, nullable) NSNumber *serverTimestamp; +@property (nonatomic, readonly) BOOL wasReceivedByUD; + - (instancetype)initMessageWithTimestamp:(uint64_t)timestamp inThread:(nullable TSThread *)thread messageBody:(nullable NSString *)body @@ -54,7 +56,8 @@ NS_ASSUME_NONNULL_BEGIN expiresInSeconds:(uint32_t)expiresInSeconds quotedMessage:(nullable TSQuotedMessage *)quotedMessage contactShare:(nullable OWSContact *)contactShare - serverTimestamp:(nullable NSNumber *)serverTimestamp NS_DESIGNATED_INITIALIZER; + serverTimestamp:(nullable NSNumber *)serverTimestamp + wasReceivedByUD:(BOOL)wasReceivedByUD NS_DESIGNATED_INITIALIZER; - (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; diff --git a/SignalServiceKit/src/Messages/Interactions/TSIncomingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSIncomingMessage.m index 0dbd5cc93..7af4f3da4 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSIncomingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSIncomingMessage.m @@ -48,7 +48,7 @@ NS_ASSUME_NONNULL_BEGIN quotedMessage:(nullable TSQuotedMessage *)quotedMessage contactShare:(nullable OWSContact *)contactShare serverTimestamp:(nullable NSNumber *)serverTimestamp -{ + wasReceivedByUD:(BOOL)wasReceivedByUD { self = [super initMessageWithTimestamp:timestamp inThread:thread messageBody:body @@ -66,6 +66,7 @@ NS_ASSUME_NONNULL_BEGIN _sourceDeviceId = sourceDeviceId; _read = NO; _serverTimestamp = serverTimestamp; + _wasReceivedByUD = wasReceivedByUD; return self; } diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 88ee36ec8..57a30c3e1 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -1115,6 +1115,7 @@ NS_ASSUME_NONNULL_BEGIN NSData *groupId = dataMessage.group ? dataMessage.group.id : nil; OWSContact *_Nullable contact = [OWSContacts contactForDataMessage:dataMessage transaction:transaction]; NSNumber *_Nullable serverTimestamp = (envelope.hasServerTimestamp ? @(envelope.serverTimestamp) : nil); + BOOL wasReceivedByUD = envelope.type == SSKProtoEnvelopeTypeUnidentifiedSender; if (dataMessage.group.type == SSKProtoGroupContextTypeRequestInfo) { [self handleGroupInfoRequest:envelope dataMessage:dataMessage transaction:transaction]; @@ -1229,7 +1230,8 @@ NS_ASSUME_NONNULL_BEGIN expiresInSeconds:dataMessage.expireTimer quotedMessage:quotedMessage contactShare:contact - serverTimestamp:serverTimestamp]; + serverTimestamp:serverTimestamp + wasReceivedByUD:wasReceivedByUD]; [self finalizeIncomingMessage:incomingMessage thread:oldGroupThread @@ -1269,7 +1271,8 @@ NS_ASSUME_NONNULL_BEGIN expiresInSeconds:dataMessage.expireTimer quotedMessage:quotedMessage contactShare:contact - serverTimestamp:serverTimestamp]; + serverTimestamp:serverTimestamp + wasReceivedByUD:wasReceivedByUD]; [self finalizeIncomingMessage:incomingMessage thread:thread From a6eed30127c8c0aca0fde2769f4dda8aa413c77f Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 10 Oct 2018 10:27:29 -0400 Subject: [PATCH 2/3] Add 'is ud' property to outgoing messages. --- Signal/src/ViewControllers/DebugUI/DebugUIMessages.m | 2 +- .../src/Messages/Interactions/TSOutgoingMessage.h | 6 +++++- .../src/Messages/Interactions/TSOutgoingMessage.m | 7 +++++-- SignalServiceKit/src/Messages/OWSMessageSender.m | 11 ++++++----- .../Messages/Interactions/TSOutgoingMessageTest.m | 2 +- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index 385a18603..938b2c36f 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -4249,7 +4249,7 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac contactShare:nil]; [message saveWithTransaction:transaction]; [message updateWithFakeMessageState:TSOutgoingMessageStateSent transaction:transaction]; - [message updateWithSentRecipient:recipientId transaction:transaction]; + [message updateWithSentRecipient:recipientId wasSentByUD:NO transaction:transaction]; [message updateWithDeliveredRecipient:recipientId deliveryTimestamp:timestamp transaction:transaction]; [message updateWithReadRecipientId:recipientId readTimestamp:timestamp.unsignedLongLongValue diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h index e23a4de10..43de963d3 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h @@ -62,6 +62,8 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { // This property should only be set if state == .sent. @property (atomic, nullable, readonly) NSNumber *readTimestamp; +@property (atomic, readonly) BOOL wasSentByUD; + @end #pragma mark - @@ -167,7 +169,9 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { #pragma mark - Update With... Methods // This method is used to record a successful send to one recipient. -- (void)updateWithSentRecipient:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction; +- (void)updateWithSentRecipient:(NSString *)recipientId + wasSentByUD:(BOOL)wasSentByUD + transaction:(YapDatabaseReadWriteTransaction *)transaction; // This method is used to record a skipped send to one recipient. - (void)updateWithSkippedRecipient:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction; diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index 4f8572f14..6f3fb497e 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -60,6 +60,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt @property (atomic) OWSOutgoingMessageRecipientState state; @property (atomic, nullable) NSNumber *deliveryTimestamp; @property (atomic, nullable) NSNumber *readTimestamp; +@property (atomic) BOOL wasSentByUD; @end @@ -637,8 +638,9 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt }]; } -- (void)updateWithSentRecipient:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction -{ +- (void)updateWithSentRecipient:(NSString *)recipientId + wasSentByUD:(BOOL)wasSentByUD + transaction:(YapDatabaseReadWriteTransaction *)transaction { OWSAssertDebug(recipientId.length > 0); OWSAssertDebug(transaction); @@ -651,6 +653,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt return; } recipientState.state = OWSOutgoingMessageRecipientStateSent; + recipientState.wasSentByUD |= wasSentByUD; }]; } diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index d4da8ac7d..d89afff36 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -1001,7 +1001,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; messages:deviceMessages timeStamp:message.timestamp unidentifiedAccess:messageSend.unidentifiedAccess]; - OWSWebSocketType webSocketType = (isUDSend ? OWSWebSocketTypeUD : OWSWebSocketTypeDefault); BOOL canMakeWebsocketRequests = ([TSSocketManager.shared canMakeRequestsOfType:webSocketType] && !messageSend.hasWebsocketSendFailed); @@ -1009,7 +1008,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; [TSSocketManager.shared makeRequest:request webSocketType:webSocketType success:^(id _Nullable responseObject) { - [self messageSendDidSucceed:messageSend deviceMessages:deviceMessages]; + [self messageSendDidSucceed:messageSend deviceMessages:deviceMessages wasSentByUD:isUDSend]; } failure:^(NSInteger statusCode, NSData *_Nullable responseData, NSError *error) { dispatch_async([OWSDispatch sendingQueue], ^{ @@ -1038,7 +1037,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; } else { [self.networkManager makeRequest:request success:^(NSURLSessionDataTask *task, id responseObject) { - [self messageSendDidSucceed:messageSend deviceMessages:deviceMessages]; + [self messageSendDidSucceed:messageSend deviceMessages:deviceMessages wasSentByUD:isUDSend]; } failure:^(NSURLSessionDataTask *task, NSError *error) { NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response; @@ -1069,7 +1068,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; - (void)messageSendDidSucceed:(OWSMessageSend *)messageSend deviceMessages:(NSArray *)deviceMessages -{ + wasSentByUD:(BOOL)wasSentByUD { OWSAssertDebug(messageSend); OWSAssertDebug(deviceMessages); @@ -1091,7 +1090,9 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; dispatch_async([OWSDispatch sendingQueue], ^{ [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [messageSend.message updateWithSentRecipient:messageSend.recipient.uniqueId transaction:transaction]; + [messageSend.message updateWithSentRecipient:messageSend.recipient.uniqueId + wasSentByUD:wasSentByUD + transaction:transaction]; // If we've just delivered a message to a user, we know they // have a valid Signal account. diff --git a/SignalServiceKit/tests/Messages/Interactions/TSOutgoingMessageTest.m b/SignalServiceKit/tests/Messages/Interactions/TSOutgoingMessageTest.m index 512f39daa..94b9e45ce 100644 --- a/SignalServiceKit/tests/Messages/Interactions/TSOutgoingMessageTest.m +++ b/SignalServiceKit/tests/Messages/Interactions/TSOutgoingMessageTest.m @@ -62,7 +62,7 @@ NS_ASSUME_NONNULL_BEGIN quotedMessage:nil contactShare:nil]; [self readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [message updateWithSentRecipient:self.contactId transaction:transaction]; + [message updateWithSentRecipient:self.contactId wasSentByUD:NO transaction:transaction]; XCTAssertTrue([message shouldStartExpireTimerWithTransaction:transaction]); }]; } From bda6fdf44f99e5c58dd9d1f0e5f13961bbca41ee Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 10 Oct 2018 14:34:14 -0400 Subject: [PATCH 3/3] Respond to CR. --- SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index 6f3fb497e..8a992cba5 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -653,7 +653,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt return; } recipientState.state = OWSOutgoingMessageRecipientStateSent; - recipientState.wasSentByUD |= wasSentByUD; + recipientState.wasSentByUD = wasSentByUD; }]; }