Respond to CR.

pull/1/head
Matthew Chen 7 years ago
parent f7827cda7b
commit f00f608833

@ -44,6 +44,8 @@ NSString *const kOWSProfileManager_GroupWhitelistCollection = @"kOWSProfileManag
const NSUInteger kOWSProfileManager_NameDataLength = 26; const NSUInteger kOWSProfileManager_NameDataLength = 26;
const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640; const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
typedef void (^ProfileManagerFailureBlock)(NSError *error);
@interface OWSProfileManager () @interface OWSProfileManager ()
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection; @property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
@ -244,7 +246,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
successBlock(); successBlock();
}]; }];
} }
failure:^{ failure:^(NSError *error) {
failureBlock(); failureBlock();
}]; }];
}; };
@ -275,11 +277,11 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
success:^(NSString *_Nullable avatarUrlPath) { success:^(NSString *_Nullable avatarUrlPath) {
tryToUpdateService(avatarUrlPath, fileName); tryToUpdateService(avatarUrlPath, fileName);
} }
failure:^{ failure:^(NSError *error) {
failureBlock(); failureBlock();
}]; }];
} }
failure:^{ failure:^(NSError *error) {
failureBlock(); failureBlock();
}]; }];
} }
@ -289,7 +291,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
success:^(NSString *_Nullable avatarUrlPath) { success:^(NSString *_Nullable avatarUrlPath) {
tryToUpdateService(nil, nil); tryToUpdateService(nil, nil);
} }
failure:^{ failure:^(NSError *error) {
failureBlock(); failureBlock();
}]; }];
} else { } else {
@ -300,8 +302,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
- (void)writeAvatarToDisk:(UIImage *)avatar - (void)writeAvatarToDisk:(UIImage *)avatar
success:(void (^)(NSData *data, NSString *fileName))successBlock success:(void (^)(NSData *data, NSString *fileName))successBlock
failure:(void (^)(void))failureBlock failure:(ProfileManagerFailureBlock)failureBlock {
{
OWSAssertDebug(avatar); OWSAssertDebug(avatar);
OWSAssertDebug(successBlock); OWSAssertDebug(successBlock);
OWSAssertDebug(failureBlock); OWSAssertDebug(failureBlock);
@ -316,12 +317,11 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
BOOL success = [data writeToFile:filePath atomically:YES]; BOOL success = [data writeToFile:filePath atomically:YES];
OWSAssertDebug(success); OWSAssertDebug(success);
if (success) { if (success) {
successBlock(data, fileName); return successBlock(data, fileName);
return;
} }
} }
} }
failureBlock(); failureBlock(OWSErrorWithCodeDescription(OWSErrorCodeAvatarWriteFailed, @"Avatar write failed."));
}); });
} }
@ -351,8 +351,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
// If avatarData is nil, we are clearing the avatar. // If avatarData is nil, we are clearing the avatar.
- (void)uploadAvatarToService:(NSData *_Nullable)avatarData - (void)uploadAvatarToService:(NSData *_Nullable)avatarData
success:(void (^)(NSString *_Nullable avatarUrlPath))successBlock success:(void (^)(NSString *_Nullable avatarUrlPath))successBlock
failure:(void (^)(void))failureBlock failure:(ProfileManagerFailureBlock)failureBlock {
{
OWSAssertDebug(successBlock); OWSAssertDebug(successBlock);
OWSAssertDebug(failureBlock); OWSAssertDebug(failureBlock);
OWSAssertDebug(avatarData == nil || avatarData.length > 0); OWSAssertDebug(avatarData == nil || avatarData.length > 0);
@ -388,8 +387,8 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
if (![formResponseObject isKindOfClass:[NSDictionary class]]) { if (![formResponseObject isKindOfClass:[NSDictionary class]]) {
OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidResponse]); OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidResponse]);
failureBlock(); return failureBlock(
return; OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
} }
NSDictionary *responseMap = formResponseObject; NSDictionary *responseMap = formResponseObject;
OWSLogError(@"responseObject: %@", formResponseObject); OWSLogError(@"responseObject: %@", formResponseObject);
@ -397,44 +396,44 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
NSString *formAcl = responseMap[@"acl"]; NSString *formAcl = responseMap[@"acl"];
if (![formAcl isKindOfClass:[NSString class]] || formAcl.length < 1) { if (![formAcl isKindOfClass:[NSString class]] || formAcl.length < 1) {
OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidAcl]); OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidAcl]);
failureBlock(); return failureBlock(
return; OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
} }
NSString *formKey = responseMap[@"key"]; NSString *formKey = responseMap[@"key"];
if (![formKey isKindOfClass:[NSString class]] || formKey.length < 1) { if (![formKey isKindOfClass:[NSString class]] || formKey.length < 1) {
OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidKey]); OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidKey]);
failureBlock(); return failureBlock(
return; OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
} }
NSString *formPolicy = responseMap[@"policy"]; NSString *formPolicy = responseMap[@"policy"];
if (![formPolicy isKindOfClass:[NSString class]] || formPolicy.length < 1) { if (![formPolicy isKindOfClass:[NSString class]] || formPolicy.length < 1) {
OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidPolicy]); OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidPolicy]);
failureBlock(); return failureBlock(
return; OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
} }
NSString *formAlgorithm = responseMap[@"algorithm"]; NSString *formAlgorithm = responseMap[@"algorithm"];
if (![formAlgorithm isKindOfClass:[NSString class]] || formAlgorithm.length < 1) { if (![formAlgorithm isKindOfClass:[NSString class]] || formAlgorithm.length < 1) {
OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidAlgorithm]); OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidAlgorithm]);
failureBlock(); return failureBlock(
return; OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
} }
NSString *formCredential = responseMap[@"credential"]; NSString *formCredential = responseMap[@"credential"];
if (![formCredential isKindOfClass:[NSString class]] || formCredential.length < 1) { if (![formCredential isKindOfClass:[NSString class]] || formCredential.length < 1) {
OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidCredential]); OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidCredential]);
failureBlock(); return failureBlock(
return; OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
} }
NSString *formDate = responseMap[@"date"]; NSString *formDate = responseMap[@"date"];
if (![formDate isKindOfClass:[NSString class]] || formDate.length < 1) { if (![formDate isKindOfClass:[NSString class]] || formDate.length < 1) {
OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidDate]); OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidDate]);
failureBlock(); return failureBlock(
return; OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
} }
NSString *formSignature = responseMap[@"signature"]; NSString *formSignature = responseMap[@"signature"];
if (![formSignature isKindOfClass:[NSString class]] || formSignature.length < 1) { if (![formSignature isKindOfClass:[NSString class]] || formSignature.length < 1) {
OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidSignature]); OWSProdFail([OWSAnalyticsEvents profileManagerErrorAvatarUploadFormInvalidSignature]);
failureBlock(); return failureBlock(
return; OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
} }
[self.avatarHTTPManager POST:@"" [self.avatarHTTPManager POST:@""
@ -472,7 +471,8 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
} }
failure:^(NSURLSessionDataTask *_Nullable uploadTask, NSError *_Nonnull error) { failure:^(NSURLSessionDataTask *_Nullable uploadTask, NSError *_Nonnull error) {
OWSLogError(@"uploading avatar failed with error: %@", error); OWSLogError(@"uploading avatar failed with error: %@", error);
failureBlock(); return failureBlock(
OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
}]; }];
} }
failure:^(NSURLSessionDataTask *task, NSError *error) { failure:^(NSURLSessionDataTask *task, NSError *error) {
@ -483,15 +483,15 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
} }
OWSLogError(@"Failed to get profile avatar upload form: %@", error); OWSLogError(@"Failed to get profile avatar upload form: %@", error);
failureBlock(); return failureBlock(
OWSErrorWithCodeDescription(OWSErrorCodeAvatarUploadFailed, @"Avatar upload failed."));
}]; }];
}); });
} }
- (void)updateServiceWithProfileName:(nullable NSString *)localProfileName - (void)updateServiceWithProfileName:(nullable NSString *)localProfileName
success:(void (^)(void))successBlock success:(void (^)(void))successBlock
failure:(void (^)(void))failureBlock failure:(ProfileManagerFailureBlock)failureBlock {
{
OWSAssertDebug(successBlock); OWSAssertDebug(successBlock);
OWSAssertDebug(failureBlock); OWSAssertDebug(failureBlock);
@ -505,7 +505,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
} }
failure:^(NSURLSessionDataTask *task, NSError *error) { failure:^(NSURLSessionDataTask *task, NSError *error) {
OWSLogError(@"Failed to update profile with error: %@", error); OWSLogError(@"Failed to update profile with error: %@", error);
failureBlock(); failureBlock(error);
}]; }];
}); });
} }
@ -566,11 +566,12 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
[self [self
rotateLocalProfileKeyIfNecessaryWithSuccess:^{ rotateLocalProfileKeyIfNecessaryWithSuccess:^{
} }
failure:^ { failure:^(NSError *error) {
}]; }];
} }
- (void)rotateLocalProfileKeyIfNecessaryWithSuccess:(dispatch_block_t)success failure:(dispatch_block_t)failure { - (void)rotateLocalProfileKeyIfNecessaryWithSuccess:(dispatch_block_t)success
failure:(ProfileManagerFailureBlock)failure {
OWSAssertDebug(AppReadiness.isAppReady); OWSAssertDebug(AppReadiness.isAppReady);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@ -620,7 +621,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
BOOL isProfileKeySharedWithBlocked = (intersectingRecipientIds.count > 0 || intersectingGroupIds.count > 0); BOOL isProfileKeySharedWithBlocked = (intersectingRecipientIds.count > 0 || intersectingGroupIds.count > 0);
if (!isProfileKeySharedWithBlocked) { if (!isProfileKeySharedWithBlocked) {
// No need to rotate the profile key. // No need to rotate the profile key.
return; return success();
} }
// Rotate the profile key // Rotate the profile key
@ -656,9 +657,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
// The value doesn't matter, we just need any non-NSError value. // The value doesn't matter, we just need any non-NSError value.
resolve(@(1)); resolve(@(1));
} }
failure:^{ failure:^(NSError *error) {
NSError *error = OWSErrorWithCodeDescription(
OWSErrorCodeProfileUpdateFailed, @"Update to profile name failed.");
resolve(error); resolve(error);
}]; }];
}]; }];
@ -686,10 +685,8 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
resolve(@(1)); resolve(@(1));
}]; }];
} }
failure:^{ failure:^(NSError *error) {
OWSLogInfo(@"Update to profile avatar after profile key rotation failed."); OWSLogInfo(@"Update to profile avatar after profile key rotation failed.");
NSError *error = OWSErrorWithCodeDescription(
OWSErrorCodeProfileUpdateFailed, @"Update to profile avatar failed.");
resolve(error); resolve(error);
}]; }];
}]; }];
@ -717,8 +714,12 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
promise = promise.then(^(id value) { promise = promise.then(^(id value) {
success(); success();
}); });
promise = promise.catch(^(id error) { promise = promise.catch(^(NSError *error) {
failure(); if ([error isKindOfClass:[NSError class]]) {
failure(error);
} else {
failure(OWSErrorWithCodeDescription(OWSErrorCodeProfileUpdateFailed, @"Profile key rotation failed."));
}
}); });
[promise retainUntilComplete]; [promise retainUntilComplete];
}); });

@ -49,6 +49,8 @@ typedef NS_ENUM(NSInteger, OWSErrorCode) {
OWSErrorCodeMessageResponseFailed = 777422, OWSErrorCodeMessageResponseFailed = 777422,
OWSErrorCodeInvalidMessage = 777423, OWSErrorCodeInvalidMessage = 777423,
OWSErrorCodeProfileUpdateFailed = 777424, OWSErrorCodeProfileUpdateFailed = 777424,
OWSErrorCodeAvatarWriteFailed = 777425,
OWSErrorCodeAvatarUploadFailed = 777426,
}; };
extern NSString *const OWSErrorRecipientIdentifierKey; extern NSString *const OWSErrorRecipientIdentifierKey;

Loading…
Cancel
Save