Clear the local profile avatar immediately when we request upload form.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 47ae4bd90c
commit 7e4859241a

@ -487,6 +487,30 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
OWSAssert(failureBlock); OWSAssert(failureBlock);
OWSAssert(avatarData == nil || avatarData.length > 0); 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), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// See: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-UsingHTTPPOST.html // See: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-UsingHTTPPOST.html
TSProfileAvatarUploadFormRequest *formRequest = [TSProfileAvatarUploadFormRequest new]; TSProfileAvatarUploadFormRequest *formRequest = [TSProfileAvatarUploadFormRequest new];
@ -497,6 +521,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
[self.networkManager makeRequest:formRequest [self.networkManager makeRequest:formRequest
success:^(NSURLSessionDataTask *task, id formResponseObject) { success:^(NSURLSessionDataTask *task, id formResponseObject) {
clearLocalAvatar();
if (avatarData == nil) { if (avatarData == nil) {
DDLogDebug(@"%@ successfully cleared avatar", self.tag); DDLogDebug(@"%@ successfully cleared avatar", self.tag);
@ -595,6 +620,10 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
}]; }];
} }
failure:^(NSURLSessionDataTask *task, NSError *error) { failure:^(NSURLSessionDataTask *task, NSError *error) {
if (task.response != nil) {
clearLocalAvatar();
}
DDLogError(@"%@ Failed to get profile avatar upload form: %@", self.tag, error); DDLogError(@"%@ Failed to get profile avatar upload form: %@", self.tag, error);
failureBlock(); failureBlock();
}]; }];

Loading…
Cancel
Save