2017-01-27 19:06:49 +01:00
|
|
|
//
|
2018-03-02 04:29:59 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-01-27 19:06:49 +01:00
|
|
|
//
|
2015-12-07 03:31:43 +01:00
|
|
|
|
|
|
|
#import "ContactsUpdater.h"
|
2016-08-26 16:01:53 +02:00
|
|
|
#import "OWSError.h"
|
2018-03-05 15:30:58 +01:00
|
|
|
#import "OWSPrimaryStorage.h"
|
2018-03-02 04:29:59 +01:00
|
|
|
#import "OWSRequestFactory.h"
|
2017-11-08 20:04:51 +01:00
|
|
|
#import "PhoneNumber.h"
|
2018-09-17 15:27:58 +02:00
|
|
|
#import "SSKEnvironment.h"
|
2015-12-07 03:31:43 +01:00
|
|
|
#import "TSNetworkManager.h"
|
2018-09-25 19:09:55 +02:00
|
|
|
#import <SignalCoreKit/Cryptography.h>
|
2018-09-21 21:41:10 +02:00
|
|
|
#import <SignalCoreKit/Threading.h>
|
2018-07-18 22:46:46 +02:00
|
|
|
#import <SignalServiceKit/SignalServiceKit-Swift.h>
|
2017-12-19 03:42:50 +01:00
|
|
|
#import <YapDatabase/YapDatabase.h>
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2016-11-07 15:39:32 +01:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2018-07-18 22:46:46 +02:00
|
|
|
@interface ContactsUpdater ()
|
|
|
|
|
|
|
|
@property (nonatomic, readonly) NSOperationQueue *contactIntersectionQueue;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2018-09-17 15:27:58 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
@implementation ContactsUpdater
|
|
|
|
|
|
|
|
+ (instancetype)sharedUpdater {
|
2018-09-17 15:27:58 +02:00
|
|
|
OWSAssertDebug(SSKEnvironment.shared.contactsUpdater);
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2018-09-17 15:27:58 +02:00
|
|
|
return SSKEnvironment.shared.contactsUpdater;
|
|
|
|
}
|
2017-04-01 00:45:46 +02:00
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-07-18 22:46:46 +02:00
|
|
|
_contactIntersectionQueue = [NSOperationQueue new];
|
2018-07-19 21:08:59 +02:00
|
|
|
_contactIntersectionQueue.maxConcurrentOperationCount = 1;
|
2018-09-14 20:02:17 +02:00
|
|
|
_contactIntersectionQueue.name = self.logTag;
|
2018-07-18 22:46:46 +02:00
|
|
|
|
2017-04-01 00:45:46 +02:00
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-03-15 21:37:19 +01:00
|
|
|
- (void)lookupIdentifiers:(NSArray<NSString *> *)identifiers
|
|
|
|
success:(void (^)(NSArray<SignalRecipient *> *recipients))success
|
|
|
|
failure:(void (^)(NSError *error))failure
|
|
|
|
{
|
|
|
|
if (identifiers.count < 1) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"Cannot lookup zero identifiers");
|
2018-07-23 15:33:06 +02:00
|
|
|
DispatchMainThreadSafe(^{
|
2018-07-23 15:30:04 +02:00
|
|
|
failure(
|
|
|
|
OWSErrorWithCodeDescription(OWSErrorCodeInvalidMethodParameters, @"Cannot lookup zero identifiers"));
|
|
|
|
});
|
2017-03-15 21:37:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-04-11 01:50:38 +02:00
|
|
|
|
2017-03-15 21:37:19 +01:00
|
|
|
[self contactIntersectionWithSet:[NSSet setWithArray:identifiers]
|
2018-07-23 15:30:04 +02:00
|
|
|
success:^(NSSet<SignalRecipient *> *recipients) {
|
|
|
|
if (recipients.count == 0) {
|
2018-08-27 18:12:53 +02:00
|
|
|
OWSLogInfo(@"no contacts are Signal users");
|
2018-07-23 15:30:04 +02:00
|
|
|
}
|
2018-07-23 15:33:06 +02:00
|
|
|
DispatchMainThreadSafe(^{
|
2018-07-23 15:30:04 +02:00
|
|
|
success(recipients.allObjects);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
failure:^(NSError *error) {
|
2018-07-23 15:33:06 +02:00
|
|
|
DispatchMainThreadSafe(^{
|
2018-07-23 15:30:04 +02:00
|
|
|
failure(error);
|
|
|
|
});
|
|
|
|
}];
|
2017-03-15 21:37:19 +01:00
|
|
|
}
|
|
|
|
|
2018-07-16 18:30:07 +02:00
|
|
|
- (void)contactIntersectionWithSet:(NSSet<NSString *> *)recipientIdsToLookup
|
|
|
|
success:(void (^)(NSSet<SignalRecipient *> *recipients))success
|
2018-07-18 22:46:46 +02:00
|
|
|
failure:(void (^)(NSError *error))failure
|
|
|
|
{
|
2018-08-18 13:36:43 +02:00
|
|
|
OWSLegacyContactDiscoveryOperation *operation =
|
|
|
|
[[OWSLegacyContactDiscoveryOperation alloc] initWithRecipientIdsToLookup:recipientIdsToLookup.allObjects];
|
2018-07-18 22:46:46 +02:00
|
|
|
|
2018-08-18 13:36:43 +02:00
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
2018-07-18 22:46:46 +02:00
|
|
|
NSArray<NSOperation *> *operationAndDependencies = [operation.dependencies arrayByAddingObject:operation];
|
|
|
|
[self.contactIntersectionQueue addOperations:operationAndDependencies waitUntilFinished:YES];
|
|
|
|
|
|
|
|
if (operation.failingError != nil) {
|
|
|
|
failure(operation.failingError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSSet<NSString *> *registeredRecipientIds = operation.registeredRecipientIds;
|
|
|
|
|
|
|
|
NSMutableSet<SignalRecipient *> *recipients = [NSMutableSet new];
|
|
|
|
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
for (NSString *recipientId in recipientIdsToLookup) {
|
|
|
|
if ([registeredRecipientIds containsObject:recipientId]) {
|
|
|
|
SignalRecipient *recipient =
|
|
|
|
[SignalRecipient markRecipientAsRegisteredAndGet:recipientId transaction:transaction];
|
|
|
|
[recipients addObject:recipient];
|
|
|
|
} else {
|
|
|
|
[SignalRecipient removeUnregisteredRecipient:recipientId transaction:transaction];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2018-07-20 21:34:02 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
success([recipients copy]);
|
|
|
|
});
|
2015-12-07 03:31:43 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2016-11-07 15:39:32 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|