Signal-iOS/src/Contacts/SignalRecipient.m
2017-04-11 17:23:20 -04:00

89 lines
2.2 KiB
Objective-C

//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "SignalRecipient.h"
#import "TSStorageHeaders.h"
#import "TSStorageManager+IdentityKeyStore.h"
NS_ASSUME_NONNULL_BEGIN
@implementation SignalRecipient
+ (NSString *)collection {
return @"SignalRecipient";
}
- (instancetype)initWithTextSecureIdentifier:(NSString *)textSecureIdentifier
relay:(nullable NSString *)relay
{
self = [super initWithUniqueId:textSecureIdentifier];
if (!self) {
return self;
}
_devices = [NSMutableOrderedSet orderedSetWithObject:[NSNumber numberWithInt:1]];
_relay = [relay isEqualToString:@""] ? nil : relay;
return self;
}
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier
withTransaction:(YapDatabaseReadTransaction *)transaction
{
return [self fetchObjectWithUniqueID:textSecureIdentifier transaction:transaction];
}
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier
{
__block SignalRecipient *recipient;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
recipient = [self recipientWithTextSecureIdentifier:textSecureIdentifier withTransaction:transaction];
}];
return recipient;
}
+ (instancetype)selfRecipient
{
SignalRecipient *myself = [self recipientWithTextSecureIdentifier:[TSStorageManager localNumber]];
if (!myself) {
myself = [[self alloc] initWithTextSecureIdentifier:[TSStorageManager localNumber]
relay:nil];
}
return myself;
}
- (NSMutableOrderedSet *)devices {
return [_devices copy];
}
- (void)addDevices:(NSSet *)set {
[self checkDevices];
[_devices unionSet:set];
}
- (void)removeDevices:(NSSet *)set {
[self checkDevices];
[_devices minusSet:set];
}
- (void)checkDevices {
if (_devices == nil || ![_devices isKindOfClass:[NSMutableOrderedSet class]]) {
_devices = [NSMutableOrderedSet orderedSetWithObject:[NSNumber numberWithInt:1]];
}
}
- (BOOL)supportsVoice
{
return YES;
}
- (BOOL)supportsWebRTC
{
return YES;
}
@end
NS_ASSUME_NONNULL_END