diff --git a/SignalServiceKit/src/Account/CreatePreKeysOperation.swift b/SignalServiceKit/src/Account/CreatePreKeysOperation.swift index 2a14afc7a..5a8c75bcf 100644 --- a/SignalServiceKit/src/Account/CreatePreKeysOperation.swift +++ b/SignalServiceKit/src/Account/CreatePreKeysOperation.swift @@ -26,17 +26,8 @@ public class CreatePreKeysOperation: OWSOperation { if identityKeyManager.identityKeyPair() == nil { identityKeyManager.generateNewIdentityKeyPair() } - - // Loki: We don't generate PreKeyRecords here. - // This is because we need the records to be linked to a contact since we don't have a central server. - // It's done automatically when we generate a pre key bundle to send to a contact (`generatePreKeyBundleForContact:`). - // You can use `getOrCreatePreKeyForContact:` to generate one if needed. - let signedPreKeyRecord = primaryStorage.generateRandomSignedRecord() - signedPreKeyRecord.markAsAcceptedByService() - primaryStorage.storeSignedPreKey(signedPreKeyRecord.id, signedPreKeyRecord: signedPreKeyRecord) - primaryStorage.setCurrentSignedPrekeyId(signedPreKeyRecord.id) - print("[Loki] Pre keys created successfully.") + SessionManagementProtocol.createPreKeys() reportSuccess() /* Loki: Original code diff --git a/SignalServiceKit/src/Account/PreKeyRefreshOperation.swift b/SignalServiceKit/src/Account/PreKeyRefreshOperation.swift index 713a32505..05c9210d4 100644 --- a/SignalServiceKit/src/Account/PreKeyRefreshOperation.swift +++ b/SignalServiceKit/src/Account/PreKeyRefreshOperation.swift @@ -38,7 +38,7 @@ public class RefreshPreKeysOperation: OWSOperation { // Loki: Doing this on the global queue to match Signal DispatchQueue.global().async { - SessionManagementProtocol.refreshPreKeys() + SessionManagementProtocol.refreshSignedPreKey() self.reportSuccess() } diff --git a/SignalServiceKit/src/Account/RotateSignedKeyOperation.swift b/SignalServiceKit/src/Account/RotateSignedKeyOperation.swift index 400710768..1fc9368dd 100644 --- a/SignalServiceKit/src/Account/RotateSignedKeyOperation.swift +++ b/SignalServiceKit/src/Account/RotateSignedKeyOperation.swift @@ -29,7 +29,7 @@ public class RotateSignedPreKeyOperation: OWSOperation { // Loki: Doing this on the global queue to match Signal DispatchQueue.global().async { - SessionManagementProtocol.rotatePreKeys() + SessionManagementProtocol.rotateSignedPreKey() self.reportSuccess() } diff --git a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.m b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.m index f7e01dd11..045e2cd7f 100644 --- a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.m +++ b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.m @@ -59,13 +59,13 @@ @try { return [self throws_loadPreKey:preKeyId]; } @catch (NSException *exception) { - [LKLogger print:[NSString stringWithFormat:@"[Loki] New pre key generated for %@.", pubKey]]; return [self generateAndStorePreKeyForContact:pubKey]; } } /// Generate prekey for a contact and store it - (PreKeyRecord *)generateAndStorePreKeyForContact:(NSString *)pubKey { + [LKLogger print:[NSString stringWithFormat:@"[Loki] Generating new pre key for: %@.", pubKey]]; OWSAssertDebug(pubKey.length > 0); NSArray *records = [self generatePreKeyRecords:1]; diff --git a/SignalServiceKit/src/Loki/Protocol/Multi Device/MultiDeviceProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Multi Device/MultiDeviceProtocol.swift index 03c7e7f9e..d59786589 100644 --- a/SignalServiceKit/src/Loki/Protocol/Multi Device/MultiDeviceProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Multi Device/MultiDeviceProtocol.swift @@ -38,12 +38,17 @@ public final class MultiDeviceProtocol : NSObject { private override init() { } // MARK: - Sending (Part 1) + @objc(isMultiDeviceRequiredForMessage:) + public static func isMultiDeviceRequired(for message: TSOutgoingMessage) -> Bool { + return !(message is DeviceLinkMessage) + } + @objc(sendMessageToDestinationAndLinkedDevices:in:) public static func sendMessageToDestinationAndLinkedDevices(_ messageSend: OWSMessageSend, in transaction: YapDatabaseReadWriteTransaction) { // TODO: I'm pretty sure there are quite a few holes in this logic let message = messageSend.message let recipientID = messageSend.recipient.recipientId() - let thread = messageSend.thread ?? TSContactThread.getOrCreateThread(withContactId: recipientID, transaction: transaction) // TODO: Added this because I think we need it + let thread = messageSend.thread ?? TSContactThread.getOrCreateThread(withContactId: recipientID, transaction: transaction) // TODO: This seems really iffy let isGroupMessage = thread.isGroupThread() let isOpenGroupMessage = (thread as? TSGroupThread)?.isPublicChat == true let isDeviceLinkMessage = message is DeviceLinkMessage diff --git a/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift index 93edcb678..62d37dcc2 100644 --- a/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift @@ -34,8 +34,8 @@ public final class SessionManagementProtocol : NSObject { print("[Loki] Pre keys created successfully.") } - @objc(refreshPreKeys) - public static func refreshPreKeys() { + @objc(refreshSignedPreKey) + public static func refreshSignedPreKey() { guard storage.currentSignedPrekeyId() == nil else { print("[Loki] Skipping pre key refresh; using existing signed pre key.") return @@ -49,15 +49,15 @@ public final class SessionManagementProtocol : NSObject { print("[Loki] Pre keys refreshed successfully.") } - @objc(rotatePreKeys) - public static func rotatePreKeys() { + @objc(rotateSignedPreKey) + public static func rotateSignedPreKey() { let signedPreKeyRecord = storage.generateRandomSignedRecord() signedPreKeyRecord.markAsAcceptedByService() storage.storeSignedPreKey(signedPreKeyRecord.id, signedPreKeyRecord: signedPreKeyRecord) storage.setCurrentSignedPrekeyId(signedPreKeyRecord.id) TSPreKeyManager.clearPreKeyUpdateFailureCount() TSPreKeyManager.clearSignedPreKeyRecords() - print("[Loki] Pre keys rotated successfully.") + print("[Loki] Signed pre key rotated successfully.") } @objc(shouldUseFallbackEncryptionForMessage:) diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index ece25c01b..14f17bf63 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -591,9 +591,14 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; } resolve(error); }]; - [self.primaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [LKMultiDeviceProtocol sendMessageToDestinationAndLinkedDevices:messageSend in:transaction]; - }]; + + if ([LKMultiDeviceProtocol isMultiDeviceRequiredForMessage:message]) { // Avoid the write transaction if possible + [self.primaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [LKMultiDeviceProtocol sendMessageToDestinationAndLinkedDevices:messageSend in:transaction]; + }]; + } else { + [self sendMessage:messageSend]; + } }]; [sendPromises addObject:sendPromise]; } @@ -1559,9 +1564,13 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; failure(error); }]; - [self.primaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [LKMultiDeviceProtocol sendMessageToDestinationAndLinkedDevices:messageSend in:transaction]; - }]; + if ([LKMultiDeviceProtocol isMultiDeviceRequiredForMessage:message]) { // Avoid the write transaction if possible + [self.primaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [LKMultiDeviceProtocol sendMessageToDestinationAndLinkedDevices:messageSend in:transaction]; + }]; + } else { + [self sendMessage:messageSend]; + } } - (NSArray *)throws_deviceMessagesForMessageSend:(OWSMessageSend *)messageSend