session-ios/Signal/src/Profiles/OWSProfileManager.m

1003 lines
34 KiB
Mathematica
Raw Normal View History

2017-07-31 20:48:43 +02:00
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "OWSProfileManager.h"
#import "Environment.h"
#import "Signal-Swift.h"
#import <SignalServiceKit/NSData+hexString.h>
#import <SignalServiceKit/NSDate+OWS.h>
#import <SignalServiceKit/OWSMessageSender.h>
#import <SignalServiceKit/SecurityUtils.h>
#import <SignalServiceKit/TSGroupThread.h>
2017-08-04 18:46:16 +02:00
#import <SignalServiceKit/TSSetProfileRequest.h>
#import <SignalServiceKit/TSStorageManager.h>
#import <SignalServiceKit/TSThread.h>
#import <SignalServiceKit/TSYapDatabaseObject.h>
#import <SignalServiceKit/TextSecureKitEnv.h>
2017-08-01 16:51:01 +02:00
2017-07-31 20:48:43 +02:00
NS_ASSUME_NONNULL_BEGIN
// UserProfile properties should only be mutated on the main thread.
@interface UserProfile : TSYapDatabaseObject
2017-08-01 16:51:01 +02:00
// These properties may be accessed from any thread.
@property (atomic, readonly) NSString *recipientId;
@property (atomic, nullable) NSData *profileKey;
// These properties may be accessed only from the main thread.
@property (nonatomic, nullable) NSString *profileName;
@property (nonatomic, nullable) NSString *avatarUrl;
@property (nonatomic, nullable) NSData *avatarDigest;
2017-08-01 16:51:01 +02:00
// This filename is relative to OWSProfileManager.profileAvatarsDirPath.
@property (nonatomic, nullable) NSString *avatarFileName;
// This should reflect when either:
//
// * The last successful update finished.
// * The current in-flight update began.
//
// This property may be accessed from any thread.
@property (nonatomic, nullable) NSDate *lastUpdateDate;
2017-08-01 16:51:01 +02:00
- (instancetype)init NS_UNAVAILABLE;
@end
#pragma mark -
@implementation UserProfile
2017-08-01 16:51:01 +02:00
- (instancetype)initWithRecipientId:(NSString *)recipientId
2017-08-01 16:51:01 +02:00
{
self = [super initWithUniqueId:recipientId];
2017-08-01 16:51:01 +02:00
if (!self) {
return self;
}
OWSAssert(recipientId.length > 0);
_recipientId = recipientId;
2017-08-01 16:51:01 +02:00
return self;
}
#pragma mark - NSObject
- (BOOL)isEqual:(UserProfile *)other
2017-08-01 16:51:01 +02:00
{
return ([other isKindOfClass:[UserProfile class]] && [self.recipientId isEqualToString:other.recipientId] &&
[self.profileName isEqualToString:other.profileName] && [self.avatarUrl isEqualToString:other.avatarUrl] &&
[self.avatarDigest isEqual:other.avatarDigest] && [self.avatarFileName isEqualToString:other.avatarFileName]);
2017-08-01 16:51:01 +02:00
}
- (NSUInteger)hash
{
return self.recipientId.hash ^ self.profileName.hash ^ self.avatarUrl.hash ^ self.avatarDigest.hash
^ self.avatarFileName.hash;
2017-08-01 16:51:01 +02:00
}
@end
#pragma mark -
2017-08-04 16:16:17 +02:00
NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
NSString *const kNSNotificationName_LocalProfileDidChange = @"kNSNotificationName_LocalProfileDidChange";
NSString *const kNSNotificationName_OtherUsersProfileDidChange = @"kNSNotificationName_OtherUsersProfileDidChange";
NSString *const kOWSProfileManager_UserWhitelistCollection = @"kOWSProfileManager_UserWhitelistCollection";
NSString *const kOWSProfileManager_GroupWhitelistCollection = @"kOWSProfileManager_GroupWhitelistCollection";
2017-07-31 20:48:43 +02:00
// TODO:
static const NSInteger kProfileKeyLength = 16;
@interface OWSProfileManager ()
2017-07-31 20:48:43 +02:00
@property (nonatomic, readonly) OWSMessageSender *messageSender;
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
@property (nonatomic, readonly) TSNetworkManager *networkManager;
2017-07-31 20:48:43 +02:00
@property (atomic, nullable) UserProfile *localUserProfile;
// This property should only be mutated on the main thread,
@property (nonatomic, nullable) UIImage *localCachedAvatarImage;
2017-07-31 20:48:43 +02:00
2017-08-03 18:05:53 +02:00
// These caches are lazy-populated. The single point of truth is the database.
//
// These three properties can be accessed on any thread.
@property (atomic, readonly) NSMutableDictionary<NSString *, NSNumber *> *userProfileWhitelistCache;
@property (atomic, readonly) NSMutableDictionary<NSString *, NSNumber *> *groupProfileWhitelistCache;
// This property should only be mutated on the main thread,
@property (nonatomic, readonly) NSCache<NSString *, UIImage *> *otherUsersProfileAvatarImageCache;
2017-08-04 21:20:33 +02:00
// This property should only be mutated on the main thread,
@property (atomic, readonly) NSMutableSet<NSString *> *currentAvatarDownloads;
2017-07-31 20:48:43 +02:00
@end
#pragma mark -
@implementation OWSProfileManager
2017-07-31 20:48:43 +02:00
+ (instancetype)sharedManager
{
static OWSProfileManager *sharedMyManager = nil;
2017-07-31 20:48:43 +02:00
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] initDefault];
});
return sharedMyManager;
}
- (instancetype)initDefault
{
TSStorageManager *storageManager = [TSStorageManager sharedManager];
OWSMessageSender *messageSender = [Environment getCurrent].messageSender;
TSNetworkManager *networkManager = [Environment getCurrent].networkManager;
2017-07-31 20:48:43 +02:00
return [self initWithStorageManager:storageManager messageSender:messageSender networkManager:networkManager];
2017-07-31 20:48:43 +02:00
}
- (instancetype)initWithStorageManager:(TSStorageManager *)storageManager
messageSender:(OWSMessageSender *)messageSender
networkManager:(TSNetworkManager *)networkManager
2017-07-31 20:48:43 +02:00
{
self = [super init];
if (!self) {
return self;
}
OWSAssert([NSThread isMainThread]);
2017-07-31 20:48:43 +02:00
OWSAssert(storageManager);
OWSAssert(messageSender);
OWSAssert(messageSender);
2017-07-31 20:48:43 +02:00
_messageSender = messageSender;
_dbConnection = storageManager.newDatabaseConnection;
_networkManager = networkManager;
_userProfileWhitelistCache = [NSMutableDictionary new];
_groupProfileWhitelistCache = [NSMutableDictionary new];
_otherUsersProfileAvatarImageCache = [NSCache new];
2017-08-04 21:20:33 +02:00
_currentAvatarDownloads = [NSMutableSet new];
2017-07-31 20:48:43 +02:00
OWSSingletonAssert();
2017-08-04 16:16:17 +02:00
self.localUserProfile = [self getOrBuildUserProfileForRecipientId:kLocalProfileUniqueId];
OWSAssert(self.localUserProfile);
if (!self.localUserProfile.profileKey) {
self.localUserProfile.profileKey = [OWSProfileManager generateLocalProfileKey];
// Make sure to save on the local db connection for consistency.
//
// NOTE: we do an async read/write here to avoid blocking during app launch path.
[self.dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[self.localUserProfile saveWithTransaction:transaction];
}];
2017-07-31 20:48:43 +02:00
}
OWSAssert(self.localUserProfile.profileKey.length == kProfileKeyLength);
2017-07-31 20:48:43 +02:00
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)observeNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
#pragma mark - User Profile Accessor
// This method can be safely called from any thread.
2017-08-04 16:16:17 +02:00
- (UserProfile *)getOrBuildUserProfileForRecipientId:(NSString *)recipientId
{
OWSAssert(recipientId.length > 0);
__block UserProfile *instance;
// Make sure to read on the local db connection for consistency.
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
instance = [UserProfile fetchObjectWithUniqueID:recipientId transaction:transaction];
}];
if (!instance) {
instance = [[UserProfile alloc] initWithRecipientId:recipientId];
}
OWSAssert(instance);
return instance;
}
// All writes to user profiles should occur on the main thread.
- (void)saveUserProfile:(UserProfile *)userProfile
{
OWSAssert([NSThread isMainThread]);
OWSAssert(userProfile);
// Make sure to save on the local db connection for consistency.
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[userProfile saveWithTransaction:transaction];
}];
if (userProfile == self.localUserProfile) {
[[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_LocalProfileDidChange
object:nil
userInfo:nil];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_OtherUsersProfileDidChange
object:nil
userInfo:nil];
}
}
2017-07-31 20:48:43 +02:00
#pragma mark - Local Profile Key
+ (NSData *)generateLocalProfileKey
{
2017-08-04 16:16:17 +02:00
DDLogInfo(@"%@ Generating profile key for local user.", self.tag);
2017-07-31 20:48:43 +02:00
// TODO:
DDLogVerbose(@"%@ Profile key generation is not yet implemented.", self.tag);
2017-07-31 20:48:43 +02:00
return [SecurityUtils generateRandomBytes:kProfileKeyLength];
}
#pragma mark - Local Profile
- (NSData *)localProfileKey
{
OWSAssert(self.localUserProfile.profileKey.length == kProfileKeyLength);
return self.localUserProfile.profileKey;
}
- (BOOL)hasLocalProfile
{
OWSAssert([NSThread isMainThread]);
return (self.localProfileName.length > 0 || self.localProfileAvatarImage != nil);
}
- (nullable NSString *)localProfileName
2017-08-01 16:51:01 +02:00
{
OWSAssert([NSThread isMainThread]);
return self.localUserProfile.profileName;
}
2017-08-01 16:51:01 +02:00
- (nullable UIImage *)localProfileAvatarImage
{
OWSAssert([NSThread isMainThread]);
2017-08-01 16:51:01 +02:00
if (!self.localCachedAvatarImage) {
if (self.localUserProfile.avatarFileName) {
self.localCachedAvatarImage = [self loadProfileAvatarWithFilename:self.localUserProfile.avatarFileName];
}
}
return self.localCachedAvatarImage;
2017-08-01 16:51:01 +02:00
}
- (void)updateLocalProfileName:(nullable NSString *)profileName
avatarImage:(nullable UIImage *)avatarImage
success:(void (^)())successBlock
failure:(void (^)())failureBlockParameter
2017-08-01 16:51:01 +02:00
{
OWSAssert([NSThread isMainThread]);
OWSAssert(successBlock);
OWSAssert(failureBlockParameter);
// Ensure that the failure block is called on the main thread.
void (^failureBlock)() = ^{
dispatch_async(dispatch_get_main_queue(), ^{
failureBlockParameter();
});
};
2017-08-01 16:51:01 +02:00
// The final steps are to:
//
// * Try to update the service.
// * Update client state on success.
void (^tryToUpdateService)(NSString *_Nullable, NSData *_Nullable, NSString *_Nullable) = ^(
NSString *_Nullable avatarUrl, NSData *_Nullable avatarDigest, NSString *_Nullable avatarFileName) {
[self updateProfileOnService:profileName
avatarUrl:avatarUrl
avatarDigest:avatarDigest
2017-08-01 16:51:01 +02:00
success:^{
// All reads and writes to user profiles should happen on the main thread.
dispatch_async(dispatch_get_main_queue(), ^{
UserProfile *userProfile = self.localUserProfile;
OWSAssert(userProfile);
userProfile.profileName = profileName;
userProfile.avatarUrl = avatarUrl;
userProfile.avatarDigest = avatarDigest;
userProfile.avatarFileName = avatarFileName;
[self saveUserProfile:userProfile];
self.localCachedAvatarImage = avatarImage;
successBlock();
});
2017-08-01 16:51:01 +02:00
}
failure:^{
failureBlock();
}];
};
UserProfile *userProfile = self.localUserProfile;
OWSAssert(userProfile);
2017-08-01 16:51:01 +02:00
// If we have a new avatar image, we must first:
//
// * Encode it to JPEG.
// * Write it to disk.
// * Upload it to service.
if (avatarImage) {
if (self.localCachedAvatarImage == avatarImage) {
OWSAssert(userProfile.avatarUrl.length > 0);
OWSAssert(userProfile.avatarDigest.length > 0);
OWSAssert(userProfile.avatarFileName.length > 0);
2017-08-01 16:51:01 +02:00
DDLogVerbose(@"%@ Updating local profile on service with unchanged avatar.", self.tag);
// If the avatar hasn't changed, reuse the existing metadata.
tryToUpdateService(userProfile.avatarUrl, userProfile.avatarDigest, userProfile.avatarFileName);
2017-08-01 16:51:01 +02:00
} else {
DDLogVerbose(@"%@ Updating local profile on service with new avatar.", self.tag);
[self writeAvatarToDisk:avatarImage
2017-08-01 16:51:01 +02:00
success:^(NSData *data, NSString *fileName) {
[self uploadAvatarToService:data
fileName:fileName
success:^(NSString *avatarUrl, NSData *avatarDigest) {
tryToUpdateService(avatarUrl, avatarDigest, fileName);
2017-08-01 16:51:01 +02:00
}
failure:^{
failureBlock();
}];
}
failure:^{
failureBlock();
}];
}
} else {
DDLogVerbose(@"%@ Updating local profile on service with no avatar.", self.tag);
tryToUpdateService(nil, nil, nil);
2017-08-01 16:51:01 +02:00
}
}
- (void)writeAvatarToDisk:(UIImage *)avatar
success:(void (^)(NSData *data, NSString *fileName))successBlock
failure:(void (^)())failureBlock
{
OWSAssert(avatar);
OWSAssert(successBlock);
OWSAssert(failureBlock);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (avatar) {
NSData *_Nullable data = UIImageJPEGRepresentation(avatar, 1.f);
OWSAssert(data);
if (data) {
NSString *fileName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:@"jpg"];
NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:fileName];
BOOL success = [data writeToFile:filePath atomically:YES];
OWSAssert(success);
if (success) {
successBlock(data, fileName);
return;
}
}
}
failureBlock();
});
}
// TODO: The exact API & encryption scheme for avatars is not yet settled.
- (void)uploadAvatarToService:(NSData *)data
fileName:(NSString *)fileName
success:(void (^)(NSString *avatarUrl, NSData *avatarDigest))successBlock
2017-08-01 16:51:01 +02:00
failure:(void (^)())failureBlock
{
OWSAssert(data.length > 0);
OWSAssert(fileName.length > 0);
OWSAssert(successBlock);
OWSAssert(failureBlock);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// TODO:
NSString *avatarUrl = @"avatarUrl";
NSData *avatarDigest = [@"avatarDigest" dataUsingEncoding:NSUTF8StringEncoding];
2017-08-01 16:51:01 +02:00
if (YES) {
successBlock(avatarUrl, avatarDigest);
2017-08-01 16:51:01 +02:00
return;
}
failureBlock();
});
}
// TODO: The exact API & encryption scheme for profiles is not yet settled.
- (void)updateProfileOnService:(nullable NSString *)localProfileName
avatarUrl:(nullable NSString *)avatarUrl
avatarDigest:(nullable NSData *)avatarDigest
2017-08-01 16:51:01 +02:00
success:(void (^)())successBlock
failure:(void (^)())failureBlock
{
OWSAssert(successBlock);
OWSAssert(failureBlock);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
2017-08-04 18:46:16 +02:00
NSData *_Nullable profileNameEncrypted = [self encryptProfileString:localProfileName];
2017-08-04 18:46:16 +02:00
TSSetProfileRequest *request = [[TSSetProfileRequest alloc] initWithProfileName:profileNameEncrypted
avatarUrl:avatarUrl
avatarDigest:avatarDigest];
[self.networkManager makeRequest:request
success:^(NSURLSessionDataTask *task, id responseObject) {
successBlock();
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
DDLogError(@"%@ Failed to update profile with error: %@", self.tag, error);
failureBlock();
}];
2017-08-01 16:51:01 +02:00
});
}
#pragma mark - Profile Whitelist
- (void)addUserToProfileWhitelist:(NSString *)recipientId
{
2017-08-03 18:05:53 +02:00
OWSAssert([NSThread isMainThread]);
OWSAssert(recipientId.length > 0);
[self.dbConnection setBool:YES forKey:recipientId inCollection:kOWSProfileManager_UserWhitelistCollection];
self.userProfileWhitelistCache[recipientId] = @(YES);
}
2017-08-03 18:05:53 +02:00
- (void)addUsersToProfileWhitelist:(NSArray<NSString *> *)recipientIds
{
OWSAssert([NSThread isMainThread]);
OWSAssert(recipientIds);
NSMutableArray<NSString *> *newRecipientIds = [NSMutableArray new];
for (NSString *recipientId in recipientIds) {
if (!self.userProfileWhitelistCache[recipientId]) {
[newRecipientIds addObject:recipientId];
}
}
if (newRecipientIds.count < 1) {
return;
}
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (NSString *recipientId in recipientIds) {
[transaction setObject:@(YES) forKey:recipientId inCollection:kOWSProfileManager_UserWhitelistCollection];
2017-08-03 18:05:53 +02:00
self.userProfileWhitelistCache[recipientId] = @(YES);
}
}];
}
- (BOOL)isUserInProfileWhitelist:(NSString *)recipientId
{
OWSAssert(recipientId.length > 0);
NSNumber *_Nullable value = self.userProfileWhitelistCache[recipientId];
if (value) {
return [value boolValue];
}
value = @([self.dbConnection hasObjectForKey:recipientId inCollection:kOWSProfileManager_UserWhitelistCollection]);
self.userProfileWhitelistCache[recipientId] = value;
return [value boolValue];
}
- (void)addGroupIdToProfileWhitelist:(NSData *)groupId
{
OWSAssert(groupId.length > 0);
NSString *groupIdKey = [groupId hexadecimalString];
[self.dbConnection setObject:@(1) forKey:groupIdKey inCollection:kOWSProfileManager_GroupWhitelistCollection];
self.groupProfileWhitelistCache[groupIdKey] = @(YES);
}
- (void)addThreadToProfileWhitelist:(TSThread *)thread
{
OWSAssert(thread);
if (thread.isGroupThread) {
TSGroupThread *groupThread = (TSGroupThread *)thread;
NSData *groupId = groupThread.groupModel.groupId;
[self addGroupIdToProfileWhitelist:groupId];
} else {
NSString *recipientId = thread.contactIdentifier;
[self addUserToProfileWhitelist:recipientId];
}
}
- (BOOL)isGroupIdInProfileWhitelist:(NSData *)groupId
{
OWSAssert(groupId.length > 0);
NSString *groupIdKey = [groupId hexadecimalString];
NSNumber *_Nullable value = self.groupProfileWhitelistCache[groupIdKey];
if (value) {
return [value boolValue];
}
value =
@(nil != [self.dbConnection objectForKey:groupIdKey inCollection:kOWSProfileManager_GroupWhitelistCollection]);
self.groupProfileWhitelistCache[groupIdKey] = value;
return [value boolValue];
}
2017-08-03 18:05:53 +02:00
- (BOOL)isThreadInProfileWhitelist:(TSThread *)thread
{
OWSAssert(thread);
if (thread.isGroupThread) {
TSGroupThread *groupThread = (TSGroupThread *)thread;
NSData *groupId = groupThread.groupModel.groupId;
return [self isGroupIdInProfileWhitelist:groupId];
2017-08-03 18:05:53 +02:00
} else {
NSString *recipientId = thread.contactIdentifier;
return [self isUserInProfileWhitelist:recipientId];
2017-08-03 18:05:53 +02:00
}
}
- (void)setContactRecipientIds:(NSArray<NSString *> *)contactRecipientIds
{
2017-08-03 18:05:53 +02:00
OWSAssert([NSThread isMainThread]);
OWSAssert(contactRecipientIds);
// TODO: The persisted whitelist could either be:
//
// * Just users manually added to the whitelist.
// * Also include users auto-added by, for example, being in the user's
// contacts or when the user initiates a 1:1 conversation with them, etc.
2017-08-03 18:05:53 +02:00
[self addUsersToProfileWhitelist:contactRecipientIds];
}
#pragma mark - Other User's Profiles
- (void)setProfileKey:(NSData *)profileKey forRecipientId:(NSString *)recipientId
{
OWSAssert(profileKey.length == kProfileKeyLength);
OWSAssert(recipientId.length > 0);
if (profileKey.length != kProfileKeyLength) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
2017-08-04 16:16:17 +02:00
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
OWSAssert(userProfile);
if (userProfile.profileKey && [userProfile.profileKey isEqual:profileKey]) {
// Ignore redundant update.
return;
}
userProfile.profileKey = profileKey;
// Clear profile state.
userProfile.profileName = nil;
userProfile.avatarUrl = nil;
userProfile.avatarDigest = nil;
userProfile.avatarFileName = nil;
[self saveUserProfile:userProfile];
[self refreshProfileForRecipientId:recipientId ignoreThrottling:YES];
});
}
- (nullable NSData *)profileKeyForRecipientId:(NSString *)recipientId
{
OWSAssert(recipientId.length > 0);
2017-08-04 16:16:17 +02:00
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
OWSAssert(userProfile);
return userProfile.profileKey;
}
- (nullable NSString *)profileNameForRecipientId:(NSString *)recipientId
{
OWSAssert([NSThread isMainThread]);
OWSAssert(recipientId.length > 0);
[self refreshProfileForRecipientId:recipientId];
2017-08-04 16:16:17 +02:00
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
return userProfile.profileName;
}
- (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId
{
OWSAssert([NSThread isMainThread]);
OWSAssert(recipientId.length > 0);
[self refreshProfileForRecipientId:recipientId];
UIImage *_Nullable image = [self.otherUsersProfileAvatarImageCache objectForKey:recipientId];
if (image) {
return image;
}
2017-08-04 16:16:17 +02:00
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
if (userProfile.avatarFileName) {
image = [self loadProfileAvatarWithFilename:userProfile.avatarFileName];
if (image) {
[self.otherUsersProfileAvatarImageCache setObject:image forKey:recipientId];
}
} else if (userProfile.avatarUrl) {
2017-08-04 21:20:33 +02:00
[self downloadAvatarForUserProfile:userProfile];
}
return image;
}
2017-08-04 21:20:33 +02:00
- (void)downloadAvatarForUserProfile:(UserProfile *)userProfile
{
2017-08-04 21:20:33 +02:00
OWSAssert([NSThread isMainThread]);
OWSAssert(userProfile);
2017-08-04 21:20:33 +02:00
if (userProfile.profileKey.length < 1 || userProfile.avatarUrl.length < 1) {
return;
}
NSData *profileKeyAtStart = userProfile.profileKey;
NSURL *url = [NSURL URLWithString:userProfile.avatarUrl];
if (!url) {
OWSFail(@"%@ Malformed avatar URL: %@", self.tag, userProfile.avatarUrl);
return;
}
NSString *_Nullable fileExtension = [[[url lastPathComponent] pathExtension] lowercaseString];
NSSet<NSString *> *validFileExtensions = [NSSet setWithArray:@[
@"jpg",
@"jpeg",
@"png",
@"gif",
]];
if (![validFileExtensions containsObject:fileExtension]) {
DDLogWarn(@"Ignoring avatar with invalid file extension: %@", userProfile.avatarUrl);
}
NSString *fileName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:fileExtension];
NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:fileName];
if ([self.currentAvatarDownloads containsObject:userProfile.recipientId]) {
// Download already in flight; ignore.
return;
}
[self.currentAvatarDownloads addObject:userProfile.recipientId];
NSString *tempDirectory = NSTemporaryDirectory();
NSString *tempFilePath = [tempDirectory stringByAppendingPathComponent:fileName];
// TODO: Should we use a special configuration as we do in TSNetworkManager?
// TODO: How does censorship circumvention fit in?
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request
progress:nil
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
return [NSURL fileURLWithPath:tempFilePath];
}
completionHandler:^(NSURLResponse *response, NSURL *filePathParam, NSError *error) {
OWSAssert([[NSURL fileURLWithPath:tempFilePath] isEqual:filePathParam]);
// Ensure disk IO and decryption occurs off the main thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *_Nullable encryptedData = (error ? nil : [NSData dataWithContentsOfFile:tempFilePath]);
NSData *_Nullable decryptedData =
[OWSProfileManager decryptProfileData:encryptedData profileKey:profileKeyAtStart];
UIImage *_Nullable image = nil;
if (decryptedData) {
// TODO: Verify avatar digest.
BOOL success = [decryptedData writeToFile:filePath atomically:YES];
if (success) {
image = [UIImage imageWithContentsOfFile:filePath];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.currentAvatarDownloads removeObject:userProfile.recipientId];
UserProfile *currentUserProfile =
[self getOrBuildUserProfileForRecipientId:userProfile.recipientId];
if (currentUserProfile.profileKey.length < 1
|| ![currentUserProfile.profileKey isEqual:userProfile.profileKey]) {
DDLogWarn(@"%@ Ignoring avatar download for obsolete user profile.", self.tag);
} else if (error) {
DDLogError(@"%@ avatar download failed: %@", self.tag, error);
} else if (!encryptedData) {
DDLogError(@"%@ avatar encrypted data could not be read.", self.tag);
} else if (!decryptedData) {
DDLogError(@"%@ avatar data could not be decrypted.", self.tag);
} else if (!image) {
DDLogError(@"%@ avatar image could not be loaded: %@", self.tag, error);
} else {
[self.otherUsersProfileAvatarImageCache setObject:image forKey:userProfile.recipientId];
userProfile.avatarFileName = fileName;
[self saveUserProfile:userProfile];
}
});
});
}];
[downloadTask resume];
}
- (void)refreshProfileForRecipientId:(NSString *)recipientId
{
[self refreshProfileForRecipientId:recipientId ignoreThrottling:NO];
}
- (void)refreshProfileForRecipientId:(NSString *)recipientId ignoreThrottling:(BOOL)ignoreThrottling
{
OWSAssert([NSThread isMainThread]);
OWSAssert(recipientId.length > 0);
2017-08-04 16:16:17 +02:00
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
if (!userProfile.profileKey) {
// There's no point in fetching the profile for a user
// if we don't have their profile key; we won't be able
// to decrypt it.
return;
}
// Throttle and debounce the updates.
const NSTimeInterval kMaxRefreshFrequency = 5 * kMinuteInterval;
if (userProfile.lastUpdateDate && fabs([userProfile.lastUpdateDate timeIntervalSinceNow]) < kMaxRefreshFrequency) {
// This profile was updated recently or already has an update in flight.
return;
}
userProfile.lastUpdateDate = [NSDate new];
[self saveUserProfile:userProfile];
[ProfileFetcherJob runWithRecipientId:recipientId
networkManager:self.networkManager
ignoreThrottling:ignoreThrottling];
}
- (void)updateProfileForRecipientId:(NSString *)recipientId
profileNameEncrypted:(NSData *_Nullable)profileNameEncrypted
avatarUrlData:(NSData *_Nullable)avatarUrlData
2017-08-04 18:46:16 +02:00
avatarDigest:(NSData *_Nullable)avatarDigestParam
{
OWSAssert(recipientId.length > 0);
2017-08-04 18:46:16 +02:00
// Ensure decryption, etc. off main thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
2017-08-04 18:46:16 +02:00
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
if (!userProfile.profileKey) {
return;
}
2017-08-04 18:46:16 +02:00
NSString *_Nullable profileName =
[self decryptProfileString:profileNameEncrypted profileKey:userProfile.profileKey];
NSString *_Nullable avatarUrl
= (avatarUrlData ? [[NSString alloc] initWithData:avatarUrlData encoding:NSUTF8StringEncoding] : nil);
NSData *_Nullable avatarDigest = avatarDigestParam;
2017-08-04 18:46:16 +02:00
if (!avatarUrl || !avatarDigest) {
// If either avatar url or digest is missing, skip both.
avatarUrl = nil;
avatarDigest = nil;
}
2017-08-04 18:46:16 +02:00
BOOL isAvatarSame = ([self isNullableStringEqual:userProfile.avatarUrl toString:avatarUrl] &&
[self isNullableDataEqual:userProfile.avatarDigest toData:avatarDigest]);
2017-08-04 18:46:16 +02:00
dispatch_async(dispatch_get_main_queue(), ^{
userProfile.profileName = profileName;
userProfile.avatarUrl = avatarUrl;
userProfile.avatarDigest = avatarDigest;
if (!isAvatarSame) {
// Evacuate avatar image cache.
[self.otherUsersProfileAvatarImageCache removeObjectForKey:recipientId];
2017-08-04 18:46:16 +02:00
if (avatarUrl) {
2017-08-04 21:20:33 +02:00
[self downloadAvatarForUserProfile:userProfile];
2017-08-04 18:46:16 +02:00
}
}
2017-08-04 18:46:16 +02:00
userProfile.lastUpdateDate = [NSDate new];
2017-08-04 18:46:16 +02:00
[self saveUserProfile:userProfile];
});
});
}
- (BOOL)isNullableDataEqual:(NSData *_Nullable)left toData:(NSData *_Nullable)right
{
if (left == nil && right == nil) {
return YES;
} else if (left == nil || right == nil) {
return YES;
} else {
return [left isEqual:right];
}
}
- (BOOL)isNullableStringEqual:(NSString *_Nullable)left toString:(NSString *_Nullable)right
{
if (left == nil && right == nil) {
return YES;
} else if (left == nil || right == nil) {
return YES;
} else {
return [left isEqualToString:right];
}
}
#pragma mark - Profile Encryption
+ (NSData *_Nullable)decryptProfileData:(NSData *_Nullable)encryptedData profileKey:(NSData *)profileKey
{
OWSAssert(profileKey.length == kProfileKeyLength);
if (!encryptedData) {
return nil;
}
// TODO: Decrypt. For now, return the input.
return encryptedData;
}
+ (NSString *_Nullable)decryptProfileString:(NSData *_Nullable)encryptedData profileKey:(NSData *)profileKey
{
OWSAssert(profileKey.length == kProfileKeyLength);
NSData *_Nullable decryptedData = [self decryptProfileData:encryptedData profileKey:profileKey];
if (decryptedData) {
return [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
} else {
return nil;
}
}
+ (NSData *_Nullable)encryptProfileData:(NSData *_Nullable)data profileKey:(NSData *)profileKey
{
OWSAssert(profileKey.length == kProfileKeyLength);
if (!data) {
return nil;
}
// TODO: Encrypt. For now, return the input.
return data;
}
+ (NSData *_Nullable)encryptProfileString:(NSString *_Nullable)value profileKey:(NSData *)profileKey
{
OWSAssert(profileKey.length == kProfileKeyLength);
if (value) {
NSData *_Nullable data = [value dataUsingEncoding:NSUTF8StringEncoding];
if (data) {
NSData *_Nullable encryptedData = [self encryptProfileData:data profileKey:profileKey];
return encryptedData;
}
}
return nil;
}
- (NSData *_Nullable)decryptProfileData:(NSData *_Nullable)encryptedData profileKey:(NSData *)profileKey
{
return [OWSProfileManager decryptProfileData:encryptedData profileKey:profileKey];
}
- (NSString *_Nullable)decryptProfileString:(NSData *_Nullable)encryptedData profileKey:(NSData *)profileKey
{
return [OWSProfileManager decryptProfileString:encryptedData profileKey:profileKey];
}
- (NSData *_Nullable)encryptProfileData:(NSData *_Nullable)data
{
return [OWSProfileManager encryptProfileData:data profileKey:self.localProfileKey];
}
- (NSData *_Nullable)encryptProfileString:(NSString *_Nullable)value
{
return [OWSProfileManager encryptProfileString:value profileKey:self.localProfileKey];
}
#pragma mark - Avatar Disk Cache
2017-08-01 22:30:24 +02:00
- (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)fileName
{
2017-08-01 16:51:01 +02:00
OWSAssert(fileName.length > 0);
NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:fileName];
UIImage *_Nullable image = [UIImage imageWithContentsOfFile:filePath];
return image;
}
- (NSString *)profileAvatarsDirPath
{
static NSString *profileAvatarsDirPath = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *documentsPath =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
profileAvatarsDirPath = [documentsPath stringByAppendingPathComponent:@"ProfileAvatars"];
BOOL isDirectory;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:profileAvatarsDirPath isDirectory:&isDirectory];
if (exists) {
OWSAssert(isDirectory);
DDLogInfo(@"Profile avatars directory already exists");
} else {
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:profileAvatarsDirPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (error) {
DDLogError(@"Failed to create profile avatars directory: %@", error);
}
}
});
return profileAvatarsDirPath;
2017-07-31 20:48:43 +02:00
}
// TODO: We may want to clean up this directory in the "orphan cleanup" logic.
2017-08-04 16:32:00 +02:00
- (void)resetProfileStorage
{
OWSAssert([NSThread isMainThread]);
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:[self profileAvatarsDirPath] error:&error];
if (error) {
DDLogError(@"Failed to delete database: %@", error.description);
}
}
2017-07-31 20:48:43 +02:00
#pragma mark - Notifications
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
OWSAssert([NSThread isMainThread]);
@synchronized(self)
{
// TODO: Sync if necessary.
}
}
#pragma mark - Logging
+ (NSString *)tag
{
return [NSString stringWithFormat:@"[%@]", self.class];
}
- (NSString *)tag
{
return self.class.tag;
}
@end
NS_ASSUME_NONNULL_END