From 7e4859241a4a3fe03cb2c0095994b4e6921244ef Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 28 Aug 2017 10:10:06 -0400 Subject: [PATCH 1/2] Clear the local profile avatar immediately when we request upload form. // FREEBIE --- Signal/src/Profiles/OWSProfileManager.m | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Signal/src/Profiles/OWSProfileManager.m b/Signal/src/Profiles/OWSProfileManager.m index 9750eca08..e96ddd836 100644 --- a/Signal/src/Profiles/OWSProfileManager.m +++ b/Signal/src/Profiles/OWSProfileManager.m @@ -487,6 +487,30 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; OWSAssert(failureBlock); OWSAssert(avatarData == nil || avatarData.length > 0); + // We want to clear the local user's profile avatar as soon as + // we request the upload form, since that request clears our + // avatar on the service. + // + // TODO: Revisit this so that failed profile updates don't leave + // the profile avatar blank, etc. + void (^clearLocalAvatar)() = ^{ + @synchronized(self) + { + UserProfile *userProfile = self.localUserProfile; + OWSAssert(userProfile); + + // TODO remote avatarUrlPath changes as result of fetching form - + // we should probably invalidate it at that point, and refresh again when + // uploading file completes. + userProfile.avatarUrlPath = nil; + userProfile.avatarFileName = nil; + + [self saveUserProfile:userProfile]; + + self.localCachedAvatarImage = nil; + } + }; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // See: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-UsingHTTPPOST.html TSProfileAvatarUploadFormRequest *formRequest = [TSProfileAvatarUploadFormRequest new]; @@ -497,6 +521,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; [self.networkManager makeRequest:formRequest success:^(NSURLSessionDataTask *task, id formResponseObject) { + clearLocalAvatar(); if (avatarData == nil) { DDLogDebug(@"%@ successfully cleared avatar", self.tag); @@ -595,6 +620,10 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; }]; } failure:^(NSURLSessionDataTask *task, NSError *error) { + if (task.response != nil) { + clearLocalAvatar(); + } + DDLogError(@"%@ Failed to get profile avatar upload form: %@", self.tag, error); failureBlock(); }]; From cdfdb80fd1ee3e729c8a1ec7021d3f12371d21be Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 28 Aug 2017 13:11:13 -0400 Subject: [PATCH 2/2] Respond to CR. // FREEBIE --- Signal/src/Profiles/OWSProfileManager.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Signal/src/Profiles/OWSProfileManager.m b/Signal/src/Profiles/OWSProfileManager.m index e96ddd836..1509ee993 100644 --- a/Signal/src/Profiles/OWSProfileManager.m +++ b/Signal/src/Profiles/OWSProfileManager.m @@ -620,6 +620,8 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; }]; } failure:^(NSURLSessionDataTask *task, NSError *error) { + // Only clear the local avatar if we have a response. Otherwise, we + // had a network failure and probably didn't reach the service. if (task.response != nil) { clearLocalAvatar(); }