Add protocol context to protocol kit.

This commit is contained in:
Matthew Chen 2018-01-30 15:49:36 -05:00
parent 074046b98e
commit d3e16583eb
17 changed files with 144 additions and 124 deletions

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
@ -26,19 +26,19 @@ class SessionResetJob: NSObject {
func run() {
Logger.info("\(TAG) Local user reset session.")
OWSDispatch.sessionStoreQueue().async {
TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in
Logger.info("\(self.TAG) deleting sessions for recipient: \(self.recipientId)")
self.storageManager.deleteAllSessions(forContact: self.recipientId)
self.storageManager.deleteAllSessions(forContact: self.recipientId, protocolContext: transaction)
DispatchQueue.main.async {
let endSessionMessage = EndSessionMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: self.thread)
self.messageSender.enqueue(endSessionMessage, success: {
OWSDispatch.sessionStoreQueue().async {
TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in
// Archive the just-created session since the recipient should delete their corresponding
// session upon receiving and decrypting our EndSession message.
// Otherwise if we send another message before them, they wont have the session to decrypt it.
self.storageManager.archiveAllSessions(forContact: self.recipientId)
self.storageManager.archiveAllSessions(forContact: self.recipientId, protocolContext: transaction)
}
Logger.info("\(self.TAG) successfully sent EndSessionMessage.")
let message = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(),
@ -46,7 +46,7 @@ class SessionResetJob: NSObject {
messageType: TSInfoMessageType.typeSessionDidEnd)
message.save()
}, failure: {error in
OWSDispatch.sessionStoreQueue().async {
TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in
// Even though this is the error handler - which means probably the recipient didn't receive the message
// there's a chance that our send did succeed and the server just timed out our repsonse or something.
// Since the cost of sending a future message using a session the recipient doesn't have is so high,
@ -55,7 +55,7 @@ class SessionResetJob: NSObject {
// Archive the just-created session since the recipient should delete their corresponding
// session upon receiving and decrypting our EndSession message.
// Otherwise if we send another message before them, they wont have the session to decrypt it.
self.storageManager.archiveAllSessions(forContact: self.recipientId)
self.storageManager.archiveAllSessions(forContact: self.recipientId, protocolContext: transaction)
}
Logger.error("\(self.TAG) failed to send EndSessionMessage with error: \(error.localizedDescription)")
})

View File

@ -80,6 +80,7 @@
#import <SignalServiceKit/OWSIdentityManager.h>
#import <SignalServiceKit/OWSMessageManager.h>
#import <SignalServiceKit/OWSMessageSender.h>
#import <SignalServiceKit/OWSMessageUtils.h>
#import <SignalServiceKit/OWSReadReceiptManager.h>
#import <SignalServiceKit/OWSVerificationStateChangeMessage.h>
#import <SignalServiceKit/SignalRecipient.h>
@ -202,7 +203,6 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
@property (nonatomic, readonly) ContactsUpdater *contactsUpdater;
@property (nonatomic, readonly) OWSMessageSender *messageSender;
@property (nonatomic, readonly) TSStorageManager *storageManager;
@property (nonatomic, readonly) OWSMessageManager *messagesManager;
@property (nonatomic, readonly) TSNetworkManager *networkManager;
@property (nonatomic, readonly) OutboundCallInitiator *outboundCallInitiator;
@property (nonatomic, readonly) OWSBlockingManager *blockingManager;
@ -274,7 +274,6 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
_messageSender = [Environment current].messageSender;
_outboundCallInitiator = SignalApp.sharedApp.outboundCallInitiator;
_storageManager = [TSStorageManager sharedManager];
_messagesManager = [OWSMessageManager sharedManager];
_networkManager = [TSNetworkManager sharedManager];
_blockingManager = [OWSBlockingManager sharedManager];
_contactsViewHelper = [[ContactsViewHelper alloc] initWithDelegate:self];
@ -700,7 +699,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
{
NSMutableArray<NSString *> *result = [NSMutableArray new];
for (NSString *recipientId in self.thread.recipientIdentifiers) {
if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId]
if ([[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId]
== OWSVerificationStateNoLongerVerified) {
[result addObject:recipientId];
}
@ -927,10 +926,14 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
continue;
}
[OWSIdentityManager.sharedManager setVerificationState:OWSVerificationStateDefault
identityKey:identityKey
recipientId:recipientId
isUserInitiatedChange:YES];
[TSStorageManager.protocolStoreDBConnection
asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[OWSIdentityManager.sharedManager setVerificationState:OWSVerificationStateDefault
identityKey:identityKey
recipientId:recipientId
isUserInitiatedChange:YES
protocolContext:transaction];
}];
}
}
@ -1300,7 +1303,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
BOOL isVerified = YES;
for (NSString *recipientId in self.thread.recipientIdentifiers) {
if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId]
if ([[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId]
!= OWSVerificationStateVerified) {
isVerified = NO;
break;
@ -3586,7 +3589,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
- (void)updateBackButtonUnreadCount
{
OWSAssertIsOnMainThread();
self.backButtonUnreadCount = [self.messagesManager unreadMessagesCountExcept:self.thread];
self.backButtonUnreadCount = [OWSMessageUtils.sharedManager unreadMessagesCountExcept:self.thread];
}
- (void)setBackButtonUnreadCount:(NSUInteger)unreadCount

View File

@ -1258,7 +1258,11 @@ NS_ASSUME_NONNULL_BEGIN
NSData *envelopeData = [envelopeBuilder build].data;
OWSAssert(envelopeData);
[[OWSBatchMessageProcessor sharedInstance] enqueueEnvelopeData:envelopeData plaintextData:plaintextData];
[TSStorageManager.protocolStoreDBConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[[OWSBatchMessageProcessor sharedInstance] enqueueEnvelopeData:envelopeData
plaintextData:plaintextData
transaction:transaction];
}];
}
+ (void)performRandomActions:(int)counter thread:(TSThread *)thread

View File

@ -30,9 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
}],
[OWSTableItem itemWithTitle:@"Log All Sessions"
actionBlock:^{
dispatch_async([OWSDispatch sessionStoreQueue], ^{
[[TSStorageManager sharedManager] printAllSessions];
});
[[TSStorageManager sharedManager] printAllSessions];
}],
[OWSTableItem itemWithTitle:@"Toggle Key Change"
actionBlock:^{
@ -41,33 +39,39 @@ NS_ASSUME_NONNULL_BEGIN
OWSIdentityManager *identityManager = [OWSIdentityManager sharedManager];
NSString *recipientId = [thread contactIdentifier];
NSData *currentKey = [identityManager identityKeyForRecipientIdWOT:recipientId];
NSMutableData *flippedKey = [NSMutableData new];
const char *currentKeyBytes = currentKey.bytes;
for (NSUInteger i = 0; i < currentKey.length; i++) {
const char xorByte = currentKeyBytes[i] ^ 0xff;
[flippedKey appendBytes:&xorByte length:1];
}
OWSAssert(flippedKey.length == currentKey.length);
[identityManager saveRemoteIdentity:flippedKey
recipientId:recipientId
protocolContext:protocolContext];
[TSStorageManager.protocolStoreDBConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
NSData *currentKey = [identityManager identityKeyForRecipientId:recipientId
protocolContext:transaction];
NSMutableData *flippedKey = [NSMutableData new];
const char *currentKeyBytes = currentKey.bytes;
for (NSUInteger i = 0; i < currentKey.length; i++) {
const char xorByte = currentKeyBytes[i] ^ 0xff;
[flippedKey appendBytes:&xorByte length:1];
}
OWSAssert(flippedKey.length == currentKey.length);
[identityManager saveRemoteIdentity:flippedKey
recipientId:recipientId
protocolContext:transaction];
}];
}],
[OWSTableItem itemWithTitle:@"Delete all sessions"
actionBlock:^{
dispatch_async([OWSDispatch sessionStoreQueue], ^{
[[TSStorageManager sharedManager]
deleteAllSessionsForContact:thread.contactIdentifier
protocolContext:protocolContext];
});
[TSStorageManager.protocolStoreDBConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[[TSStorageManager sharedManager]
deleteAllSessionsForContact:thread.contactIdentifier
protocolContext:transaction];
}];
}],
[OWSTableItem itemWithTitle:@"Archive all sessions"
actionBlock:^{
dispatch_async([OWSDispatch sessionStoreQueue], ^{
[[TSStorageManager sharedManager]
archiveAllSessionsForContact:thread.contactIdentifier
protocolContext:protocolContext];
});
[TSStorageManager.protocolStoreDBConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[[TSStorageManager sharedManager]
archiveAllSessionsForContact:thread.contactIdentifier
protocolContext:transaction];
}];
}],
[OWSTableItem itemWithTitle:@"Send session reset"
actionBlock:^{
@ -101,32 +105,20 @@ NS_ASSUME_NONNULL_BEGIN
#if DEBUG
+ (void)clearSessionAndIdentityStore
{
dispatch_async([OWSDispatch sessionStoreQueue], ^{
[[TSStorageManager sharedManager] resetSessionStore];
dispatch_async(dispatch_get_main_queue(), ^{
[[OWSIdentityManager sharedManager] clearIdentityState];
});
});
[[TSStorageManager sharedManager] resetSessionStore];
[[OWSIdentityManager sharedManager] clearIdentityState];
}
+ (void)snapshotSessionAndIdentityStore
{
dispatch_async([OWSDispatch sessionStoreQueue], ^{
[[TSStorageManager sharedManager] snapshotSessionStore];
dispatch_async(dispatch_get_main_queue(), ^{
[[OWSIdentityManager sharedManager] snapshotIdentityState];
});
});
[[TSStorageManager sharedManager] snapshotSessionStore];
[[OWSIdentityManager sharedManager] snapshotIdentityState];
}
+ (void)restoreSessionAndIdentityStore
{
dispatch_async([OWSDispatch sessionStoreQueue], ^{
[[TSStorageManager sharedManager] restoreSessionStore];
dispatch_async(dispatch_get_main_queue(), ^{
[[OWSIdentityManager sharedManager] restoreIdentityState];
});
});
[[TSStorageManager sharedManager] restoreSessionStore];
[[OWSIdentityManager sharedManager] restoreIdentityState];
}
#endif

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "FingerprintViewController.h"
@ -338,7 +338,8 @@ typedef void (^CustomLayoutBlock)(void);
{
OWSAssert(self.recipientId.length > 0);
BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:self.recipientId]
BOOL isVerified =
[[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:self.recipientId]
== OWSVerificationStateVerified;
if (isVerified) {
@ -512,15 +513,19 @@ typedef void (^CustomLayoutBlock)(void);
- (void)verifyUnverifyButtonTapped:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateRecognized) {
BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:self.recipientId]
== OWSVerificationStateVerified;
[TSStorageManager.protocolStoreDBConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:self.recipientId
transaction:transaction]
== OWSVerificationStateVerified;
OWSVerificationState newVerificationState
= (isVerified ? OWSVerificationStateDefault : OWSVerificationStateVerified);
[[OWSIdentityManager sharedManager] setVerificationState:newVerificationState
identityKey:self.identityKey
recipientId:self.recipientId
isUserInitiatedChange:YES];
OWSVerificationState newVerificationState
= (isVerified ? OWSVerificationStateDefault : OWSVerificationStateVerified);
[[OWSIdentityManager sharedManager] setVerificationState:newVerificationState
identityKey:self.identityKey
recipientId:self.recipientId
isUserInitiatedChange:YES
protocolContext:transaction];
}];
[self dismissViewControllerAnimated:YES completion:nil];
}

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "FingerprintViewScanController.h"
@ -200,10 +200,15 @@ NS_ASSUME_NONNULL_BEGIN
@"Button that marks user as verified after a successful fingerprint scan.")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[OWSIdentityManager.sharedManager setVerificationState:OWSVerificationStateVerified
identityKey:identityKey
recipientId:recipientId
isUserInitiatedChange:YES];
[TSStorageManager.protocolStoreDBConnection
asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[OWSIdentityManager.sharedManager
setVerificationState:OWSVerificationStateVerified
identityKey:identityKey
recipientId:recipientId
isUserInitiatedChange:YES
protocolContext:transaction];
}];
[viewController dismissViewControllerAnimated:true completion:nil];
}]];
UIAlertAction *dismissAction =

View File

@ -25,6 +25,7 @@
#import <SignalServiceKit/NSDate+OWS.h>
#import <SignalServiceKit/OWSBlockingManager.h>
#import <SignalServiceKit/OWSMessageSender.h>
#import <SignalServiceKit/OWSMessageUtils.h>
#import <SignalServiceKit/TSOutgoingMessage.h>
#import <SignalServiceKit/Threading.h>
#import <YapDatabase/YapDatabase.h>
@ -57,7 +58,6 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
@property (nonatomic, readonly) AccountManager *accountManager;
@property (nonatomic, readonly) OWSContactsManager *contactsManager;
@property (nonatomic, readonly) OWSMessageManager *messagesManager;
@property (nonatomic, readonly) OWSMessageSender *messageSender;
@property (nonatomic, readonly) OWSBlockingManager *blockingManager;
@ -106,7 +106,6 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
{
_accountManager = SignalApp.sharedApp.accountManager;
_contactsManager = [Environment current].contactsManager;
_messagesManager = [OWSMessageManager sharedManager];
_messageSender = [Environment current].messageSender;
_blockingManager = [OWSBlockingManager sharedManager];
_blockedPhoneNumberSet = [NSSet setWithArray:[_blockingManager blockedPhoneNumbers]];
@ -752,7 +751,7 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
- (void)updateInboxCountLabel
{
NSUInteger numberOfItems = [self.messagesManager unreadMessagesCount];
NSUInteger numberOfItems = [OWSMessageUtils.sharedManager unreadMessagesCount];
NSString *unreadString = NSLocalizedString(@"WHISPER_NAV_BAR_TITLE", nil);
if (numberOfItems > 0) {

View File

@ -205,7 +205,7 @@ const NSUInteger kAvatarViewDiameter = 52;
}
NSAttributedString *attributedDate = [self dateAttributedString:thread.lastMessageDate];
NSUInteger unreadCount = [[OWSMessageManager sharedManager] unreadMessagesInThread:thread];
NSUInteger unreadCount = [[OWSMessageUtils sharedManager] unreadMessagesInThread:thread];
[[NSNotificationCenter defaultCenter] addObserver:self

View File

@ -682,7 +682,8 @@ NS_ASSUME_NONNULL_BEGIN
}
}
BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId]
BOOL isVerified =
[[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId]
== OWSVerificationStateVerified;
if (isVerified) {
NSMutableAttributedString *subtitle = [NSMutableAttributedString new];

View File

@ -149,7 +149,7 @@ NS_ASSUME_NONNULL_BEGIN
// Optimistically set this flag.
[OWSDeviceManager.sharedManager setMayHaveLinkedDevices];
ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair];
ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPairWithoutProtocolContext];
OWSAssert(identityKeyPair);
NSData *myPublicKey = identityKeyPair.publicKey;
NSData *myPrivateKey = identityKeyPair.ows_privateKey;

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
@ -55,8 +55,8 @@ class SafetyNumberConfirmationAlert: NSObject {
let confirmAction = UIAlertAction(title: confirmationText, style: .default) { _ in
Logger.info("\(self.TAG) Confirmed identity: \(untrustedIdentity)")
OWSDispatch.sessionStoreQueue().async {
OWSIdentityManager.shared().setVerificationState(.default, identityKey: untrustedIdentity.identityKey, recipientId: untrustedIdentity.recipientId, isUserInitiatedChange: true)
TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in
OWSIdentityManager.shared().setVerificationState(.default, identityKey: untrustedIdentity.identityKey, recipientId: untrustedIdentity.recipientId, isUserInitiatedChange: true, protocolContext: transaction)
DispatchQueue.main.async {
completion(true)
}

View File

@ -177,7 +177,7 @@ NS_ASSUME_NONNULL_BEGIN
ContactTableViewCell *cell = [ContactTableViewCell new];
SignalAccount *signalAccount = [helper signalAccountForRecipientId:recipientId];
OWSVerificationState verificationState =
[[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId];
[[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId];
BOOL isVerified = verificationState == OWSVerificationStateVerified;
BOOL isNoLongerVerified = verificationState == OWSVerificationStateNoLongerVerified;
BOOL isBlocked = [helper isRecipientIdBlocked:recipientId];
@ -244,17 +244,22 @@ NS_ASSUME_NONNULL_BEGIN
OWSIdentityManager *identityManger = [OWSIdentityManager sharedManager];
NSArray<NSString *> *recipientIds = [self noLongerVerifiedRecipientIds];
for (NSString *recipientId in recipientIds) {
OWSVerificationState verificationState = [identityManger verificationStateForRecipientId:recipientId];
OWSVerificationState verificationState =
[identityManger verificationStateForRecipientIdWithoutTransaction:recipientId];
if (verificationState == OWSVerificationStateNoLongerVerified) {
NSData *identityKey = [identityManger identityKeyForRecipientIdWOT:recipientId];
if (identityKey.length < 1) {
OWSFail(@"Missing identity key for: %@", recipientId);
continue;
}
[identityManger setVerificationState:OWSVerificationStateDefault
identityKey:identityKey
recipientId:recipientId
isUserInitiatedChange:YES];
[TSStorageManager.protocolStoreDBConnection
asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[identityManger setVerificationState:OWSVerificationStateDefault
identityKey:identityKey
recipientId:recipientId
isUserInitiatedChange:YES
protocolContext:transaction];
}];
}
}
@ -266,7 +271,7 @@ NS_ASSUME_NONNULL_BEGIN
{
NSMutableArray<NSString *> *result = [NSMutableArray new];
for (NSString *recipientId in self.thread.recipientIdentifiers) {
if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId]
if ([[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId]
== OWSVerificationStateNoLongerVerified) {
[result addObject:recipientId];
}

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "NotificationsManager.h"
@ -291,7 +291,8 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
// "no longer verified".
BOOL isNoLongerVerified = NO;
for (NSString *recipientId in thread.recipientIdentifiers) {
if ([OWSIdentityManager.sharedManager verificationStateForRecipientId:recipientId]
if ([OWSIdentityManager.sharedManager
verificationStateForRecipientIdWithoutTransaction:recipientId]
== OWSVerificationStateNoLongerVerified) {
isNoLongerVerified = YES;
break;

View File

@ -385,9 +385,10 @@ typedef void (^SendMessageBlock)(SendCompletionBlock completion);
DDLogDebug(@"%@ Confirming identity for recipient: %@", self.logTag, recipientId);
dispatch_async([OWSDispatch sessionStoreQueue], ^(void) {
[TSStorageManager.protocolStoreDBConnection asyncReadWriteWithBlock:^(
YapDatabaseReadWriteTransaction *transaction) {
OWSVerificationState verificationState =
[[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId];
[[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId transaction:transaction];
switch (verificationState) {
case OWSVerificationStateVerified: {
OWSFail(@"%@ Shouldn't need to confirm identity if it was already verified", self.logTag);
@ -404,12 +405,14 @@ typedef void (^SendMessageBlock)(SendCompletionBlock completion);
}
case OWSVerificationStateNoLongerVerified: {
DDLogInfo(@"%@ marked recipient: %@ as default verification status.", self.logTag, recipientId);
NSData *identityKey = [[OWSIdentityManager sharedManager] identityKeyForRecipientIdWOT:recipientId];
NSData *identityKey = [[OWSIdentityManager sharedManager] identityKeyForRecipientId:recipientId
protocolContext:transaction];
OWSAssert(identityKey);
[[OWSIdentityManager sharedManager] setVerificationState:OWSVerificationStateDefault
identityKey:identityKey
recipientId:recipientId
isUserInitiatedChange:YES];
isUserInitiatedChange:YES
protocolContext:transaction];
break;
}
}
@ -417,7 +420,7 @@ typedef void (^SendMessageBlock)(SendCompletionBlock completion);
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self resendMessage:message fromViewController:fromViewController];
});
});
}];
}
- (void)resendMessage:(TSOutgoingMessage *)message fromViewController:(UIViewController *)fromViewController

View File

@ -1,5 +1,5 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
@ -130,10 +130,10 @@ public class ProfileFetcherJob: NSObject {
}
private func verifyIdentityUpToDateAsync(recipientId: String, latestIdentityKey: Data) {
OWSDispatch.sessionStoreQueue().async {
if OWSIdentityManager.shared().saveRemoteIdentity(latestIdentityKey, recipientId: recipientId) {
TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in
if OWSIdentityManager.shared().saveRemoteIdentity(latestIdentityKey, recipientId: recipientId, protocolContext: transaction) {
Logger.info("\(self.TAG) updated identity key with fetched profile for recipient: \(recipientId)")
self.storageManager.archiveAllSessions(forContact: recipientId)
self.storageManager.archiveAllSessions(forContact: recipientId, protocolContext: transaction)
} else {
// no change in identity.
}

View File

@ -41,7 +41,9 @@ extern const NSUInteger kIdentityKeyLength;
isUserInitiatedChange:(BOOL)isUserInitiatedChange
protocolContext:(nullable id)protocolContext;
- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId;
- (OWSVerificationState)verificationStateForRecipientIdWithoutTransaction:(NSString *)recipientId;
- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (nullable OWSRecipientIdentity *)recipientIdentityForRecipientId:(NSString *)recipientId;
@ -50,8 +52,7 @@ extern const NSUInteger kIdentityKeyLength;
* @returns nil if the recipient does not exist, or is trusted for sending
* else returns the untrusted recipient.
*/
- (nullable OWSRecipientIdentity *)untrustedIdentityForSendingToRecipientId:(NSString *)recipientId
protocolContext:(nullable id)protocolContext;
- (nullable OWSRecipientIdentity *)untrustedIdentityForSendingToRecipientId:(NSString *)recipientId;
// This method can be called from any thread.
- (void)processIncomingSyncMessage:(OWSSignalServiceProtosVerified *)verified;

View File

@ -305,7 +305,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
[self fireIdentityStateChangeNotification];
}
- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId
- (OWSVerificationState)verificationStateForRecipientIdWithoutTransaction:(NSString *)recipientId
{
__block OWSVerificationState result;
// Use a read/write transaction to block on latest.
@ -345,30 +345,31 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
}
- (nullable OWSRecipientIdentity *)untrustedIdentityForSendingToRecipientId:(NSString *)recipientId
protocolContext:(nullable id)protocolContext
{
OWSAssert(recipientId.length > 0);
OWSAssert([protocolContext isKindOfClass:[YapDatabaseReadWriteTransaction class]]);
YapDatabaseReadWriteTransaction *transaction = protocolContext;
__block OWSRecipientIdentity *_Nullable result;
// Use a read/write transaction to block on latest.
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
OWSRecipientIdentity *_Nullable recipientIdentity =
[OWSRecipientIdentity fetchObjectWithUniqueID:recipientId transaction:transaction];
OWSRecipientIdentity *_Nullable recipientIdentity =
[OWSRecipientIdentity fetchObjectWithUniqueID:recipientId transaction:transaction];
if (recipientIdentity == nil) {
// trust on first use
return;
}
if (recipientIdentity == nil) {
// trust on first use
return nil;
}
BOOL isTrusted = [self isTrustedIdentityKey:recipientIdentity.identityKey
recipientId:recipientId
direction:TSMessageDirectionOutgoing
protocolContext:protocolContext];
if (isTrusted) {
return nil;
} else {
return recipientIdentity;
}
BOOL isTrusted = [self isTrustedIdentityKey:recipientIdentity.identityKey
recipientId:recipientId
direction:TSMessageDirectionOutgoing
protocolContext:transaction];
if (isTrusted) {
return;
} else {
result = recipientIdentity;
}
}];
return result;
}
- (void)fireIdentityStateChangeNotification