From 7493e037c99f5d5005dd76de26df0e5c36fe93f1 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 20 May 2019 11:20:03 +1000 Subject: [PATCH] Fix minor code style issues --- Signal/src/Jobs/SessionResetJob.swift | 22 +++---- .../Cells/OWSSystemMessageCell.m | 4 +- .../src/Contacts/Threads/TSContactThread.h | 6 +- .../Loki/Extensions/OWSPrimaryStorage+Loki.h | 2 +- .../Loki/Extensions/OWSPrimaryStorage+Loki.m | 9 +-- .../src/Loki/Extensions/SessionCipher+Loki.h | 10 +-- .../src/Loki/Extensions/SessionCipher+Loki.m | 62 ++++++------------- .../Loki/Messages/OWSFriendRequestMessage.h | 1 - .../src/Messages/Interactions/TSInfoMessage.h | 2 +- .../src/Messages/Interactions/TSInfoMessage.m | 2 +- .../src/Messages/OWSMessageDecrypter.m | 5 +- .../src/Messages/OWSMessageManager.m | 33 +++++----- 12 files changed, 64 insertions(+), 94 deletions(-) diff --git a/Signal/src/Jobs/SessionResetJob.swift b/Signal/src/Jobs/SessionResetJob.swift index 6e652dab8..55ddce922 100644 --- a/Signal/src/Jobs/SessionResetJob.swift +++ b/Signal/src/Jobs/SessionResetJob.swift @@ -108,9 +108,9 @@ public class SessionResetOperation: OWSOperation, DurableOperation { override public func run() { assert(self.durableOperationDelegate != nil) - /* Loki Original Code + /* Loki: Original code * We don't want to delete session. Ref: SignalServiceKit/Loki/Docs/SessionReset.md - * ================== + * ================ if firstAttempt { self.dbConnection.readWrite { transaction in Logger.info("deleting sessions for recipient: \(self.recipientId)") @@ -118,7 +118,8 @@ public class SessionResetOperation: OWSOperation, DurableOperation { } firstAttempt = false } - */ + * ================ + */ let endSessionMessage = EndSessionMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: self.contactThread) @@ -132,22 +133,21 @@ public class SessionResetOperation: OWSOperation, DurableOperation { // Otherwise if we send another message before them, they wont have the session to decrypt it. self.primaryStorage.archiveAllSessions(forContact: self.recipientId, protocolContext: transaction) - /* Loki original code - * ================== + /* Loki: Original code + * ================ let message = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: self.contactThread, messageType: TSInfoMessageType.typeSessionDidEnd) message.save(with: transaction) - */ + * ================ + */ if (self.contactThread.sessionResetState != .requestReceived) { - let message = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), - in: self.contactThread, - messageType: .typeLokiSessionResetProgress) + let message = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: self.contactThread, messageType: .typeLokiSessionResetProgress) message.save(with: transaction) - /// Loki: We have initiated a session reset - Logger.debug("[Loki Session Reset] Session reset has been initiated") + // Loki: We have initiated a session reset + Logger.debug("[Loki] Session reset has been initiated.") self.contactThread.sessionResetState = .initiated self.contactThread.save(with: transaction) } diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSSystemMessageCell.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSSystemMessageCell.m index da7c7454f..76a315c15 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSSystemMessageCell.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSSystemMessageCell.m @@ -273,7 +273,7 @@ typedef void (^SystemMessageActionBlock)(void); case TSInfoMessageAddGroupToProfileWhitelistOffer: case TSInfoMessageTypeGroupUpdate: case TSInfoMessageTypeGroupQuit: - case TSInfoMessageTypeLokiSessionResetProgress: + case TSInfoMessageTypeLokiSessionResetInProgress: case TSInfoMessageTypeLokiSessionResetDone: return nil; case TSInfoMessageTypeDisappearingMessagesUpdate: { @@ -461,7 +461,7 @@ typedef void (^SystemMessageActionBlock)(void); switch (message.messageType) { case TSInfoMessageUserNotRegistered: case TSInfoMessageTypeSessionDidEnd: - case TSInfoMessageTypeLokiSessionResetProgress: + case TSInfoMessageTypeLokiSessionResetInProgress: case TSInfoMessageTypeLokiSessionResetDone: return nil; case TSInfoMessageTypeUnsupportedMessage: diff --git a/SignalServiceKit/src/Contacts/Threads/TSContactThread.h b/SignalServiceKit/src/Contacts/Threads/TSContactThread.h index 8519e260a..19de31641 100644 --- a/SignalServiceKit/src/Contacts/Threads/TSContactThread.h +++ b/SignalServiceKit/src/Contacts/Threads/TSContactThread.h @@ -10,9 +10,9 @@ NS_ASSUME_NONNULL_BEGIN typedef NS_ENUM(NSInteger, TSContactThreadSessionResetState) { // No ongoing session reset TSContactThreadSessionResetStateNone, - // We initiated session reset + // We initiated a session reset TSContactThreadSessionResetStateInitiated, - // We received the session reset + // We received a session reset TSContactThreadSessionResetStateRequestReceived, }; @@ -20,7 +20,7 @@ extern NSString *const TSContactThreadPrefix; @interface TSContactThread : TSThread -// Loki: The current session reset state with this thread +// Loki: The current session reset state for this thread @property (atomic) TSContactThreadSessionResetState sessionResetState; @property (nonatomic) BOOL hasDismissedOffers; diff --git a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.h b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.h index df1ee385b..9a9a694b4 100644 --- a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.h +++ b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.h @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN @param pubKey The hex encoded public key of the contact. @param transaction A `YapDatabaseReadTransaction`. - @return The record associated with the contact or nil if it didn't exist. + @return The record associated with the contact or `nil` if it doesn't exist. */ - (PreKeyRecord *_Nullable)getPreKeyForContact:(NSString *)pubKey transaction:(YapDatabaseReadTransaction *)transaction; diff --git a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m index 4ae75ee58..0d2698e31 100644 --- a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m +++ b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m @@ -38,13 +38,10 @@ int preKeyId = [transaction intForKey:pubKey inCollection:LokiPreKeyContactCollection]; // If we don't have an id then return nil - if (preKeyId <= 0) { - return nil; - } + if (preKeyId <= 0) { return nil; } - /// thows_loadPreKey doesn't allow us to pass transaction ;( - return [transaction preKeyRecordForKey:[self keyFromInt:preKeyId] - inCollection:OWSPrimaryStoragePreKeyStoreCollection]; + /// throws_loadPreKey doesn't allow us to pass transaction ;( + return [transaction preKeyRecordForKey:[self keyFromInt:preKeyId] inCollection:OWSPrimaryStoragePreKeyStoreCollection]; } - (PreKeyRecord *)getOrCreatePreKeyForContact:(NSString *)pubKey { diff --git a/SignalServiceKit/src/Loki/Extensions/SessionCipher+Loki.h b/SignalServiceKit/src/Loki/Extensions/SessionCipher+Loki.h index 60f8f170f..ad261ebf9 100644 --- a/SignalServiceKit/src/Loki/Extensions/SessionCipher+Loki.h +++ b/SignalServiceKit/src/Loki/Extensions/SessionCipher+Loki.h @@ -1,4 +1,4 @@ -/// Loki: Refer to Docs/SessionReset.md for explanations +// Loki: Refer to Docs/SessionReset.md for explanations #import "SessionCipher.h" @@ -11,14 +11,14 @@ extern NSString *const kNSNotificationKey_ContactPubKey; /** Decrypt the given `CipherMessage`. - This function is a wrapper around `throws_decrypt:protocolContext:` and adds on the custom loki session handling ontop. - Refer to SignalServiceKit/Loki/Docs/SessionReset.md for overview on how it works. + This function is a wrapper around `throws_decrypt:protocolContext:` and adds on the custom Loki session handling. + Refer to SignalServiceKit/Loki/Docs/SessionReset.md for an overview of how it works. @param whisperMessage The cipher message. - @param protocolContext The protocol context (YapDatabaseReadWriteTransaction) + @param protocolContext The protocol context (a `YapDatabaseReadWriteTransaction`). @return The decrypted data. */ -- (NSData *)throws_lokiDecrypt:(id)whisperMessage protocolContext:(nullable id)protocolContext NS_SWIFT_UNAVAILABLE("throws objc exceptions"); +- (NSData *)throws_lokiDecrypt:(id)whisperMessage protocolContext:(nullable id)protocolContext NS_SWIFT_UNAVAILABLE("throws Obj-C exceptions"); @end diff --git a/SignalServiceKit/src/Loki/Extensions/SessionCipher+Loki.m b/SignalServiceKit/src/Loki/Extensions/SessionCipher+Loki.m index 50aabea91..a98e3b7c2 100644 --- a/SignalServiceKit/src/Loki/Extensions/SessionCipher+Loki.m +++ b/SignalServiceKit/src/Loki/Extensions/SessionCipher+Loki.m @@ -1,4 +1,4 @@ -/// Loki: Refer to Docs/SessionReset.md for explanations +// Loki: Refer to Docs/SessionReset.md for explanations #import "SessionCipher+Loki.h" #import "NSNotificationCenter+OWS.h" @@ -47,36 +47,26 @@ NSString *const kNSNotificationKey_ContactPubKey = @"kNSNotificationKey_ContactP SessionState *state = record.sessionState; // Check if session is initialized - if (!state.hasSenderChain) { - return nil; - } + if (!state.hasSenderChain) { return nil; } return state; } -/// Handle any loki session reset stuff -- (void)handleSessionReset:(id)whisperMessage - previousState:(SessionState *_Nullable)previousState - protocolContext:(nullable id)protocolContext +/// Handle any Loki session reset stuff +- (void)handleSessionReset:(id)whisperMessage previousState:(SessionState *_Nullable)previousState protocolContext:(nullable id)protocolContext { // Don't bother doing anything if we didn't have a session before - if (!previousState) { - return; - } + if (!previousState) { return; } OWSAssertDebug([protocolContext isKindOfClass:[YapDatabaseReadWriteTransaction class]]); YapDatabaseReadWriteTransaction *transaction = protocolContext; // Get the thread TSContactThread *thread = [TSContactThread getThreadWithContactId:self.recipientId transaction:transaction]; - if (!thread) { - return; - } + if (!thread) { return; } // Bail early if no session reset is in progress - if (thread.sessionResetState == TSContactThreadSessionResetStateNone) { - return; - } + if (thread.sessionResetState == TSContactThreadSessionResetStateNone) { return; } BOOL sessionResetReceived = thread.sessionResetState == TSContactThreadSessionResetStateRequestReceived; SessionState *_Nullable currentState = [self getCurrentState:protocolContext]; @@ -106,12 +96,8 @@ NSString *const kNSNotificationKey_ContactPubKey = @"kNSNotificationKey_ContactP /// Send a notification about a new session being adopted - (void)notifySessionAdopted { - [[NSNotificationCenter defaultCenter] - postNotificationNameAsync:kNSNotificationName_SessionAdopted - object:nil - userInfo:@{ - kNSNotificationKey_ContactPubKey : self.recipientId, - }]; + NSDictionary *userInfo = @{ kNSNotificationKey_ContactPubKey : self.recipientId }; + [NSNotificationCenter.defaultCenter postNotificationNameAsync:kNSNotificationName_SessionAdopted object:nil userInfo:userInfo]; } /// Delete all other sessions except the given one @@ -121,10 +107,7 @@ NSString *const kNSNotificationKey_ContactPubKey = @"kNSNotificationKey_ContactP [record removePreviousSessionStates]; [record setState:state]; - [self.sessionStore storeSession:self.recipientId - deviceId:self.deviceId - session:record - protocolContext:protocolContext]; + [self.sessionStore storeSession:self.recipientId deviceId:self.deviceId session:record protocolContext:protocolContext]; } /// Set the given session as the active one while archiving the old one @@ -136,17 +119,14 @@ NSString *const kNSNotificationKey_ContactPubKey = @"kNSNotificationKey_ContactP [record.previousSessionStates enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(SessionState *obj, NSUInteger idx, BOOL *stop) { if ([state.aliceBaseKey isEqualToData:obj.aliceBaseKey]) { [record.previousSessionStates removeObjectAtIndex:idx]; - *stop = true; + *stop = YES; } }]; // Promote it so the previous state gets archived [record promoteState:state]; - [self.sessionStore storeSession:self.recipientId - deviceId:self.deviceId - session:record - protocolContext:protocolContext]; + [self.sessionStore storeSession:self.recipientId deviceId:self.deviceId session:record protocolContext:protocolContext]; } /// Check that we have matching prekeys in the case of a `PreKeyWhisperMessage` @@ -155,26 +135,22 @@ NSString *const kNSNotificationKey_ContactPubKey = @"kNSNotificationKey_ContactP OWSAssertDebug([protocolContext isKindOfClass:[YapDatabaseReadTransaction class]]); YapDatabaseReadTransaction *transaction = protocolContext; - /// We only want to look at `PreKeyWhisperMessage` - if (![whisperMessage isKindOfClass:[PreKeyWhisperMessage class]]) { - return; - } + // We only want to look at `PreKeyWhisperMessage` + if (![whisperMessage isKindOfClass:[PreKeyWhisperMessage class]]) { return; } - /// We need the primary storage to access contact prekeys - if (![self.prekeyStore isKindOfClass:[OWSPrimaryStorage class]]) { - return; - } + // We need the primary storage to access contact prekeys + if (![self.prekeyStore isKindOfClass:[OWSPrimaryStorage class]]) { return; } PreKeyWhisperMessage *preKeyMessage = whisperMessage; OWSPrimaryStorage *primaryStorage = self.prekeyStore; PreKeyRecord *_Nullable storedPreKey = [primaryStorage getPreKeyForContact:self.recipientId transaction:transaction]; - if(!storedPreKey) { - OWSRaiseException(@"LokiInvalidPreKey", @"Received a friend request from a pubkey for which no prekey bundle was created"); + if (!storedPreKey) { + OWSRaiseException(@"LokiInvalidPreKey", @"Received a friend request from a public key for which no prekey bundle was created."); } if (storedPreKey.Id != preKeyMessage.prekeyID) { - OWSRaiseException(@"LokiPreKeyIdsDontMatch", @"Received a preKeyWhisperMessage (friend request accept) from an unknown source"); + OWSRaiseException(@"LokiPreKeyIdsDontMatch", @"Received a PreKeyWhisperMessage (friend request accept) from an unknown source."); } } diff --git a/SignalServiceKit/src/Loki/Messages/OWSFriendRequestMessage.h b/SignalServiceKit/src/Loki/Messages/OWSFriendRequestMessage.h index 320b3662f..432706588 100644 --- a/SignalServiceKit/src/Loki/Messages/OWSFriendRequestMessage.h +++ b/SignalServiceKit/src/Loki/Messages/OWSFriendRequestMessage.h @@ -1,4 +1,3 @@ -#import "SignalServiceKit.h" #import "TSOutgoingMessage.h" NS_ASSUME_NONNULL_BEGIN diff --git a/SignalServiceKit/src/Messages/Interactions/TSInfoMessage.h b/SignalServiceKit/src/Messages/Interactions/TSInfoMessage.h index b3ded8455..26c42e819 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSInfoMessage.h +++ b/SignalServiceKit/src/Messages/Interactions/TSInfoMessage.h @@ -21,7 +21,7 @@ typedef NS_ENUM(NSInteger, TSInfoMessageType) { TSInfoMessageVerificationStateChange, TSInfoMessageAddUserToProfileWhitelistOffer, TSInfoMessageAddGroupToProfileWhitelistOffer, - TSInfoMessageTypeLokiSessionResetProgress, + TSInfoMessageTypeLokiSessionResetInProgress, TSInfoMessageTypeLokiSessionResetDone, }; diff --git a/SignalServiceKit/src/Messages/Interactions/TSInfoMessage.m b/SignalServiceKit/src/Messages/Interactions/TSInfoMessage.m index 262fc20c1..483b44529 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSInfoMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSInfoMessage.m @@ -117,7 +117,7 @@ NSUInteger TSInfoMessageSchemaVersion = 1; - (NSString *)previewTextWithTransaction:(YapDatabaseReadTransaction *)transaction { switch (_messageType) { - case TSInfoMessageTypeLokiSessionResetProgress: + case TSInfoMessageTypeLokiSessionResetInProgress: return NSLocalizedString(@"Secure session reset in progress", nil); case TSInfoMessageTypeLokiSessionResetDone: return NSLocalizedString(@"Secure session reset done", nil); diff --git a/SignalServiceKit/src/Messages/OWSMessageDecrypter.m b/SignalServiceKit/src/Messages/OWSMessageDecrypter.m index 52366e998..82f0ea0c2 100644 --- a/SignalServiceKit/src/Messages/OWSMessageDecrypter.m +++ b/SignalServiceKit/src/Messages/OWSMessageDecrypter.m @@ -438,10 +438,11 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes NSData *_Nullable plaintextData = [[cipher throws_lokiDecrypt:cipherMessage protocolContext:transaction] removePadding]; - /* Loki original code - * ================= + /* Loki: Original code + * ================ NSData *_Nullable plaintextData = [[cipher throws_decrypt:cipherMessage protocolContext:transaction] removePadding]; + * ================ */ OWSMessageDecryptResult *result = [OWSMessageDecryptResult resultWithEnvelopeData:envelopeData plaintextData:plaintextData diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index dd9e359c2..f91b852ac 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -88,11 +88,8 @@ NS_ASSUME_NONNULL_BEGIN _dbConnection = primaryStorage.newDatabaseConnection; _incomingMessageFinder = [[OWSIncomingMessageFinder alloc] initWithPrimaryStorage:primaryStorage]; - /// Loki: Add observation for new session - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(onNewSessionAdopted:) - name:kNSNotificationName_SessionAdopted - object:nil]; + // Loki: Add observation for new session + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(onNewSessionAdopted:) name:kNSNotificationName_SessionAdopted object:nil]; OWSSingletonAssert(); @@ -100,7 +97,7 @@ NS_ASSUME_NONNULL_BEGIN } - (void)dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self]; + [NSNotificationCenter.defaultCenter removeObserver:self]; } #pragma mark - Dependencies @@ -1004,14 +1001,15 @@ NS_ASSUME_NONNULL_BEGIN TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction]; // MJK TODO - safe to remove senderTimestamp - [[[TSInfoMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp] + [[[TSInfoMessage alloc] initWithTimestamp:NSDate.ows_millisecondTimeStamp inThread:thread - messageType:TSInfoMessageTypeLokiSessionResetProgress] saveWithTransaction:transaction]; - /* Loki original code - * ================== + messageType:TSInfoMessageTypeLokiSessionResetInProgress] saveWithTransaction:transaction]; + /* Loki: Original code + * ================ [[[TSInfoMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp] inThread:thread messageType:TSInfoMessageTypeSessionDidEnd] saveWithTransaction:transaction]; + * ================ */ /// Loki: Archive all our sessions @@ -1026,11 +1024,12 @@ NS_ASSUME_NONNULL_BEGIN OWSEphemeralMessage *emptyMessage = [OWSEphemeralMessage createEmptyOutgoingMessageInThread:thread]; [self.messageSenderJobQueue addMessage:emptyMessage transaction:transaction]; - OWSLogDebug(@"[Loki Session Reset] Session reset has been received from %@", envelope.source); + OWSLogDebug(@"[Loki] Session reset has been received from %@.", envelope.source); - /* Loki Original Code - * =================== + /* Loki: Original code + * ================ [self.primaryStorage deleteAllSessionsForContact:envelope.source protocolContext:transaction]; + * ================ */ } @@ -1688,15 +1687,13 @@ NS_ASSUME_NONNULL_BEGIN - (void)onNewSessionAdopted:(NSNotification *)notification { NSString *pubKey = notification.userInfo[kNSNotificationKey_ContactPubKey]; - if (pubKey.length == 0) { - return; - } + if (pubKey.length == 0) { return; } [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { TSContactThread *_Nullable thread = [TSContactThread getThreadWithContactId:pubKey transaction:transaction]; if (!thread) { - OWSLogDebug(@"[Loki Session Reset] New session was adopted but we failed to get the thread for %@", pubKey); + OWSLogDebug(@"[Loki] New session was adopted but we failed to get the thread for %@.", pubKey); return; } @@ -1707,7 +1704,7 @@ NS_ASSUME_NONNULL_BEGIN } // Show session reset done message - [[[TSInfoMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp] + [[[TSInfoMessage alloc] initWithTimestamp:NSDate.ows_millisecondTimeStamp inThread:thread messageType:TSInfoMessageTypeLokiSessionResetDone] saveWithTransaction:transaction];