Handle session adoption correctly

This commit is contained in:
Mikunj 2020-02-17 12:39:22 +11:00
parent 5bb9d135b1
commit 080b24b282
2 changed files with 26 additions and 33 deletions

View File

@ -41,4 +41,30 @@ public class LokiSessionReset: NSObject, SessionResetProtocol {
guard let thread = TSContactThread.getWithContactId(recipientId, transaction: transaction) else { return .none }
return thread.sessionResetStatus
}
public func onNewSessionAdopted(for recipientId: String, protocolContext: Any?) {
guard let transaction = protocolContext as? YapDatabaseReadWriteTransaction else {
Logger.warn("[Loki] Cannot handle new session adoption because invalid transaction was passed")
return
}
guard recipientId.count > 0 else { return }
guard let thread = TSContactThread.getWithContactId(recipientId, transaction: transaction) else {
Logger.debug("[Loki] A new session was adopted but the thread couldn't be found for \(recipientId)")
return
}
// If the current user initiated the reset then send back an empty message to acknowledge the completion of the session reset
if (thread.sessionResetStatus == .initiated) {
let emptyMessage = EphemeralMessage(in: thread)
SSKEnvironment.shared.messageSender.sendPromise(message: emptyMessage).retainUntilComplete()
}
// Show session reset done message
TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeLokiSessionResetDone).save(with: transaction)
// Clear the session reset status
thread.sessionResetStatus = .none
thread.save(with: transaction)
}
}

View File

@ -93,9 +93,6 @@ NS_ASSUME_NONNULL_BEGIN
_primaryStorage = primaryStorage;
_dbConnection = primaryStorage.newDatabaseConnection;
_incomingMessageFinder = [[OWSIncomingMessageFinder alloc] initWithPrimaryStorage:primaryStorage];
// Loki: Observe session changes
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleNewSessionAdopted:) name:LKSessionCipher.sessionAdoptedNotification object:nil];
OWSSingletonAssert();
@ -2019,36 +2016,6 @@ NS_ASSUME_NONNULL_BEGIN
}
}
# pragma mark - Loki Session Handling
- (void)handleNewSessionAdopted:(NSNotification *)notification {
NSString *hexEncodedPublicKey = notification.userInfo[LKSessionCipher.contactPubKeyField];
if (hexEncodedPublicKey.length == 0) { return; }
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSContactThread *thread = [TSContactThread getThreadWithContactId:hexEncodedPublicKey transaction:transaction];
if (thread == nil) {
NSLog(@"[Loki] A new session was adopted but the thread couldn't be found for: %@.", hexEncodedPublicKey);
return;
}
// If the current user initiated the reset then send back an empty message to acknowledge the completion of the session reset
if (thread.sessionResetStatus == LKSessionResetStatusInitiated) {
LKEphemeralMessage *emptyMessage = [[LKEphemeralMessage alloc] initInThread:thread];
[self.messageSenderJobQueue addMessage:emptyMessage transaction:transaction];
}
// Show session reset done message
[[[TSInfoMessage alloc] initWithTimestamp:NSDate.ows_millisecondTimeStamp
inThread:thread
messageType:TSInfoMessageTypeLokiSessionResetDone] saveWithTransaction:transaction];
// Clear the session reset state
thread.sessionResetStatus = LKSessionResetStatusNone;
[thread saveWithTransaction:transaction];
}];
}
@end
NS_ASSUME_NONNULL_END