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.
session-ios/SignalServiceKit/src/Contacts/SignalRecipient.m

268 lines
8.7 KiB
Matlab

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
10 years ago
#import "SignalRecipient.h"
#import "OWSDevice.h"
#import "ProfileManagerProtocol.h"
#import "SSKEnvironment.h"
#import "TSAccountManager.h"
#import <SignalServiceKit/SignalServiceKit-Swift.h>
#import <YapDatabase/YapDatabaseConnection.h>
10 years ago
NS_ASSUME_NONNULL_BEGIN
@interface SignalRecipient ()
@property (nonatomic) NSOrderedSet *devices;
@end
#pragma mark -
10 years ago
@implementation SignalRecipient
#pragma mark - Dependencies
- (id<ProfileManagerProtocol>)profileManager
{
return SSKEnvironment.shared.profileManager;
}
- (id<OWSUDManager>)udManager
{
return SSKEnvironment.shared.udManager;
}
#pragma mark -
7 years ago
+ (instancetype)getOrBuildUnsavedRecipientForRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssertDebug(transaction);
OWSAssertDebug(recipientId.length > 0);
SignalRecipient *_Nullable recipient = [self registeredRecipientForRecipientId:recipientId transaction:transaction];
if (!recipient) {
recipient = [[self alloc] initWithTextSecureIdentifier:recipientId];
}
return recipient;
}
10 years ago
- (instancetype)initWithTextSecureIdentifier:(NSString *)textSecureIdentifier
{
10 years ago
self = [super initWithUniqueId:textSecureIdentifier];
if (!self) {
return self;
10 years ago
}
OWSAssertDebug([TSAccountManager localNumber].length > 0);
if ([[TSAccountManager localNumber] isEqualToString:textSecureIdentifier]) {
// Default to no devices.
//
// This instance represents our own account and is used for sending
// sync message to linked devices. We shouldn't have any linked devices
// yet when we create the "self" SignalRecipient, and we don't need to
// send sync messages to the primary - we ARE the primary.
_devices = [NSOrderedSet new];
} else {
// Default to sending to just primary device.
//
// OWSMessageSender will correct this if it is wrong the next time
// we send a message to this recipient.
_devices = [NSOrderedSet orderedSetWithObject:@(OWSDevicePrimaryDeviceId)];
}
10 years ago
return self;
}
- (nullable instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (!self) {
return self;
}
if (_devices == nil) {
_devices = [NSOrderedSet new];
}
if ([self.uniqueId isEqual:[TSAccountManager localNumber]] &&
[self.devices containsObject:@(OWSDevicePrimaryDeviceId)]) {
OWSFailDebug(@"self as recipient device");
}
return self;
}
+ (nullable instancetype)registeredRecipientForRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssertDebug(transaction);
OWSAssertDebug(recipientId.length > 0);
return [self fetchObjectWithUniqueID:recipientId transaction:transaction];
10 years ago
}
- (void)addDevices:(NSSet *)devices
{
OWSAssertDebug(devices.count > 0);
if ([self.uniqueId isEqual:[TSAccountManager localNumber]] &&
[devices containsObject:@(OWSDevicePrimaryDeviceId)]) {
OWSFailDebug(@"adding self as recipient device");
return;
}
10 years ago
7 years ago
NSMutableOrderedSet *updatedDevices = [self.devices mutableCopy];
[updatedDevices unionSet:devices];
self.devices = [updatedDevices copy];
10 years ago
}
- (void)removeDevices:(NSSet *)devices
{
OWSAssertDebug(devices.count > 0);
7 years ago
NSMutableOrderedSet *updatedDevices = [self.devices mutableCopy];
[updatedDevices minusSet:devices];
self.devices = [updatedDevices copy];
10 years ago
}
- (void)updateRegisteredRecipientWithDevicesToAdd:(nullable NSArray *)devicesToAdd
devicesToRemove:(nullable NSArray *)devicesToRemove
transaction:(YapDatabaseReadWriteTransaction *)transaction {
OWSAssertDebug(transaction);
OWSAssertDebug(devicesToAdd.count > 0 || devicesToRemove.count > 0);
// Add before we remove, since removeDevicesFromRecipient:...
// can removeUnregisteredRecipient:... if the recipient has
// no devices left.
if (devicesToAdd.count > 0) {
[self addDevicesToRegisteredRecipient:[NSSet setWithArray:devicesToAdd] transaction:transaction];
}
if (devicesToRemove.count > 0) {
[self removeDevicesFromRecipient:[NSSet setWithArray:devicesToRemove] transaction:transaction];
}
// Device changes
dispatch_async(dispatch_get_main_queue(), ^{
// Device changes can affect the UD access mode for a recipient,
// so we need to fetch the profile for this user to update UD access mode.
[self.profileManager fetchProfileForRecipientId:self.recipientId];
});
}
- (void)addDevicesToRegisteredRecipient:(NSSet *)devices transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssertDebug(transaction);
OWSAssertDebug(devices.count > 0);
OWSLogDebug(@"adding devices: %@, to recipient: %@", devices, self);
[self reloadWithTransaction:transaction];
[self addDevices:devices];
[self saveWithTransaction_internal:transaction];
}
- (void)removeDevicesFromRecipient:(NSSet *)devices transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssertDebug(transaction);
OWSAssertDebug(devices.count > 0);
OWSLogDebug(@"removing devices: %@, from registered recipient: %@", devices, self);
[self reloadWithTransaction:transaction];
[self removeDevices:devices];
if (self.devices.count > 0) {
[self saveWithTransaction_internal:transaction];
} else {
[SignalRecipient removeUnregisteredRecipient:self.recipientId transaction:transaction];
}
}
- (NSString *)recipientId
{
return self.uniqueId;
}
- (NSComparisonResult)compare:(SignalRecipient *)other
{
return [self.recipientId compare:other.recipientId];
}
- (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
7 years ago
{
7 years ago
// We only want to mutate the persisted SignalRecipients in the database
// using other methods of this class, e.g. markRecipientAsRegistered...
// to create, addDevices and removeDevices to mutate. We're trying to
// be strict about using persisted SignalRecipients as a cache to
// reflect "last known registration status". Forcing our codebase to
// use those methods helps ensure that we update the cache deliberately.
OWSFailDebug(@"Don't call saveWithTransaction from outside this class.");
7 years ago
[self saveWithTransaction_internal:transaction];
}
- (void)saveWithTransaction_internal:(YapDatabaseReadWriteTransaction *)transaction
{
[super saveWithTransaction:transaction];
OWSLogVerbose(@"saved signal recipient: %@", self.recipientId);
}
7 years ago
+ (BOOL)isRegisteredRecipient:(NSString *)recipientId transaction:(YapDatabaseReadTransaction *)transaction
{
7 years ago
SignalRecipient *_Nullable instance = [self registeredRecipientForRecipientId:recipientId transaction:transaction];
return instance != nil;
}
7 years ago
+ (SignalRecipient *)markRecipientAsRegisteredAndGet:(NSString *)recipientId
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssertDebug(transaction);
OWSAssertDebug(recipientId.length > 0);
7 years ago
SignalRecipient *_Nullable instance = [self registeredRecipientForRecipientId:recipientId transaction:transaction];
if (!instance) {
OWSLogDebug(@"creating recipient: %@", recipientId);
instance = [[self alloc] initWithTextSecureIdentifier:recipientId];
7 years ago
[instance saveWithTransaction_internal:transaction];
}
return instance;
}
7 years ago
+ (void)markRecipientAsRegistered:(NSString *)recipientId
deviceId:(UInt32)deviceId
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssertDebug(transaction);
OWSAssertDebug(recipientId.length > 0);
7 years ago
SignalRecipient *recipient = [self markRecipientAsRegisteredAndGet:recipientId transaction:transaction];
if (![recipient.devices containsObject:@(deviceId)]) {
OWSLogDebug(@"Adding device %u to existing recipient.", (unsigned int)deviceId);
[recipient addDevices:[NSSet setWithObject:@(deviceId)]];
7 years ago
[recipient saveWithTransaction_internal:transaction];
}
}
7 years ago
+ (void)removeUnregisteredRecipient:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssertDebug(transaction);
OWSAssertDebug(recipientId.length > 0);
7 years ago
SignalRecipient *_Nullable instance = [self registeredRecipientForRecipientId:recipientId transaction:transaction];
if (!instance) {
return;
}
OWSLogDebug(@"removing recipient: %@", recipientId);
[instance removeWithTransaction:transaction];
}
10 years ago
@end
NS_ASSUME_NONNULL_END