From e4d612a58e0bf83b870a5cf0c962740046666e49 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 6 May 2019 14:22:34 +1000 Subject: [PATCH] Only send message to the primary device. Fail send if sending message to self. We do this because loki doesn't support multi-device sending at the moment. --- .../src/Messages/OWSMessageSender.m | 12 ++++++++++++ .../Network/API/Requests/OWSRequestFactory.m | 17 ++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 0697213f2..7170e9d91 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -1118,6 +1118,12 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; // so that we can learn from the service whether or not there are // linked devices that we don't know about. OWSLogWarn(@"Sending a message with no device messages."); + + // LOKI: We don't handle linked devices yet so it's better to just exit early + // This would only occur if we're sending a message to ourself + NSError *error = OWSErrorMakeFailedToSendOutgoingMessageError(); + [error setIsRetryable:NO]; + return messageSend.failure(error); } // TODO: Update message here to show the pow cog icon @@ -1514,7 +1520,13 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; messageSend.isLocalNumber, messageSend.isUDSend); + // LOKI: Since we don't support multi-device sending yet, just send it to the primary device + NSMutableArray *deviceIds = @[@(OWSDevicePrimaryDeviceId)]; + + /* Original code NSMutableArray *deviceIds = [recipient.devices mutableCopy]; + */ + OWSAssertDebug(deviceIds); if (messageSend.isLocalNumber) { diff --git a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m index e0fc4ad5f..88357240e 100644 --- a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m +++ b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m @@ -356,7 +356,7 @@ NS_ASSUME_NONNULL_BEGIN // LOKI: Convert Signal JSON messages to Loki messages // Refer to OWSMessageServiceParams for the Signal JSON params -+ (NSDictionary *)lokiMessagesFromMessages:(NSArray *)messages ++ (NSArray *)lokiMessagesFromMessages:(NSArray *)messages nonceArray:(NSArray *)nonceArray ttl:(NSNumber *)ttl { NSMutableArray *modifiedMessages = [[NSMutableArray alloc] init]; @@ -394,15 +394,18 @@ NS_ASSUME_NONNULL_BEGIN ttl: (NSNumber *)ttl { // NOTE: messages may be empty; See comments in OWSDeviceManager. + // This doesn't apply to loki since we don't have linked device support. OWSAssertDebug(recipientId.length > 0); + OWSAssertDebug(messages.count > 0); - NSDictionary *lokiMessages = [self lokiMessagesFromMessages:messages nonceArray:nonceArray ttl:ttl]; - - NSString *path = [textSecureMessagesAPI stringByAppendingString:recipientId]; - NSDictionary *parameters = @{ - @"messages" : lokiMessages, - }; + // Convert to loki json format + NSArray *lokiMessages = [self lokiMessagesFromMessages:messages nonceArray:nonceArray ttl:ttl]; + OWSAssertDebug(lokiMessages.count > 0); + // Just send the first message + NSString *path = [textSecureMessagesAPI stringByAppendingString:recipientId]; + NSDictionary *parameters = [lokiMessages objectAtIndex:0]; + TSRequest *request = [TSRequest requestWithUrl:[NSURL URLWithString:path] method:@"PUT" parameters:parameters]; return request; }