mirror of https://github.com/oxen-io/session-ios
IdentityKeyStore changes
1) Always accept keys from incoming messages 2) Block sending only if it's a recent change, or if always block is enabled // FREEBIE // FREEBIEpull/1/head
parent
4851e43047
commit
bb25d2beb6
@ -0,0 +1,51 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
@objc
|
||||||
|
class MarkIdentityAsSeenJob: NSObject {
|
||||||
|
let TAG = "[MarkIdentityAsSeenJob]"
|
||||||
|
|
||||||
|
private let thread: TSThread
|
||||||
|
|
||||||
|
public class func run(thread: TSThread) {
|
||||||
|
MarkIdentityAsSeenJob(thread: thread).run()
|
||||||
|
}
|
||||||
|
|
||||||
|
init(thread: TSThread) {
|
||||||
|
self.thread = thread
|
||||||
|
}
|
||||||
|
|
||||||
|
public func run() {
|
||||||
|
switch self.thread {
|
||||||
|
case let contactThread as TSContactThread:
|
||||||
|
markAsSeenIfNecessary(recipientId: contactThread.contactIdentifier())
|
||||||
|
case let groupThread as TSGroupThread:
|
||||||
|
groupThread.groupModel.groupMemberIds?.forEach { memberId in
|
||||||
|
guard let recipientId = memberId as? String else {
|
||||||
|
Logger.error("\(TAG) unexecpted type in group members.")
|
||||||
|
assertionFailure("\(TAG) unexecpted type in group members.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
markAsSeenIfNecessary(recipientId: recipientId)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
assertionFailure("Unexpected thread type: \(self.thread)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func markAsSeenIfNecessary(recipientId: String) {
|
||||||
|
guard let identity = OWSRecipientIdentity.fetch(uniqueId: recipientId) else {
|
||||||
|
Logger.verbose("\(TAG) no existing identity for recipient: \(recipientId). No messages with them yet?")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !identity.wasSeen {
|
||||||
|
Logger.info("\(TAG) marking identity as seen for recipient: \(recipientId)")
|
||||||
|
identity.markAsSeen()
|
||||||
|
identity.save()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "OWSDatabaseMigration.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
@interface OWS104CreateRecipientIdentities : OWSDatabaseMigration
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,77 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "OWS104CreateRecipientIdentities.h"
|
||||||
|
#import <SignalServiceKit/OWSRecipientIdentity.h>
|
||||||
|
#import <SignalServiceKit/TSStorageManager+IdentityKeyStore.h>
|
||||||
|
#import <YapDatabase/YapDatabaseConnection.h>
|
||||||
|
#import <YapDatabase/YapDatabaseTransaction.h>
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
// Increment a similar constant for every future DBMigration
|
||||||
|
static NSString *const OWS104CreateRecipientIdentitiesMigrationId = @"104";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* New SN behavaior requires tracking additional state - not just the identity key data.
|
||||||
|
* So we wrap the key, along with the new meta-data in an OWSRecipientIdentity.
|
||||||
|
*/
|
||||||
|
@implementation OWS104CreateRecipientIdentities
|
||||||
|
|
||||||
|
+ (NSString *)migrationId
|
||||||
|
{
|
||||||
|
return OWS104CreateRecipientIdentitiesMigrationId;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overriding runUp instead of runUpWithTransaction in order to implement a blocking migration.
|
||||||
|
- (void)runUp
|
||||||
|
{
|
||||||
|
[[OWSRecipientIdentity dbConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
||||||
|
NSMutableDictionary<NSString *, NSData *> *identityKeys = [NSMutableDictionary new];
|
||||||
|
|
||||||
|
[transaction
|
||||||
|
enumerateKeysAndObjectsInCollection:TSStorageManagerTrustedKeysCollection
|
||||||
|
usingBlock:^(
|
||||||
|
NSString *_Nonnull recipientId, id _Nonnull object, BOOL *_Nonnull stop) {
|
||||||
|
if (![object isKindOfClass:[NSData class]]) {
|
||||||
|
DDLogError(
|
||||||
|
@"%@ Unexpected object in trusted keys collection key: %@ object: %@",
|
||||||
|
self.tag,
|
||||||
|
recipientId,
|
||||||
|
object);
|
||||||
|
OWSAssert(NO);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
NSData *identityKey = (NSData *)object;
|
||||||
|
[identityKeys setObject:identityKey forKey:recipientId];
|
||||||
|
}];
|
||||||
|
|
||||||
|
[identityKeys enumerateKeysAndObjectsUsingBlock:^(
|
||||||
|
NSString *_Nonnull recipientId, NSData *_Nonnull identityKey, BOOL *_Nonnull stop) {
|
||||||
|
DDLogInfo(@"%@ Migrating identity key for recipient: %@", self.tag, recipientId);
|
||||||
|
[[[OWSRecipientIdentity alloc] initWithRecipientId:recipientId
|
||||||
|
identityKey:identityKey
|
||||||
|
isFirstKnownKey:NO
|
||||||
|
createdAt:[NSDate dateWithTimeIntervalSince1970:0]
|
||||||
|
approvedForBlockingUse:YES
|
||||||
|
approvedForNonBlockingUse:NO] saveWithTransaction:transaction];
|
||||||
|
}];
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - Logging
|
||||||
|
|
||||||
|
+ (NSString *)tag
|
||||||
|
{
|
||||||
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSString *)tag
|
||||||
|
{
|
||||||
|
return self.class.tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
Loading…
Reference in New Issue