From 2bca5a850cf161daca7a9aa36891e3a866beeaf9 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 8 Nov 2019 14:41:06 +1100 Subject: [PATCH] Fix sent message syncing bug --- .../Loki/Settings/DeviceLinkingModal.swift | 2 +- .../src/Messages/OWSMessageManager.m | 10 +++---- .../src/Messages/OWSMessageSender.m | 27 ++++++++++++------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Signal/src/Loki/Settings/DeviceLinkingModal.swift b/Signal/src/Loki/Settings/DeviceLinkingModal.swift index d78728b25..1c6f8fbb8 100644 --- a/Signal/src/Loki/Settings/DeviceLinkingModal.swift +++ b/Signal/src/Loki/Settings/DeviceLinkingModal.swift @@ -138,7 +138,7 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate { @objc private func authorizeDeviceLink() { let deviceLink = self.deviceLink! let linkingAuthorizationMessage = DeviceLinkingUtilities.getLinkingAuthorizationMessage(for: deviceLink) - (0..<4).forEach { _ in ThreadUtil.enqueue(linkingAuthorizationMessage) } + ThreadUtil.enqueue(linkingAuthorizationMessage) let session = DeviceLinkingSession.current! session.stopListeningForLinkingRequests() session.markLinkingRequestAsProcessed() diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 35305b4a5..1686ea842 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -902,11 +902,11 @@ NS_ASSUME_NONNULL_BEGIN } NSString *localNumber = self.tsAccountManager.localNumber; - if (![localNumber isEqualToString:envelope.source]) { - // Sync messages should only come from linked devices. - OWSProdErrorWEnvelope([OWSAnalyticsEvents messageManagerErrorSyncMessageFromUnknownSource], envelope); - return; - } +// if (![localNumber isEqualToString:envelope.source]) { +// // Sync messages should only come from linked devices. +// OWSProdErrorWEnvelope([OWSAnalyticsEvents messageManagerErrorSyncMessageFromUnknownSource], envelope); +// return; +// } if (syncMessage.sent) { OWSIncomingSentMessageTranscript *transcript = diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 2fcebc234..9293930c9 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -498,9 +498,17 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; OWSAssertDebug(message); OWSAssertDebug(errorHandle); - NSMutableSet *recipientIds = [NSMutableSet new]; + __block NSMutableSet *recipientIds = [NSMutableSet new]; if ([message isKindOfClass:[OWSOutgoingSyncMessage class]]) { [recipientIds addObject:self.tsAccountManager.localNumber]; + NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey; + if ([message isKindOfClass:OWSOutgoingSyncMessage.class]) { + [OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + NSString *masterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:userHexEncodedPublicKey in:transaction] ?: userHexEncodedPublicKey; + NSSet *linkedDeviceHexEncodedPublicKeys = [LKDatabaseUtilities getLinkedDeviceHexEncodedPublicKeysFor:userHexEncodedPublicKey in:transaction]; + recipientIds = [recipientIds setByAddingObjectsFromSet:linkedDeviceHexEncodedPublicKeys].mutableCopy; + }]; + } } else if (thread.isGroupThread) { [self.primaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { LKPublicChat *publicChat = [LKDatabaseUtilities getPublicChatForThreadID:thread.uniqueId transaction:transaction]; @@ -1652,7 +1660,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; BOOL isFriendRequest = [messageSend.message isKindOfClass:LKFriendRequestMessage.class]; BOOL isDeviceLinkMessage = [messageSend.message isKindOfClass:LKDeviceLinkMessage.class]; if (!isFriendRequest && !isDeviceLinkMessage) { - [self throws_ensureRecipientHasSessionForMessageSend:messageSend deviceId:@(OWSDevicePrimaryDeviceId)]; + [self throws_ensureRecipientHasSessionForMessageSend:messageSend recipientID:recipientID deviceId:@(OWSDevicePrimaryDeviceId)]; } __block NSDictionary *_Nullable messageDict; @@ -1695,19 +1703,18 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; return [messagesArray copy]; } -- (void)throws_ensureRecipientHasSessionForMessageSend:(OWSMessageSend *)messageSend deviceId:(NSNumber *)deviceId +- (void)throws_ensureRecipientHasSessionForMessageSend:(OWSMessageSend *)messageSend recipientID:(NSString *)recipientID deviceId:(NSNumber *)deviceId { OWSAssertDebug(messageSend); OWSAssertDebug(deviceId); OWSPrimaryStorage *storage = self.primaryStorage; SignalRecipient *recipient = messageSend.recipient; - NSString *recipientId = recipient.recipientId; - OWSAssertDebug(recipientId.length > 0); + OWSAssertDebug(recipientID.length > 0); __block BOOL hasSession; [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - hasSession = [storage containsSession:recipientId deviceId:[deviceId intValue] protocolContext:transaction]; + hasSession = [storage containsSession:recipientID deviceId:[deviceId intValue] protocolContext:transaction]; }]; if (hasSession) { return; @@ -1718,7 +1725,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; OWSRaiseException(NoSessionForTransientMessageException, @"No session for transient message."); } - PreKeyBundle *_Nullable bundle = [storage getPreKeyBundleForContact:recipientId]; + PreKeyBundle *_Nullable bundle = [storage getPreKeyBundleForContact:recipientID]; __block NSException *exception; /** Loki: Original code @@ -1762,14 +1769,14 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; preKeyStore:storage signedPreKeyStore:storage identityKeyStore:self.identityManager - recipientId:recipientId + recipientId:recipientID deviceId:[deviceId intValue]]; [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { @try { [builder throws_processPrekeyBundle:bundle protocolContext:transaction]; // Loki: Discard the pre key bundle here since the session has been established - [storage removePreKeyBundleForContact:recipientId transaction:transaction]; + [storage removePreKeyBundleForContact:recipientID transaction:transaction]; } @catch (NSException *caughtException) { exception = caughtException; } @@ -1777,7 +1784,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; if (exception) { if ([exception.name isEqualToString:UntrustedIdentityKeyException]) { OWSRaiseExceptionWithUserInfo(UntrustedIdentityKeyException, - (@{ TSInvalidPreKeyBundleKey : bundle, TSInvalidRecipientKey : recipientId }), + (@{ TSInvalidPreKeyBundleKey : bundle, TSInvalidRecipientKey : recipientID }), @""); } @throw exception;