From 3d2b65fc86f8f736a4fd107c9c7d694545179e81 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Thu, 7 May 2020 15:16:33 +1000 Subject: [PATCH] Unbork session reset --- .../ConversationViewController.m | 2 +- .../SessionManagementProtocol.swift | 32 +++++++++++-------- .../src/Messages/OWSMessageManager.m | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index fce608bd0..cc4a39a30 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -1256,7 +1256,7 @@ typedef enum : NSUInteger { - (void)restoreSession { dispatch_async(dispatch_get_main_queue(), ^{ [OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [LKSessionManagementProtocol sending_startSessionResetInThread:self.thread using:transaction]; + [LKSessionManagementProtocol startSessionResetInThread:self.thread using:transaction]; }]; }); } diff --git a/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift index 118426946..82a4a0318 100644 --- a/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Session Management/SessionManagementProtocol.swift @@ -80,9 +80,8 @@ public final class SessionManagementProtocol : NSObject { } // MARK: - Sending - // TODO: Confusing that we have this but also a receiving version - @objc(sending_startSessionResetInThread:using:) - public static func sending_startSessionReset(in thread: TSThread, using transaction: YapDatabaseReadWriteTransaction) { + @objc(startSessionResetInThread:using:) + public static func startSessionReset(in thread: TSThread, using transaction: YapDatabaseReadWriteTransaction) { guard let thread = thread as? TSContactThread else { print("[Loki] Can't restore session for non contact thread.") return @@ -97,7 +96,7 @@ public final class SessionManagementProtocol : NSObject { } let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeLokiSessionResetInProgress) infoMessage.save(with: transaction) - thread.sessionResetStatus = .requestReceived + thread.sessionResetStatus = .initiated thread.save(with: transaction) thread.removeAllSessionRestoreDevices(with: transaction) } @@ -227,27 +226,32 @@ public final class SessionManagementProtocol : NSObject { storage.setPreKeyBundle(preKeyBundle, forContact: hexEncodedPublicKey, transaction: transaction) // If we received a friend request (i.e. also a new pre key bundle), but we were already friends with the other user, reset the session. // The envelope type is set during UD decryption. - // TODO: Should this ignore session requests? - if envelope.type == .friendRequest, // TODO: Should this check that the envelope doesn't have the session request flag set? - let thread = TSContactThread.getWithContactId(hexEncodedPublicKey, transaction: transaction), // TODO: Should this be getOrCreate? - thread.isContactFriend { - receiving_startSessionReset(in: thread, using: transaction) + if envelope.type == .friendRequest, + storage.getFriendRequestStatus(for: hexEncodedPublicKey, transaction: transaction) == .friends { + let thread = TSContactThread.getOrCreateThread(withContactId: hexEncodedPublicKey, transaction: transaction) + // Archive all sessions + storage.archiveAllSessions(forContact: hexEncodedPublicKey, protocolContext: transaction) + // Send an ephemeral message + let ephemeralMessage = EphemeralMessage(in: thread) + let messageSenderJobQueue = SSKEnvironment.shared.messageSenderJobQueue + messageSenderJobQueue.add(message: ephemeralMessage, transaction: transaction) } } // TODO: Confusing that we have this but also a sending version - @objc(receiving_startSessionResetInThread:using:) - public static func receiving_startSessionReset(in thread: TSContactThread, using transaction: YapDatabaseReadWriteTransaction) { + @objc(handleEndSessionMessageReceivedInThread:using:) + public static func handleEndSessionMessageReceived(in thread: TSContactThread, using transaction: YapDatabaseReadWriteTransaction) { let hexEncodedPublicKey = thread.contactIdentifier() - print("[Loki] Session reset request received from: \(hexEncodedPublicKey).") + print("[Loki] End session message received from: \(hexEncodedPublicKey).") + // Notify the user let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeLokiSessionResetInProgress) infoMessage.save(with: transaction) // Archive all sessions storage.archiveAllSessions(forContact: hexEncodedPublicKey, protocolContext: transaction) - // Update session reset status + // Update the session reset status thread.sessionResetStatus = .requestReceived thread.save(with: transaction) - // Send an ephemeral message to trigger session reset for the other party as well + // Send an ephemeral message let ephemeralMessage = EphemeralMessage(in: thread) let messageSenderJobQueue = SSKEnvironment.shared.messageSenderJobQueue messageSenderJobQueue.add(message: ephemeralMessage, transaction: transaction) diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index a161d2ae5..94716b5b6 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -1042,7 +1042,7 @@ NS_ASSUME_NONNULL_BEGIN // Loki: Handle session reset NSString *hexEncodedPublicKey = envelope.source; TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:hexEncodedPublicKey transaction:transaction]; - [LKSessionManagementProtocol receiving_startSessionResetInThread:thread using:transaction]; + [LKSessionManagementProtocol handleEndSessionMessageReceivedInThread:thread using:transaction]; } - (void)handleExpirationTimerUpdateMessageWithEnvelope:(SSKProtoEnvelope *)envelope