Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Update SignalRecipient with “is WebRTC enabled” property from service. #91

Merged
merged 2 commits into from Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Contacts/ContactsUpdater.h
@@ -1,5 +1,6 @@
// Created by Frederic Jacobs on 21/11/15.
// Copyright © 2015 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//

#import "SignalRecipient.h"

Expand All @@ -15,6 +16,7 @@ NS_ASSUME_NONNULL_BEGIN

- (nullable SignalRecipient *)synchronousLookup:(NSString *)identifier error:(NSError **)error;

// This asynchronously updates the SignalRecipient for a given contactId.
- (void)lookupIdentifier:(NSString *)identifier
success:(void (^)(NSSet<NSString *> *matchedIds))success
failure:(void (^)(NSError *error))failure;
Expand Down
23 changes: 8 additions & 15 deletions src/Contacts/ContactsUpdater.m
Expand Up @@ -147,25 +147,18 @@ - (void)contactIntersectionWithSet:(NSSet<NSString *> *)idSet
SignalRecipient *recipient =
[SignalRecipient recipientWithTextSecureIdentifier:identifier withTransaction:transaction];
if (!recipient) {
recipient =
[[SignalRecipient alloc] initWithTextSecureIdentifier:identifier relay:nil supportsVoice:NO];
recipient = [[SignalRecipient alloc] initWithTextSecureIdentifier:identifier
relay:nil
supportsVoice:NO
supportsWebRTC:NO];
}

NSDictionary *attributes = [attributesForIdentifier objectForKey:identifier];

NSString *relay = [attributes objectForKey:@"relay"];
if (relay) {
recipient.relay = relay;
} else {
recipient.relay = nil;
}

BOOL supportsVoice = [[attributes objectForKey:@"voice"] boolValue];
if (supportsVoice) {
recipient.supportsVoice = YES;
} else {
recipient.supportsVoice = NO;
}
recipient.relay = attributes[@"relay"];
recipient.supportsVoice = [attributes[@"voice"] boolValue];
// The key for the "supports WebRTC audio/video" property is "video".
recipient.supportsWebRTC = [attributes[@"video"] boolValue];

[recipient saveWithTransaction:transaction];
}
Expand Down
5 changes: 4 additions & 1 deletion src/Contacts/SignalRecipient.h
Expand Up @@ -9,7 +9,8 @@ NS_ASSUME_NONNULL_BEGIN

- (instancetype)initWithTextSecureIdentifier:(NSString *)textSecureIdentifier
relay:(nullable NSString *)relay
supportsVoice:(BOOL)voiceCapable;
supportsVoice:(BOOL)voiceCapable
supportsWebRTC:(BOOL)supportsWebRTC;

+ (instancetype)selfRecipient;
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier;
Expand All @@ -23,6 +24,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, nullable) NSString *relay;
@property (nonatomic, retain) NSMutableOrderedSet *devices;
@property BOOL supportsVoice;
// This property indicates support for both WebRTC audio and video calls.
@property BOOL supportsWebRTC;

@end

Expand Down
9 changes: 8 additions & 1 deletion src/Contacts/SignalRecipient.m
Expand Up @@ -16,6 +16,7 @@ + (NSString *)collection {
- (instancetype)initWithTextSecureIdentifier:(NSString *)textSecureIdentifier
relay:(nullable NSString *)relay
supportsVoice:(BOOL)voiceCapable
supportsWebRTC:(BOOL)supportsWebRTC
{
self = [super initWithUniqueId:textSecureIdentifier];
if (!self) {
Expand All @@ -25,6 +26,7 @@ - (instancetype)initWithTextSecureIdentifier:(NSString *)textSecureIdentifier
_devices = [NSMutableOrderedSet orderedSetWithObject:[NSNumber numberWithInt:1]];
_relay = [relay isEqualToString:@""] ? nil : relay;
_supportsVoice = voiceCapable;
_supportsWebRTC = supportsWebRTC;

return self;
}
Expand All @@ -48,7 +50,12 @@ + (instancetype)selfRecipient
{
SignalRecipient *myself = [self recipientWithTextSecureIdentifier:[TSStorageManager localNumber]];
if (!myself) {
myself = [[self alloc] initWithTextSecureIdentifier:[TSStorageManager localNumber] relay:nil supportsVoice:YES];
myself = [[self alloc] initWithTextSecureIdentifier:[TSStorageManager localNumber]
relay:nil
supportsVoice:YES
// This property may be inaccurate, but it's fine since this will only be
// sent to the current user's other devices, which will ignore this value.
supportsWebRTC:YES];
}
return myself;
}
Expand Down
16 changes: 12 additions & 4 deletions src/Contacts/Threads/TSContactThread.m
@@ -1,5 +1,6 @@
// Created by Frederic Jacobs on 16/11/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//

#import "TSContactThread.h"
#import "ContactsManagerProtocol.h"
Expand Down Expand Up @@ -32,15 +33,22 @@ + (instancetype)getOrCreateThreadWithContactId:(NSString *)contactId
[SignalRecipient recipientWithTextSecureIdentifier:contactId withTransaction:transaction];

if (!recipient) {
recipient = [[SignalRecipient alloc] initWithTextSecureIdentifier:contactId relay:relay supportsVoice:YES];
// If no recipient record exists for that contactId, create an empty record
// for immediate use, then ask ContactsUpdater to try to update it async.
recipient =
[[SignalRecipient alloc] initWithTextSecureIdentifier:contactId
relay:relay
supportsVoice:YES
// Default to NO; ContactsUpdater will try to update this property.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the contactsUpdater does create a SignalRecipient with the correct supportsWebRTC value, aren't we clobbering it with a NO on line 48? [recipient saveWithTransaction:transaction];

Maybe the save needs to be moved up. Or maybe it can be deleted, since it looks like ContactsUpdater saves the recipient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. Fixed, and added a comment.

Also, per our offline conversation, let's keep thinking about how we can:

  • Avoid using concurrency unless necessary.
  • Avoid concurrency issues when mutating our models.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

Some relevant reading that might shape our solutions https://github.com/YapStudios/YapDatabase/wiki#intermediate

supportsWebRTC:NO];
[recipient saveWithTransaction:transaction];

[[ContactsUpdater sharedUpdater] lookupIdentifier:contactId
success:^(NSSet<NSString *> *matchedIds) {
}
failure:^(NSError *error) {
DDLogWarn(@"Failed to lookup contact with error:%@", error);
}];
[recipient saveWithTransaction:transaction];
}

return [self getOrCreateThreadWithContactId:contactId transaction:transaction];
Expand Down