session-ios/SignalServiceKit/src/Contacts/ContactsUpdater.m

120 lines
3.8 KiB
Mathematica
Raw Normal View History

//
2018-03-02 04:29:59 +01:00
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
2015-12-07 03:31:43 +01:00
#import "ContactsUpdater.h"
#import "OWSError.h"
#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>
#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
NS_ASSUME_NONNULL_BEGIN
@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;
}
- (instancetype)init
{
self = [super init];
if (!self) {
return self;
}
_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;
OWSSingletonAssert();
return self;
}
- (void)lookupIdentifiers:(NSArray<NSString *> *)identifiers
success:(void (^)(NSArray<SignalRecipient *> *recipients))success
failure:(void (^)(NSError *error))failure
{
if (identifiers.count < 1) {
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"));
});
return;
}
[self contactIntersectionWithSet:[NSSet setWithArray:identifiers]
2018-07-23 15:30:04 +02:00
success:^(NSSet<SignalRecipient *> *recipients) {
if (recipients.count == 0) {
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);
});
}];
}
2018-07-16 18:30:07 +02:00
- (void)contactIntersectionWithSet:(NSSet<NSString *> *)recipientIdsToLookup
success:(void (^)(NSSet<SignalRecipient *> *recipients))success
failure:(void (^)(NSError *error))failure
{
OWSLegacyContactDiscoveryOperation *operation =
[[OWSLegacyContactDiscoveryOperation alloc] initWithRecipientIdsToLookup:recipientIdsToLookup.allObjects];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
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
NS_ASSUME_NONNULL_END