diff --git a/SignalServiceKit/src/Contacts/SignalAccount.h b/SignalServiceKit/src/Contacts/SignalAccount.h index 8e4fc2668e..381ace4fde 100644 --- a/SignalServiceKit/src/Contacts/SignalAccount.h +++ b/SignalServiceKit/src/Contacts/SignalAccount.h @@ -8,6 +8,7 @@ NS_ASSUME_NONNULL_BEGIN @class Contact; @class SignalRecipient; +@class SignalServiceAddress; @class YapDatabaseReadTransaction; // This class represents a single valid Signal account. @@ -17,11 +18,15 @@ NS_ASSUME_NONNULL_BEGIN // * For non-contacts, the contact property will be nil. @interface SignalAccount : TSYapDatabaseObject -// 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; +/// An E164 value identifying the signal account. +@property (nullable, nonatomic, readonly) NSString *recipientPhoneNumber; + +/// A UUID identifying the signal account. +@property (nullable, nonatomic, readonly) NSString *recipientUUID; + +/// An address representing the signal account. This will be +/// the UUID, if defined, otherwise it will be the E164 number. +@property (nonatomic, readonly) SignalServiceAddress *recipientAddress; // This property is optional and will not be set for // non-contact account. @@ -39,7 +44,7 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithSignalRecipient:(SignalRecipient *)signalRecipient; -- (instancetype)initWithRecipientId:(NSString *)recipientId; +- (instancetype)initWithSignalServiceAddress:(SignalServiceAddress *)address; // --- CODE GENERATION MARKER diff --git a/SignalServiceKit/src/Contacts/SignalAccount.m b/SignalServiceKit/src/Contacts/SignalAccount.m index 952c27c7c8..9c73b4b115 100644 --- a/SignalServiceKit/src/Contacts/SignalAccount.m +++ b/SignalServiceKit/src/Contacts/SignalAccount.m @@ -7,13 +7,14 @@ #import "NSString+SSK.h" #import "OWSPrimaryStorage.h" #import "SignalRecipient.h" +#import NS_ASSUME_NONNULL_BEGIN +NSUInteger const SignalAccountSchemaVersion = 1; + @interface SignalAccount () - -@property (nonatomic) NSString *recipientId; - +@property (nonatomic, readonly) NSUInteger accountSchemaVersion; @end #pragma mark - @@ -26,16 +27,41 @@ NS_ASSUME_NONNULL_BEGIN return [self initWithRecipientId:signalRecipient.recipientId]; } -- (instancetype)initWithRecipientId:(NSString *)recipientId +- (instancetype)initWithSignalServiceAddress:(SignalServiceAddress *)serviceAddress { if (self = [super init]) { - OWSAssertDebug(recipientId.length > 0); + if (serviceAddress.isUUID) { + _recipientUUID = serviceAddress.stringIdentifier; + } else { + _recipientPhoneNumber = serviceAddress.stringIdentifier; + } - _recipientId = recipientId; + _accountSchemaVersion = SignalAccountSchemaVersion; } return self; } +- (nullable instancetype)initWithCoder:(NSCoder *)coder +{ + self = [super initWithCoder:coder]; + if (!self) { + return self; + } + + // Migrating from an everyone has a phone number world to a + // world in which we have UUIDs + if (_accountSchemaVersion == 0) { + // Rename recipientId to recipientPhoneNumber + _recipientPhoneNumber = [coder decodeObjectForKey:@"recipientId"]; + + OWSAssert(_recipientPhoneNumber != nil); + } + + _accountSchemaVersion = SignalAccountSchemaVersion; + + return self; +} + // --- CODE GENERATION MARKER // This snippet is generated by /Scripts/sds_codegen/sds_generate.py. Do not manually edit it, instead run @@ -67,11 +93,6 @@ NS_ASSUME_NONNULL_BEGIN // --- CODE GENERATION MARKER -- (nullable NSString *)uniqueId -{ - return _recipientId; -} - - (nullable NSString *)contactFullName { return self.contact.fullName.filterStringForDisplay; @@ -82,6 +103,17 @@ NS_ASSUME_NONNULL_BEGIN return _multipleAccountLabelText.filterStringForDisplay; } +- (SignalServiceAddress *)recipientAddress +{ + if (self.recipientUUID != nil) { + return [[SignalServiceAddress alloc] initWithUuidString:self.recipientUUID]; + } else if (self.recipientPhoneNumber != nil) { + return [[SignalServiceAddress alloc] initWithPhoneNumber:self.recipientPhoneNumber]; + } else { + OWSFail(@"unexpectedly have no address for SignalAccount, this should never happen."); + } +} + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Contacts/SignalServiceAddress.swift b/SignalServiceKit/src/Contacts/SignalServiceAddress.swift index 7e28699a3e..6b4cfc0b24 100644 --- a/SignalServiceKit/src/Contacts/SignalServiceAddress.swift +++ b/SignalServiceKit/src/Contacts/SignalServiceAddress.swift @@ -14,23 +14,60 @@ public class SignalServiceAddress: NSObject { private let backingAddress: BackingStorage @objc - public init(phoneNumber: String) { - self.backingAddress = .phoneNumber(phoneNumber) + public init?(phoneNumber: String) { + guard !phoneNumber.isEmpty else { + owsFailDebug("Unexpectedly initialized signal service address with invalid phone number") + return nil + } + backingAddress = .phoneNumber(phoneNumber) } @objc public init(uuid: UUID) { - self.backingAddress = .uuid(uuid) + backingAddress = .uuid(uuid) + } + + @objc + public init?(uuidString: String) { + guard let uuid = UUID(uuidString: uuidString) else { + owsFailDebug("Tried to intialize signal service address with invalid UUID") + return nil + } + backingAddress = .uuid(uuid) + } + + @objc + public var stringIdentifier: String { + switch backingAddress { + case .phoneNumber(let phoneNumber): + return phoneNumber + case .uuid(let uuid): + return uuid.uuidString + } + } + + @objc + public var isUUID: Bool { + guard case .uuid = backingAddress else { + return false + } + return true + } + + @objc + public var isPhoneNumber: Bool { + guard case .phoneNumber = backingAddress else { + return false + } + return true } @objc public var transitional_phoneNumber: String! { - switch backingAddress { - case .phoneNumber(let phoneNumber): - return phoneNumber - case .uuid: + guard case .phoneNumber(let phoneNumber) = backingAddress else { owsFailDebug("transitional_phoneNumber was unexpectedly nil") return nil } + return phoneNumber } } diff --git a/SignalServiceKit/src/Storage/Database/SSKAccessors+SDS.h b/SignalServiceKit/src/Storage/Database/SSKAccessors+SDS.h index 332807026e..966cac80f0 100644 --- a/SignalServiceKit/src/Storage/Database/SSKAccessors+SDS.h +++ b/SignalServiceKit/src/Storage/Database/SSKAccessors+SDS.h @@ -152,4 +152,12 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - +@interface SignalAccount (SDS) + +@property (nonatomic, readonly) NSUInteger accountSchemaVersion; + +@end + +#pragma mark - + NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Storage/FullTextSearchFinder.swift b/SignalServiceKit/src/Storage/FullTextSearchFinder.swift index 2c88de845d..c7a20b4245 100644 --- a/SignalServiceKit/src/Storage/FullTextSearchFinder.swift +++ b/SignalServiceKit/src/Storage/FullTextSearchFinder.swift @@ -229,7 +229,7 @@ public class FullTextSearchFinder: NSObject { } return self.messageIndexer.index(message, transaction: transaction) } else if let signalAccount = object as? SignalAccount { - return self.recipientIndexer.index(signalAccount.recipientId, transaction: transaction) + return self.recipientIndexer.index(signalAccount.recipientAddress.transitional_phoneNumber, transaction: transaction) } else { return nil }