From 93224428819b89caeddc019489b369a4a4612881 Mon Sep 17 00:00:00 2001
From: Matthew Chen <matthew@signal.org>
Date: Thu, 18 Oct 2018 11:04:17 -0400
Subject: [PATCH] Don't assume all linked devices support UD.

---
 SignalMessaging/profiles/OWSProfileManager.h        |  4 ----
 SignalServiceKit/src/Account/TSAccountManager.m     | 13 +++++++++++++
 SignalServiceKit/src/Messages/UD/OWSUDManager.swift |  8 --------
 .../src/Protocols/ProfileManagerProtocol.h          |  6 ++++--
 4 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/SignalMessaging/profiles/OWSProfileManager.h b/SignalMessaging/profiles/OWSProfileManager.h
index e0846acf6..9e917cc6d 100644
--- a/SignalMessaging/profiles/OWSProfileManager.h
+++ b/SignalMessaging/profiles/OWSProfileManager.h
@@ -51,10 +51,6 @@ extern const NSUInteger kOWSProfileManager_MaxAvatarDiameter;
 
 - (BOOL)isProfileNameTooLong:(nullable NSString *)profileName;
 
-// The local profile state can fall out of sync with the service
-// (e.g. due to a botched profile update, for example).
-- (void)fetchLocalUsersProfile;
-
 #pragma mark - Profile Whitelist
 
 // These methods are for debugging.
diff --git a/SignalServiceKit/src/Account/TSAccountManager.m b/SignalServiceKit/src/Account/TSAccountManager.m
index 3cb85c82e..b834085de 100644
--- a/SignalServiceKit/src/Account/TSAccountManager.m
+++ b/SignalServiceKit/src/Account/TSAccountManager.m
@@ -10,6 +10,7 @@
 #import "OWSError.h"
 #import "OWSPrimaryStorage+SessionStore.h"
 #import "OWSRequestFactory.h"
+#import "ProfileManagerProtocol.h"
 #import "SSKEnvironment.h"
 #import "TSNetworkManager.h"
 #import "TSPreKeyManager.h"
@@ -117,6 +118,12 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
     return SSKEnvironment.shared.networkManager;
 }
 
+- (id<ProfileManagerProtocol>)profileManager {
+    OWSAssertDebug(SSKEnvironment.shared.profileManager);
+
+    return SSKEnvironment.shared.profileManager;
+}
+
 #pragma mark -
 
 - (void)setPhoneNumberAwaitingVerification:(NSString *_Nullable)phoneNumberAwaitingVerification
@@ -661,6 +668,12 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa
                                    inCollection:TSAccountManager_UserAccountCollection];
             }
         }];
+
+        // Fetch the local profile, as we may have changed its
+        // account attributes.  Specifically, we need to determine
+        // if all devices for our account now support UD for sync
+        // messages.
+        [self.profileManager fetchLocalUsersProfile];
     });
     [promise retainUntilComplete];
     return promise;
diff --git a/SignalServiceKit/src/Messages/UD/OWSUDManager.swift b/SignalServiceKit/src/Messages/UD/OWSUDManager.swift
index 06dbb54aa..cb7dc7ce7 100644
--- a/SignalServiceKit/src/Messages/UD/OWSUDManager.swift
+++ b/SignalServiceKit/src/Messages/UD/OWSUDManager.swift
@@ -152,14 +152,6 @@ public class OWSUDManagerImpl: NSObject, OWSUDManager {
 
     @objc
     func unidentifiedAccessMode(recipientId: RecipientIdentifier) -> UnidentifiedAccessMode {
-        if tsAccountManager.localNumber() == recipientId {
-            if shouldAllowUnrestrictedAccessLocal() {
-                return .unrestricted
-            } else {
-                return .enabled
-            }
-        }
-
         guard let existingRawValue = dbConnection.object(forKey: recipientId, inCollection: kUnidentifiedAccessCollection) as? Int else {
             return .unknown
         }
diff --git a/SignalServiceKit/src/Protocols/ProfileManagerProtocol.h b/SignalServiceKit/src/Protocols/ProfileManagerProtocol.h
index 1ab3dda0e..2537676f7 100644
--- a/SignalServiceKit/src/Protocols/ProfileManagerProtocol.h
+++ b/SignalServiceKit/src/Protocols/ProfileManagerProtocol.h
@@ -1,9 +1,9 @@
 //
-//  Copyright (c) 2017 Open Whisper Systems. All rights reserved.
+//  Copyright (c) 2018 Open Whisper Systems. All rights reserved.
 //
 
-@class TSThread;
 @class OWSAES256Key;
+@class TSThread;
 
 NS_ASSUME_NONNULL_BEGIN
 
@@ -21,6 +21,8 @@ NS_ASSUME_NONNULL_BEGIN
 - (void)addUserToProfileWhitelist:(NSString *)recipientId;
 - (void)addGroupIdToProfileWhitelist:(NSData *)groupId;
 
+- (void)fetchLocalUsersProfile;
+
 @end
 
 NS_ASSUME_NONNULL_END