mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
5.2 KiB
Matlab
158 lines
5.2 KiB
Matlab
8 years ago
|
//
|
||
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "OWSContactsSyncing.h"
|
||
|
#import "OWSContactsManager.h"
|
||
7 years ago
|
#import "OWSProfileManager.h"
|
||
8 years ago
|
#import <SignalServiceKit/DataSource.h>
|
||
8 years ago
|
#import <SignalServiceKit/MIMETypeUtil.h>
|
||
|
#import <SignalServiceKit/OWSMessageSender.h>
|
||
|
#import <SignalServiceKit/OWSSyncContactsMessage.h>
|
||
|
#import <SignalServiceKit/TSAccountManager.h>
|
||
|
#import <SignalServiceKit/TSStorageManager.h>
|
||
|
|
||
8 years ago
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
8 years ago
|
NSString *const kTSStorageManagerOWSContactsSyncingCollection = @"kTSStorageManagerOWSContactsSyncingCollection";
|
||
7 years ago
|
NSString *const kTSStorageManagerOWSContactsSyncingLastMessageKey
|
||
|
= @"kTSStorageManagerOWSContactsSyncingLastMessageKey";
|
||
8 years ago
|
|
||
|
@interface OWSContactsSyncing ()
|
||
|
|
||
8 years ago
|
@property (nonatomic, readonly) dispatch_queue_t serialQueue;
|
||
|
|
||
8 years ago
|
@property (nonatomic, readonly) OWSContactsManager *contactsManager;
|
||
8 years ago
|
@property (nonatomic, readonly) OWSIdentityManager *identityManager;
|
||
8 years ago
|
@property (nonatomic, readonly) OWSMessageSender *messageSender;
|
||
8 years ago
|
@property (nonatomic, readonly) OWSProfileManager *profileManager;
|
||
8 years ago
|
|
||
8 years ago
|
@property (nonatomic) BOOL isRequestInFlight;
|
||
|
|
||
|
@end
|
||
|
|
||
|
@implementation OWSContactsSyncing
|
||
|
|
||
|
- (instancetype)initWithContactsManager:(OWSContactsManager *)contactsManager
|
||
8 years ago
|
identityManager:(OWSIdentityManager *)identityManager
|
||
8 years ago
|
messageSender:(OWSMessageSender *)messageSender
|
||
8 years ago
|
profileManager:(OWSProfileManager *)profileManager
|
||
8 years ago
|
{
|
||
|
self = [super init];
|
||
|
|
||
|
if (!self) {
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
OWSAssert(contactsManager);
|
||
|
OWSAssert(messageSender);
|
||
8 years ago
|
OWSAssert(identityManager);
|
||
8 years ago
|
|
||
|
_contactsManager = contactsManager;
|
||
8 years ago
|
_identityManager = identityManager;
|
||
8 years ago
|
_messageSender = messageSender;
|
||
8 years ago
|
_profileManager = profileManager;
|
||
8 years ago
|
|
||
|
OWSSingletonAssert();
|
||
|
|
||
8 years ago
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
||
|
selector:@selector(signalAccountsDidChange:)
|
||
|
name:OWSContactsManagerSignalAccountsDidChangeNotification
|
||
|
object:nil];
|
||
8 years ago
|
|
||
|
return self;
|
||
|
}
|
||
|
|
||
8 years ago
|
- (void)dealloc
|
||
|
{
|
||
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||
|
}
|
||
|
|
||
|
- (void)signalAccountsDidChange:(id)notification
|
||
|
{
|
||
8 years ago
|
OWSAssert([NSThread isMainThread]);
|
||
|
|
||
|
[self sendSyncContactsMessageIfPossible];
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
#pragma mark - Methods
|
||
|
|
||
|
- (void)sendSyncContactsMessageIfNecessary
|
||
|
{
|
||
8 years ago
|
AssertIsOnMainThread();
|
||
|
|
||
8 years ago
|
if (!self.serialQueue) {
|
||
|
_serialQueue = dispatch_queue_create("org.whispersystems.contacts.syncing", DISPATCH_QUEUE_SERIAL);
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
dispatch_async(self.serialQueue, ^{
|
||
8 years ago
|
|
||
8 years ago
|
if (self.isRequestInFlight) {
|
||
|
// De-bounce. It's okay if we ignore some new changes;
|
||
|
// `sendSyncContactsMessageIfPossible` is called fairly
|
||
|
// often so we'll sync soon.
|
||
|
return;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
OWSSyncContactsMessage *syncContactsMessage =
|
||
|
[[OWSSyncContactsMessage alloc] initWithSignalAccounts:self.contactsManager.signalAccounts
|
||
|
identityManager:self.identityManager
|
||
|
profileManager:self.profileManager];
|
||
8 years ago
|
|
||
8 years ago
|
NSData *messageData = [syncContactsMessage buildPlainTextAttachmentData];
|
||
8 years ago
|
|
||
8 years ago
|
NSData *lastMessageData =
|
||
|
[[TSStorageManager sharedManager] objectForKey:kTSStorageManagerOWSContactsSyncingLastMessageKey
|
||
|
inCollection:kTSStorageManagerOWSContactsSyncingCollection];
|
||
8 years ago
|
|
||
8 years ago
|
if (lastMessageData && [lastMessageData isEqual:messageData]) {
|
||
|
// Ignore redundant contacts sync message.
|
||
|
return;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
self.isRequestInFlight = YES;
|
||
|
|
||
8 years ago
|
DataSource *dataSource =
|
||
8 years ago
|
[DataSourceValue dataSourceWithSyncMessage:[syncContactsMessage buildPlainTextAttachmentData]];
|
||
8 years ago
|
[self.messageSender enqueueTemporaryAttachment:dataSource
|
||
8 years ago
|
contentType:OWSMimeTypeApplicationOctetStream
|
||
|
inMessage:syncContactsMessage
|
||
|
success:^{
|
||
8 years ago
|
DDLogInfo(@"%@ Successfully sent contacts sync message.", self.logTag);
|
||
8 years ago
|
|
||
|
[[TSStorageManager sharedManager] setObject:messageData
|
||
|
forKey:kTSStorageManagerOWSContactsSyncingLastMessageKey
|
||
|
inCollection:kTSStorageManagerOWSContactsSyncingCollection];
|
||
|
|
||
|
dispatch_async(self.serialQueue, ^{
|
||
|
self.isRequestInFlight = NO;
|
||
|
});
|
||
|
}
|
||
|
failure:^(NSError *error) {
|
||
8 years ago
|
DDLogError(@"%@ Failed to send contacts sync message with error: %@", self.logTag, error);
|
||
8 years ago
|
|
||
|
dispatch_async(self.serialQueue, ^{
|
||
|
self.isRequestInFlight = NO;
|
||
|
});
|
||
|
}];
|
||
|
});
|
||
8 years ago
|
}
|
||
|
|
||
|
- (void)sendSyncContactsMessageIfPossible
|
||
|
{
|
||
8 years ago
|
AssertIsOnMainThread();
|
||
8 years ago
|
if (self.contactsManager.signalAccounts.count == 0) {
|
||
8 years ago
|
// Don't bother if the contacts manager has no contacts,
|
||
|
// e.g. if the contacts manager hasn't finished setup.
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
if ([TSAccountManager sharedInstance]) {
|
||
|
[self sendSyncContactsMessageIfNecessary];
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
@end
|
||
8 years ago
|
|
||
|
NS_ASSUME_NONNULL_END
|