Rework user profile saves; block SAE if no local user profile key.

This commit is contained in:
Matthew Chen 2017-12-08 14:43:11 -05:00
parent 3ea901044a
commit 97ce1a6675
7 changed files with 59 additions and 14 deletions

View File

@ -30,6 +30,9 @@ extern const NSUInteger kOWSProfileManager_MaxAvatarDiameter;
// These two methods should only be called from the main thread.
- (OWSAES256Key *)localProfileKey;
// localUserProfileExists is true if there is _ANY_ local profile.
- (BOOL)localProfileExists;
// localUserProfileExists is true if there is a local profile with a name or avatar.
- (BOOL)hasLocalProfile;
- (nullable NSString *)localProfileName;
- (nullable UIImage *)localProfileAvatarImage;

View File

@ -165,6 +165,11 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
return _localUserProfile;
}
- (BOOL)localProfileExists
{
return [OWSUserProfile localUserProfileExists:self.dbConnection];
}
- (OWSAES256Key *)localProfileKey
{
OWSAssert(self.localUserProfile.profileKey.keyData.length == kAES256_KeyByteLength);

View File

@ -40,6 +40,8 @@ extern NSString *const kLocalProfileUniqueId;
+ (OWSUserProfile *)getOrBuildUserProfileForRecipientId:(NSString *)recipientId
dbConnection:(YapDatabaseConnection *)dbConnection;
+ (BOOL)localUserProfileExists:(YapDatabaseConnection *)dbConnection;
#pragma mark - Update With... Methods
- (void)updateWithProfileName:(nullable NSString *)profileName

View File

@ -6,6 +6,7 @@
#import "NSString+OWS.h"
#import <SignalServiceKit/AppContext.h>
#import <SignalServiceKit/Cryptography.h>
#import <SignalServiceKit/NSData+hexString.h>
#import <SignalServiceKit/NSNotificationCenter+OWS.h>
#import <SignalServiceKit/TSAccountManager.h>
#import <YapDatabase/YapDatabaseConnection.h>
@ -60,6 +61,16 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
return userProfile;
}
+ (BOOL)localUserProfileExists:(YapDatabaseConnection *)dbConnection
{
__block BOOL result = NO;
[dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
result = [OWSUserProfile fetchObjectWithUniqueID:kLocalProfileUniqueId transaction:transaction] != nil;
}];
return result;
}
- (instancetype)initWithRecipientId:(NSString *)recipientId
{
self = [super initWithUniqueId:recipientId];
@ -125,7 +136,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setProfileName:[profileName ows_stripped]];
[userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setAvatarFileName:avatarFileName];
}];
}
saveIfMissing:YES];
}];
[self finalizeWithCompletion:completion];
}
@ -144,7 +156,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setAvatarFileName:avatarFileName];
[userProfile setLastUpdateDate:lastUpdateDate];
}];
}
saveIfMissing:YES];
}];
[self finalizeWithCompletion:completion];
}
@ -161,7 +174,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setProfileName:[profileName ows_stripped]];
[userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setLastUpdateDate:lastUpdateDate];
}];
}
saveIfMissing:YES];
}];
[self finalizeWithCompletion:completion];
}
@ -180,7 +194,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setProfileKey:profileKey];
[userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setAvatarFileName:avatarFileName];
}];
}
saveIfMissing:YES];
}];
[self finalizeWithCompletion:completion];
}
@ -195,7 +210,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setAvatarFileName:avatarFileName];
}];
}
saveIfMissing:YES];
}];
[self finalizeWithCompletion:completion];
}
@ -208,7 +224,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setAvatarFileName:avatarFileName];
}];
}
saveIfMissing:YES];
}];
[self finalizeWithCompletion:completion];
}
@ -221,7 +238,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setLastUpdateDate:lastUpdateDate];
}];
}
saveIfMissing:YES];
}];
[self finalizeWithCompletion:completion];
}
@ -240,7 +258,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setAvatarUrlPath:nil];
[userProfile setAvatarFileName:nil];
[userProfile setLastUpdateDate:nil];
}];
}
saveIfMissing:YES];
}];
[self finalizeWithCompletion:completion];
}
@ -257,7 +276,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setProfileKey:profileKey];
}];
}
saveIfMissing:YES];
}];
[self finalizeWithCompletion:completion];
}

View File

@ -151,6 +151,10 @@ NS_ASSUME_NONNULL_BEGIN
- (void)applyChangeToSelfAndLatestCopy:(YapDatabaseReadWriteTransaction *)transaction
changeBlock:(void (^)(id))changeBlock;
- (void)applyChangeToSelfAndLatestCopy:(YapDatabaseReadWriteTransaction *)transaction
changeBlock:(void (^)(id))changeBlock
saveIfMissing:(BOOL)saveIfMissing;
@end
NS_ASSUME_NONNULL_END

View File

@ -200,10 +200,15 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark Update With...
// This method does the work for the "updateWith..." methods. Please see
// the header for a discussion of those methods.
- (void)applyChangeToSelfAndLatestCopy:(YapDatabaseReadWriteTransaction *)transaction
changeBlock:(void (^)(id))changeBlock
{
[self applyChangeToSelfAndLatestCopy:transaction changeBlock:changeBlock saveIfMissing:NO];
}
- (void)applyChangeToSelfAndLatestCopy:(YapDatabaseReadWriteTransaction *)transaction
changeBlock:(void (^)(id))changeBlock
saveIfMissing:(BOOL)saveIfMissing
{
OWSAssert(transaction);
@ -214,6 +219,8 @@ NS_ASSUME_NONNULL_BEGIN
if (latestInstance) {
changeBlock(latestInstance);
[latestInstance saveWithTransaction:transaction];
} else if (saveIfMissing) {
[self saveWithTransaction:transaction];
}
}

View File

@ -232,10 +232,14 @@ public class ShareViewController: UINavigationController, ShareViewDelegate, SAE
Logger.info("Presenting initial root view controller")
if TSAccountManager.isRegistered() {
presentConversationPicker()
} else {
if !TSAccountManager.isRegistered() {
showNotRegisteredView()
} else if !OWSProfileManager.shared().localProfileExists() {
// This is a rare edge case, but we want to ensure that the user
// is has already saved their local profile key in the main app.
showNotReadyView()
} else {
presentConversationPicker()
}
// We don't use the AppUpdateNag in the SAE.