diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 8a9632a0e..465ccfba4 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -785,6 +785,8 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; // If there were any messages in our local queue which we hadn't yet processed. [[OWSMessageReceiver sharedInstance] handleAnyUnprocessedEnvelopesAsync]; + + [OWSProfileManager.sharedManager syncLocalProfile]; } - (void)ensureRootViewController diff --git a/Signal/src/Profiles/OWSProfileManager.h b/Signal/src/Profiles/OWSProfileManager.h index a54bda298..a300c6d69 100644 --- a/Signal/src/Profiles/OWSProfileManager.h +++ b/Signal/src/Profiles/OWSProfileManager.h @@ -48,6 +48,10 @@ 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)syncLocalProfile; + #pragma mark - Profile Whitelist #ifdef DEBUG diff --git a/Signal/src/Profiles/OWSProfileManager.m b/Signal/src/Profiles/OWSProfileManager.m index 459cf4cbb..12373e90c 100644 --- a/Signal/src/Profiles/OWSProfileManager.m +++ b/Signal/src/Profiles/OWSProfileManager.m @@ -12,6 +12,7 @@ #import #import #import +#import #import #import #import @@ -673,6 +674,17 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; }); } +- (void)syncLocalProfile +{ + OWSAssert([NSThread isMainThread]); + + NSString *_Nullable localNumber = [TSAccountManager sharedInstance].localNumber; + if (!localNumber) { + return; + } + [ProfileFetcherJob runWithRecipientId:localNumber networkManager:self.networkManager ignoreThrottling:YES]; +} + #pragma mark - Profile Whitelist #ifdef DEBUG @@ -1083,6 +1095,17 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; [self saveUserProfile:userProfile]; } + + // If we're updating the profile that corresponds to our local number, + // update the local profile as well. + NSString *_Nullable localNumber = [TSAccountManager sharedInstance].localNumber; + if (localNumber && [localNumber isEqualToString:userProfile.recipientId]) { + UserProfile *localUserProfile = self.localUserProfile; + OWSAssert(localUserProfile); + localUserProfile.avatarFileName = fileName; + [self saveUserProfile:localUserProfile]; + self.localCachedAvatarImage = image; + } } }); }]; @@ -1160,6 +1183,22 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; userProfile.avatarUrlPath = avatarUrlPath; userProfile.avatarFileName = nil; + // If we're updating the profile that corresponds to our local number, + // update the local profile as well. + NSString *_Nullable localNumber = [TSAccountManager sharedInstance].localNumber; + if (localNumber && [localNumber isEqualToString:recipientId]) { + UserProfile *localUserProfile = self.localUserProfile; + OWSAssert(localUserProfile); + localUserProfile.profileName = profileName; + localUserProfile.avatarUrlPath = avatarUrlPath; + // Don't clear avatarFileName and localCachedAvatarImage optimistically. + // * The profile avatar probably isn't out of sync. + // * If the profile avatar is out of sync, it can be synced on next app launch. + // * We don't want to touch local avatar state until we've + // downloaded the latest avatar by downloadAvatarForUserProfile. + [self saveUserProfile:localUserProfile]; + } + if (!isAvatarSame) { // Evacuate avatar image cache. [self.otherUsersProfileAvatarImageCache removeObjectForKey:recipientId];