From 1a684aa4706bc7c95cfd1a4e8ffbd36c7a2c842f Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 9 Dec 2019 16:27:00 +1100 Subject: [PATCH] Reset session upon receiving a friend request from a contact we are friends with. --- SignalMessaging/contacts/OWSSyncManager.m | 9 +++++ .../src/Messages/OWSMessageManager.m | 40 +++++++++++-------- .../src/TestUtils/OWSMockSyncManager.swift | 6 +++ .../src/Util/OWSSyncManagerProtocol.h | 3 ++ 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/SignalMessaging/contacts/OWSSyncManager.m b/SignalMessaging/contacts/OWSSyncManager.m index 7fe2dac66..a7532e918 100644 --- a/SignalMessaging/contacts/OWSSyncManager.m +++ b/SignalMessaging/contacts/OWSSyncManager.m @@ -264,6 +264,15 @@ NSString *const kSyncManagerLastContactSyncKey = @"kTSStorageManagerOWSSyncManag return [self syncContactsForSignalAccounts:@[ signalAccount ]]; } +- (AnyPromise *)syncContact:(NSString *)hexEncodedPubKey transaction:(YapDatabaseReadTransaction *)transaction +{ + TSContactThread *thread = [TSContactThread getThreadWithContactId:hexEncodedPubKey transaction:transaction]; + if (thread != nil && thread.isContactFriend) { + return [self syncContactsForSignalAccounts:@[[[SignalAccount alloc] initWithRecipientId:hexEncodedPubKey]]]; + } + return [AnyPromise promiseWithValue:@1]; +} + - (AnyPromise *)syncAllContacts { NSMutableArray *friends = @[].mutableCopy; diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index f2a706e83..67849777b 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -447,6 +447,17 @@ NS_ASSUME_NONNULL_BEGIN OWSFailDebug(@"Failed to create a pre key bundle."); } [self.primaryStorage setPreKeyBundle:bundle forContact:envelope.source transaction:transaction]; + + // If we got a friend request and we were friends with this user then we need to reset our session + if (envelope.type == SSKProtoEnvelopeTypeFriendRequest) { + TSContactThread *thread = [TSContactThread getThreadWithContactId:envelope.source transaction:transaction]; + if (thread && thread.isContactFriend) { + [self resetSessionWithContact:envelope.source transaction:transaction]; + + // Let our other devices know that we have reset session + [SSKEnvironment.shared.syncManager syncContact:envelope.source transaction:transaction]; + } + } } // Loki: Handle address message if needed @@ -535,7 +546,10 @@ NS_ASSUME_NONNULL_BEGIN OWSFail(@"Missing transaction."); return; } - + + // Loki - Don't process session restore message + if ((dataMessage.flags & SSKProtoDataMessageFlagsSessionRestore) != 0) { return; } + if ([self isDataMessageBlocked:dataMessage envelope:envelope]) { NSString *logMessage = [NSString stringWithFormat:@"Ignoring blocked message from sender: %@", envelope.source]; if (dataMessage.group) { @@ -1081,23 +1095,21 @@ NS_ASSUME_NONNULL_BEGIN return; } - TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction]; + [self resetSessionWithContact:envelope.source transaction:transaction]; +} + +- (void)resetSessionWithContact:(NSString *)hexEncodedPublicKey + transaction:(YapDatabaseReadWriteTransaction *)transaction { + TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:hexEncodedPublicKey transaction:transaction]; // MJK TODO - safe to remove senderTimestamp [[[TSInfoMessage alloc] initWithTimestamp:NSDate.ows_millisecondTimeStamp inThread:thread messageType:TSInfoMessageTypeLokiSessionResetInProgress] saveWithTransaction:transaction]; - /* Loki: Original code - * ================ - [[[TSInfoMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp] - inThread:thread - messageType:TSInfoMessageTypeSessionDidEnd] saveWithTransaction:transaction]; - * ================ - */ // Loki: Archive all our sessions // Ref: SignalServiceKit/Loki/Docs/SessionReset.md - [self.primaryStorage archiveAllSessionsForContact:envelope.source protocolContext:transaction]; + [self.primaryStorage archiveAllSessionsForContact:hexEncodedPublicKey protocolContext:transaction]; // Loki: Set our session reset state thread.sessionResetState = TSContactThreadSessionResetStateRequestReceived; @@ -1107,13 +1119,7 @@ NS_ASSUME_NONNULL_BEGIN LKEphemeralMessage *emptyMessage = [[LKEphemeralMessage alloc] initInThread:thread]; [self.messageSenderJobQueue addMessage:emptyMessage transaction:transaction]; - NSLog(@"[Loki] Session reset received from %@.", envelope.source); - - /* Loki: Original code - * ================ - [self.primaryStorage deleteAllSessionsForContact:envelope.source protocolContext:transaction]; - * ================ - */ + NSLog(@"[Loki] Session reset received from %@.", hexEncodedPublicKey); } - (void)handleExpirationTimerUpdateMessageWithEnvelope:(SSKProtoEnvelope *)envelope diff --git a/SignalServiceKit/src/TestUtils/OWSMockSyncManager.swift b/SignalServiceKit/src/TestUtils/OWSMockSyncManager.swift index 2655257a6..a3dcb9187 100644 --- a/SignalServiceKit/src/TestUtils/OWSMockSyncManager.swift +++ b/SignalServiceKit/src/TestUtils/OWSMockSyncManager.swift @@ -19,6 +19,12 @@ public class OWSMockSyncManager: NSObject, OWSSyncManagerProtocol { return AnyPromise() } + + @objc public func syncContact(_ hexEncodedPubKey: String, transaction: YapDatabaseReadTransaction) -> AnyPromise { + Logger.info("") + + return AnyPromise() + } @objc public func syncAllContacts() -> AnyPromise { Logger.info("") diff --git a/SignalServiceKit/src/Util/OWSSyncManagerProtocol.h b/SignalServiceKit/src/Util/OWSSyncManagerProtocol.h index da5e28684..eafb2e6fa 100644 --- a/SignalServiceKit/src/Util/OWSSyncManagerProtocol.h +++ b/SignalServiceKit/src/Util/OWSSyncManagerProtocol.h @@ -6,6 +6,7 @@ NS_ASSUME_NONNULL_BEGIN @class AnyPromise; @class SignalAccount; +@class YapDatabaseReadTransaction; @protocol OWSSyncManagerProtocol @@ -13,6 +14,8 @@ NS_ASSUME_NONNULL_BEGIN - (AnyPromise *)syncLocalContact __attribute__((warn_unused_result)); +- (AnyPromise *)syncContact:(NSString *)hexEncodedPubKey transaction:(YapDatabaseReadTransaction *)transaction; + - (AnyPromise *)syncAllContacts __attribute__((warn_unused_result)); - (AnyPromise *)syncContactsForSignalAccounts:(NSArray *)signalAccounts __attribute__((warn_unused_result));