diff --git a/SignalMessaging/profiles/OWSProfileManager.m b/SignalMessaging/profiles/OWSProfileManager.m index 90c280a52..47e91fcb4 100644 --- a/SignalMessaging/profiles/OWSProfileManager.m +++ b/SignalMessaging/profiles/OWSProfileManager.m @@ -785,12 +785,9 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; return nil; } -- (void)downloadAvatarForUserProfile:(OWSUserProfile *)userProfileParameter +- (void)downloadAvatarForUserProfile:(OWSUserProfile *)userProfile { - OWSAssert(userProfileParameter); - - // Make a local copy. - OWSUserProfile *userProfile = [userProfileParameter copy]; + OWSAssert(userProfile); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (userProfile.avatarUrlPath.length < 1) { @@ -915,14 +912,25 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; return; } + // If we're transitioning from (no avatar -> no avatar) or from (same avatar -> same avatar), + // don't bother updating the avatar. + BOOL canSkipAvatarUpdate = ((avatarUrlPath.length == 0 && userProfile.avatarUrlPath.length == 0 + && userProfile.avatarFileName.length == 0) + || (avatarUrlPath.length > 0 && userProfile.avatarUrlPath.length > 0 && + [avatarUrlPath isEqualToString:userProfile.avatarUrlPath] && userProfile.avatarFileName)); + NSString *_Nullable profileName = [self decryptProfileNameData:profileNameEncrypted profileKey:userProfile.profileKey]; - [userProfile updateWithProfileName:profileName - avatarUrlPath:avatarUrlPath - avatarFileName:userProfile.avatarFileName // use existing file name if already downloaded - dbConnection:self.dbConnection - completion:nil]; + if (canSkipAvatarUpdate) { + [userProfile updateWithProfileName:profileName dbConnection:self.dbConnection completion:nil]; + } else { + [userProfile updateWithProfileName:profileName + avatarUrlPath:avatarUrlPath + avatarFileName:nil + dbConnection:self.dbConnection + completion:nil]; + } // If we're updating the profile that corresponds to our local number, // update the local profile as well. @@ -942,7 +950,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; completion:nil]; } - if (userProfile.avatarUrlPath.length > 0 && userProfile.avatarFileName.length == 0) { + if (avatarUrlPath.length > 0) { [self downloadAvatarForUserProfile:userProfile]; } }); diff --git a/SignalMessaging/profiles/OWSUserProfile.h b/SignalMessaging/profiles/OWSUserProfile.h index c58e59e79..6499db695 100644 --- a/SignalMessaging/profiles/OWSUserProfile.h +++ b/SignalMessaging/profiles/OWSUserProfile.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // #import @@ -41,6 +41,10 @@ extern NSString *const kLocalProfileUniqueId; #pragma mark - Update With... Methods +- (void)updateWithProfileName:(nullable NSString *)profileName + dbConnection:(YapDatabaseConnection *)dbConnection + completion:(nullable OWSUserProfileCompletion)completion; + - (void)updateWithProfileName:(nullable NSString *)profileName avatarUrlPath:(nullable NSString *)avatarUrlPath avatarFileName:(nullable NSString *)avatarFileName diff --git a/SignalMessaging/profiles/OWSUserProfile.m b/SignalMessaging/profiles/OWSUserProfile.m index 5bcf35de0..0b229a93b 100644 --- a/SignalMessaging/profiles/OWSUserProfile.m +++ b/SignalMessaging/profiles/OWSUserProfile.m @@ -115,6 +115,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; didChange = [_avatarUrlPath isEqualToString:avatarUrlPath]; } + _avatarUrlPath = avatarUrlPath; + if (didChange) { // If the avatarURL changed, the avatarFileName can't be valid. // Clear it. @@ -137,8 +139,6 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; dbConnection:(YapDatabaseConnection *)dbConnection completion:(nullable OWSUserProfileCompletion)completion { - NSDictionary *beforeSnapshot = self.dictionaryValue; - changeBlock(self); __block BOOL didChange = YES; @@ -146,12 +146,16 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; NSString *collection = [[self class] collection]; OWSUserProfile *_Nullable latestInstance = [transaction objectForKey:self.uniqueId inCollection:collection]; if (latestInstance) { + NSDictionary *beforeSnapshot = latestInstance.dictionaryValue; + changeBlock(latestInstance); NSDictionary *afterSnapshot = latestInstance.dictionaryValue; if ([beforeSnapshot isEqual:afterSnapshot]) { - DDLogVerbose( - @"%@ Ignoring redundant update in %s: %@", self.logTag, functionName, self.debugDescription); + DDLogVerbose(@"%@ Ignoring redundant update in %s: %@", + self.logTag, + functionName, + self.debugDescription); didChange = NO; } else { [latestInstance saveWithTransaction:transaction]; @@ -200,6 +204,18 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId"; }); } +- (void)updateWithProfileName:(nullable NSString *)profileName + dbConnection:(YapDatabaseConnection *)dbConnection + completion:(nullable OWSUserProfileCompletion)completion +{ + [self applyChanges:^(OWSUserProfile *userProfile) { + [userProfile setProfileName:[profileName ows_stripped]]; + } + functionName:__PRETTY_FUNCTION__ + dbConnection:dbConnection + completion:completion]; +} + - (void)updateWithProfileName:(nullable NSString *)profileName avatarUrlPath:(nullable NSString *)avatarUrlPath avatarFileName:(nullable NSString *)avatarFileName