2017-04-13 18:54:03 +02:00
|
|
|
//
|
2018-02-07 18:44:09 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-04-13 18:54:03 +02:00
|
|
|
//
|
2016-08-26 01:01:35 +02:00
|
|
|
|
|
|
|
#import "OWSSyncContactsMessage.h"
|
|
|
|
#import "Contact.h"
|
2016-08-27 00:07:54 +02:00
|
|
|
#import "ContactsManagerProtocol.h"
|
2016-08-29 02:42:07 +02:00
|
|
|
#import "OWSContactsOutputStream.h"
|
2017-06-22 03:04:16 +02:00
|
|
|
#import "OWSIdentityManager.h"
|
2017-09-22 16:30:35 +02:00
|
|
|
#import "ProfileManagerProtocol.h"
|
2019-11-29 03:21:09 +01:00
|
|
|
#import "ProtoUtils.h"
|
2018-08-31 19:12:40 +02:00
|
|
|
#import "SSKEnvironment.h"
|
2017-05-05 18:32:36 +02:00
|
|
|
#import "SignalAccount.h"
|
2018-10-05 21:56:21 +02:00
|
|
|
#import "TSAccountManager.h"
|
2016-08-26 01:01:35 +02:00
|
|
|
#import "TSAttachment.h"
|
|
|
|
#import "TSAttachmentStream.h"
|
2018-07-04 03:05:00 +02:00
|
|
|
#import "TSContactThread.h"
|
2020-06-05 02:38:44 +02:00
|
|
|
#import <SessionCoreKit/NSDate+OWS.h>
|
2020-06-05 05:43:06 +02:00
|
|
|
#import <SessionServiceKit/SessionServiceKit-Swift.h>
|
2019-11-11 05:52:17 +01:00
|
|
|
#import "OWSPrimaryStorage.h"
|
2016-08-26 01:01:35 +02:00
|
|
|
|
2018-10-05 21:56:21 +02:00
|
|
|
@import Contacts;
|
|
|
|
|
2016-08-27 00:07:54 +02:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2016-08-26 01:01:35 +02:00
|
|
|
@interface OWSSyncContactsMessage ()
|
|
|
|
|
2017-08-26 23:45:05 +02:00
|
|
|
@property (nonatomic, readonly) NSArray<SignalAccount *> *signalAccounts;
|
2017-06-22 03:04:16 +02:00
|
|
|
@property (nonatomic, readonly) OWSIdentityManager *identityManager;
|
2017-08-25 23:16:49 +02:00
|
|
|
@property (nonatomic, readonly) id<ProfileManagerProtocol> profileManager;
|
2016-08-26 01:01:35 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation OWSSyncContactsMessage
|
|
|
|
|
2017-08-26 23:45:05 +02:00
|
|
|
- (instancetype)initWithSignalAccounts:(NSArray<SignalAccount *> *)signalAccounts
|
|
|
|
identityManager:(OWSIdentityManager *)identityManager
|
|
|
|
profileManager:(id<ProfileManagerProtocol>)profileManager
|
2016-08-26 01:01:35 +02:00
|
|
|
{
|
2018-05-11 16:36:40 +02:00
|
|
|
self = [super init];
|
2016-08-26 01:01:35 +02:00
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-08-26 23:45:05 +02:00
|
|
|
_signalAccounts = signalAccounts;
|
2017-06-22 03:04:16 +02:00
|
|
|
_identityManager = identityManager;
|
2017-08-25 23:16:49 +02:00
|
|
|
_profileManager = profileManager;
|
2016-08-26 01:01:35 +02:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-06-07 17:52:39 +02:00
|
|
|
- (nullable instancetype)initWithCoder:(NSCoder *)coder
|
|
|
|
{
|
|
|
|
return [super initWithCoder:coder];
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:56:21 +02:00
|
|
|
#pragma mark - Dependencies
|
|
|
|
|
|
|
|
- (id<ContactsManagerProtocol>)contactsManager {
|
|
|
|
return SSKEnvironment.shared.contactsManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (TSAccountManager *)tsAccountManager {
|
|
|
|
return TSAccountManager.sharedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2018-08-02 00:00:58 +02:00
|
|
|
- (nullable SSKProtoSyncMessageBuilder *)syncMessageBuilder
|
2016-08-26 01:01:35 +02:00
|
|
|
{
|
2019-11-12 02:11:29 +01:00
|
|
|
NSError *error;
|
2019-11-11 05:52:17 +01:00
|
|
|
if (self.attachmentIds.count > 1) {
|
|
|
|
OWSLogError(@"Expected sync contact message to have one or zero attachments, but found %lu.", (unsigned long)self.attachmentIds.count);
|
2016-08-26 01:01:35 +02:00
|
|
|
}
|
2016-09-03 01:44:51 +02:00
|
|
|
|
2019-11-11 05:52:17 +01:00
|
|
|
SSKProtoSyncMessageContactsBuilder *contactsBuilder;
|
|
|
|
if (self.attachmentIds.count == 0) {
|
2019-11-12 02:11:29 +01:00
|
|
|
SSKProtoAttachmentPointerBuilder *attachmentProtoBuilder = [SSKProtoAttachmentPointer builderWithId:0];
|
|
|
|
SSKProtoAttachmentPointer *attachmentProto = [attachmentProtoBuilder buildAndReturnError:&error];
|
2019-11-25 00:43:04 +01:00
|
|
|
contactsBuilder = [SSKProtoSyncMessageContacts builder];
|
|
|
|
[contactsBuilder setBlob:attachmentProto];
|
2019-11-11 05:52:17 +01:00
|
|
|
__block NSData *data;
|
|
|
|
[OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
data = [self buildPlainTextAttachmentDataWithTransaction:transaction];
|
|
|
|
}];
|
|
|
|
[contactsBuilder setData:data];
|
|
|
|
} else {
|
|
|
|
SSKProtoAttachmentPointer *attachmentProto = [TSAttachmentStream buildProtoForAttachmentId:self.attachmentIds.firstObject];
|
|
|
|
if (attachmentProto == nil) {
|
|
|
|
OWSFailDebug(@"Couldn't build protobuf.");
|
|
|
|
return nil;
|
|
|
|
}
|
2019-11-25 00:43:04 +01:00
|
|
|
contactsBuilder = [SSKProtoSyncMessageContacts builder];
|
|
|
|
[contactsBuilder setBlob:attachmentProto];
|
2018-08-02 19:29:24 +02:00
|
|
|
}
|
2017-05-05 18:36:48 +02:00
|
|
|
[contactsBuilder setIsComplete:YES];
|
2019-11-11 05:52:17 +01:00
|
|
|
|
|
|
|
SSKProtoSyncMessageContacts *contactsProto = [contactsBuilder buildAndReturnError:&error];
|
|
|
|
if (error || contactsProto == nil) {
|
|
|
|
OWSFailDebug(@"Couldn't build protobuf due to error: %@.", error);
|
2018-08-02 00:00:58 +02:00
|
|
|
return nil;
|
|
|
|
}
|
2018-09-24 15:26:08 +02:00
|
|
|
SSKProtoSyncMessageBuilder *syncMessageBuilder = [SSKProtoSyncMessage builder];
|
2018-08-02 00:00:58 +02:00
|
|
|
[syncMessageBuilder setContacts:contactsProto];
|
2019-11-27 06:26:15 +01:00
|
|
|
|
2017-06-22 19:05:28 +02:00
|
|
|
return syncMessageBuilder;
|
2016-08-26 01:01:35 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 18:57:48 +02:00
|
|
|
- (nullable NSData *)buildPlainTextAttachmentDataWithTransaction:(YapDatabaseReadTransaction *)transaction
|
2016-08-26 01:01:35 +02:00
|
|
|
{
|
2018-10-05 21:56:21 +02:00
|
|
|
NSMutableArray<SignalAccount *> *signalAccounts = [self.signalAccounts mutableCopy];
|
2018-06-20 17:41:14 +02:00
|
|
|
|
2016-08-29 02:42:07 +02:00
|
|
|
// TODO use temp file stream to avoid loading everything into memory at once
|
|
|
|
// First though, we need to re-engineer our attachment process to accept streams (encrypting with stream,
|
|
|
|
// and uploading with streams).
|
|
|
|
NSOutputStream *dataOutputStream = [NSOutputStream outputStreamToMemory];
|
|
|
|
[dataOutputStream open];
|
2018-08-07 18:57:48 +02:00
|
|
|
OWSContactsOutputStream *contactsOutputStream =
|
2018-10-10 15:59:10 +02:00
|
|
|
[[OWSContactsOutputStream alloc] initWithOutputStream:dataOutputStream];
|
2016-08-26 01:01:35 +02:00
|
|
|
|
2018-10-05 21:56:21 +02:00
|
|
|
for (SignalAccount *signalAccount in signalAccounts) {
|
2017-08-26 23:45:05 +02:00
|
|
|
OWSRecipientIdentity *_Nullable recipientIdentity =
|
|
|
|
[self.identityManager recipientIdentityForRecipientId:signalAccount.recipientId];
|
2017-08-25 23:16:49 +02:00
|
|
|
NSData *_Nullable profileKeyData = [self.profileManager profileKeyDataForRecipientId:signalAccount.recipientId];
|
2018-06-20 17:41:14 +02:00
|
|
|
|
2018-07-06 00:56:14 +02:00
|
|
|
OWSDisappearingMessagesConfiguration *_Nullable disappearingMessagesConfiguration;
|
|
|
|
NSString *conversationColorName;
|
|
|
|
|
|
|
|
TSContactThread *_Nullable contactThread = [TSContactThread getThreadWithContactId:signalAccount.recipientId transaction:transaction];
|
|
|
|
if (contactThread) {
|
|
|
|
conversationColorName = contactThread.conversationColorName;
|
|
|
|
disappearingMessagesConfiguration = [contactThread disappearingMessagesConfigurationWithTransaction:transaction];
|
|
|
|
} else {
|
2018-10-04 18:03:33 +02:00
|
|
|
conversationColorName = [TSThread stableColorNameForNewConversationWithString:signalAccount.recipientId];
|
2018-07-06 00:56:14 +02:00
|
|
|
}
|
2018-07-06 00:40:08 +02:00
|
|
|
|
2017-08-25 23:16:49 +02:00
|
|
|
[contactsOutputStream writeSignalAccount:signalAccount
|
|
|
|
recipientIdentity:recipientIdentity
|
2018-06-20 17:41:14 +02:00
|
|
|
profileKeyData:profileKeyData
|
2018-10-05 21:56:21 +02:00
|
|
|
contactsManager:self.contactsManager
|
2018-07-06 00:56:14 +02:00
|
|
|
conversationColorName:conversationColorName
|
|
|
|
disappearingMessagesConfiguration:disappearingMessagesConfiguration];
|
2016-08-29 02:42:07 +02:00
|
|
|
}
|
2018-10-05 21:56:21 +02:00
|
|
|
|
2016-08-29 02:42:07 +02:00
|
|
|
[dataOutputStream close];
|
2016-08-26 01:01:35 +02:00
|
|
|
|
2018-08-07 18:57:48 +02:00
|
|
|
if (contactsOutputStream.hasError) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"Could not write contacts sync stream.");
|
2018-08-07 18:57:48 +02:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2016-08-29 02:42:07 +02:00
|
|
|
return [dataOutputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
|
2016-08-26 01:01:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2016-08-27 00:07:54 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|