Use SignalAccount class to sync contacts.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 741e5c02ac
commit 41e564db47

@ -0,0 +1,51 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
NS_ASSUME_NONNULL_BEGIN
@class Contact;
@class SignalRecipient;
@class YapDatabaseReadTransaction;
// This class represents a single valid Signal account.
//
// * Contacts with multiple signal accounts will correspond to
// multiple instances of SignalAccount.
// * For non-contacts, the contact property will be nil.
//
// New instances of SignalAccount for active accounts are
// created every time we do a contacts intersection (e.g.
// in response to a change to the device contacts).
@interface SignalAccount : NSObject
// An E164 value identifying the signal account.
//
// This is the key property of this class and it
// will always be non-null.
@property (nonatomic, readonly) NSString *recipientId;
// This property is optional and will not be set for
// non-contact account.
@property (nonatomic, nullable) Contact *contact;
@property (nonatomic) BOOL hasMultipleAccountContact;
// For contacts with more than one signal account,
// this is a label for the account.
@property (nonatomic) NSString *multipleAccountLabelText;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithSignalRecipient:(SignalRecipient *)signalRecipient;
- (instancetype)initWithRecipientId:(NSString *)recipientId;
// In most cases this should be non-null. This should only
// be null in the case where the SignalRecipient was
// deleted before this property was accessed.
- (nullable SignalRecipient *)signalRecipientWithTransaction:(YapDatabaseReadTransaction *)transaction;
@end
NS_ASSUME_NONNULL_END

@ -0,0 +1,51 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "SignalAccount.h"
#import "SignalRecipient.h"
#import "TSStorageManager.h"
NS_ASSUME_NONNULL_BEGIN
@interface SignalAccount ()
@property (nonatomic) NSString *recipientId;
@end
#pragma mark -
@implementation SignalAccount
- (instancetype)initWithSignalRecipient:(SignalRecipient *)signalRecipient
{
if (self = [super init]) {
OWSAssert(signalRecipient);
_recipientId = signalRecipient.uniqueId;
}
return self;
}
- (instancetype)initWithRecipientId:(NSString *)recipientId
{
if (self = [super init]) {
OWSAssert(recipientId.length > 0);
_recipientId = recipientId;
}
return self;
}
- (nullable SignalRecipient *)signalRecipientWithTransaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssert([NSThread isMainThread]);
OWSAssert(transaction);
return [SignalRecipient recipientWithTextSecureIdentifier:self.recipientId withTransaction:transaction];
}
@end
NS_ASSUME_NONNULL_END

@ -1,14 +1,16 @@
// Copyright © 2016 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "OWSChunkedOutputStream.h"
NS_ASSUME_NONNULL_BEGIN
@class Contact;
@class SignalAccount;
@interface OWSContactsOutputStream : OWSChunkedOutputStream
- (void)writeContact:(Contact *)contact;
- (void)writeSignalAccount:(SignalAccount *)signalAccount;
@end

@ -1,28 +1,34 @@
// Copyright © 2016 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "OWSContactsOutputStream.h"
#import "Contact.h"
#import "MIMETypeUtil.h"
#import "OWSSignalServiceProtos.pb.h"
#import "SignalAccount.h"
#import <ProtocolBuffers/CodedOutputStream.h>
NS_ASSUME_NONNULL_BEGIN
@implementation OWSContactsOutputStream
- (void)writeContact:(Contact *)contact
- (void)writeSignalAccount:(SignalAccount *)signalAccount
{
OWSAssert(signalAccount);
OWSAssert(signalAccount.contact);
OWSSignalServiceProtosContactDetailsBuilder *contactBuilder = [OWSSignalServiceProtosContactDetailsBuilder new];
[contactBuilder setName:contact.fullName];
[contactBuilder setNumber:contact.textSecureIdentifiers.firstObject];
[contactBuilder setName:signalAccount.contact.fullName];
[contactBuilder setNumber:signalAccount.recipientId];
NSData *avatarPng;
if (contact.image) {
if (signalAccount.contact.image) {
OWSSignalServiceProtosContactDetailsAvatarBuilder *avatarBuilder =
[OWSSignalServiceProtosContactDetailsAvatarBuilder new];
[avatarBuilder setContentType:OWSMimeTypeImagePng];
avatarPng = UIImagePNGRepresentation(contact.image);
avatarPng = UIImagePNGRepresentation(signalAccount.contact.image);
[avatarBuilder setLength:(uint32_t)avatarPng.length];
[contactBuilder setAvatarBuilder:avatarBuilder];
}
@ -33,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
[self.delegateStream writeRawVarint32:contactDataLength];
[self.delegateStream writeRawData:contactData];
if (contact.image) {
if (signalAccount.contact.image) {
[self.delegateStream writeRawData:avatarPng];
}
}

@ -8,6 +8,7 @@
#import "NSDate+millisecondTimeStamp.h"
#import "OWSContactsOutputStream.h"
#import "OWSSignalServiceProtos.pb.h"
#import "SignalAccount.h"
#import "TSAttachment.h"
#import "TSAttachmentStream.h"
@ -63,8 +64,8 @@ NS_ASSUME_NONNULL_BEGIN
[dataOutputStream open];
OWSContactsOutputStream *contactsOutputStream = [OWSContactsOutputStream streamWithOutputStream:dataOutputStream];
for (Contact *contact in self.contactsManager.signalContacts) {
[contactsOutputStream writeContact:contact];
for (SignalAccount *signalAccount in self.contactsManager.signalAccounts) {
[contactsOutputStream writeSignalAccount:signalAccount];
}
[contactsOutputStream flush];

@ -1,14 +1,16 @@
// Created by Frederic Jacobs on 05/12/15.
// Copyright © 2016 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
@class PhoneNumber;
@class Contact;
@class PhoneNumber;
@class SignalAccount;
@class UIImage;
@protocol ContactsManagerProtocol <NSObject>
- (NSString * _Nonnull)displayNameForPhoneIdentifier:(NSString * _Nullable)phoneNumber;
- (NSArray<Contact *> * _Nonnull)signalContacts;
- (NSArray<SignalAccount *> * _Nonnull)signalAccounts;
#if TARGET_OS_IPHONE
- (UIImage * _Nullable)imageForPhoneIdentifier:(NSString * _Nullable)phoneNumber;

Loading…
Cancel
Save