2017-07-31 20:48:43 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
#import "OWSProfileManager.h"
|
|
|
|
#import "Environment.h"
|
2017-08-03 17:13:40 +02:00
|
|
|
#import "Signal-Swift.h"
|
2017-08-14 17:31:43 +02:00
|
|
|
#import <SignalServiceKit/Cryptography.h>
|
2017-08-02 19:12:26 +02:00
|
|
|
#import <SignalServiceKit/NSData+hexString.h>
|
|
|
|
#import <SignalServiceKit/NSDate+OWS.h>
|
|
|
|
#import <SignalServiceKit/OWSMessageSender.h>
|
2017-08-14 17:31:43 +02:00
|
|
|
#import <SignalServiceKit/OWSRequestBuilder.h>
|
2017-08-02 19:12:26 +02:00
|
|
|
#import <SignalServiceKit/SecurityUtils.h>
|
|
|
|
#import <SignalServiceKit/TSGroupThread.h>
|
2017-08-04 23:29:48 +02:00
|
|
|
#import <SignalServiceKit/TSProfileAvatarUploadFormRequest.h>
|
2017-08-02 19:12:26 +02:00
|
|
|
#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
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
// UserProfile properties should only be mutated on the main thread.
|
2017-08-02 16:36:54 +02:00
|
|
|
@interface UserProfile : TSYapDatabaseObject
|
2017-08-01 16:51:01 +02:00
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
// These properties may be accessed from any thread.
|
|
|
|
@property (atomic, readonly) NSString *recipientId;
|
2017-08-14 17:31:43 +02:00
|
|
|
@property (atomic, nullable) OWSAES128Key *profileKey;
|
2017-08-02 18:03:06 +02:00
|
|
|
|
|
|
|
// These properties may be accessed only from the main thread.
|
2017-08-02 16:36:54 +02:00
|
|
|
@property (nonatomic, nullable) NSString *profileName;
|
2017-08-14 17:31:43 +02:00
|
|
|
|
2017-08-14 20:51:51 +02:00
|
|
|
@property (nonatomic, nullable) NSString *avatarUrlPath;
|
2017-08-01 16:51:01 +02:00
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
// This filename is relative to OWSProfileManager.profileAvatarsDirPath.
|
2017-08-02 16:36:54 +02:00
|
|
|
@property (nonatomic, nullable) NSString *avatarFileName;
|
|
|
|
|
|
|
|
// This should reflect when either:
|
|
|
|
//
|
2017-08-02 18:03:06 +02:00
|
|
|
// * The last successful update finished.
|
|
|
|
// * The current in-flight update began.
|
|
|
|
//
|
|
|
|
// This property may be accessed from any thread.
|
2017-08-02 16:36:54 +02:00
|
|
|
@property (nonatomic, nullable) NSDate *lastUpdateDate;
|
2017-08-01 16:51:01 +02:00
|
|
|
|
|
|
|
- (instancetype)init NS_UNAVAILABLE;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
@implementation UserProfile
|
2017-08-01 16:51:01 +02:00
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
- (instancetype)initWithRecipientId:(NSString *)recipientId
|
2017-08-01 16:51:01 +02:00
|
|
|
{
|
2017-08-02 16:36:54 +02:00
|
|
|
self = [super initWithUniqueId:recipientId];
|
2017-08-01 16:51:01 +02:00
|
|
|
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
_recipientId = recipientId;
|
2017-08-01 16:51:01 +02:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - NSObject
|
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
- (BOOL)isEqual:(UserProfile *)other
|
2017-08-01 16:51:01 +02:00
|
|
|
{
|
2017-08-02 16:36:54 +02:00
|
|
|
return ([other isKindOfClass:[UserProfile class]] && [self.recipientId isEqualToString:other.recipientId] &&
|
2017-08-14 20:51:51 +02:00
|
|
|
[self.profileName isEqualToString:other.profileName] && [self.avatarUrlPath isEqualToString:other.avatarUrlPath] &&
|
2017-08-11 17:26:59 +02:00
|
|
|
[self.avatarFileName isEqualToString:other.avatarFileName]);
|
2017-08-01 16:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger)hash
|
|
|
|
{
|
2017-08-14 20:51:51 +02:00
|
|
|
return self.recipientId.hash ^ self.profileName.hash ^ self.avatarUrlPath.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";
|
2017-08-02 18:03:06 +02:00
|
|
|
|
2017-07-31 23:49:52 +02:00
|
|
|
NSString *const kNSNotificationName_LocalProfileDidChange = @"kNSNotificationName_LocalProfileDidChange";
|
2017-08-02 15:27:29 +02:00
|
|
|
NSString *const kNSNotificationName_OtherUsersProfileDidChange = @"kNSNotificationName_OtherUsersProfileDidChange";
|
2017-07-31 23:49:52 +02:00
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
NSString *const kOWSProfileManager_UserWhitelistCollection = @"kOWSProfileManager_UserWhitelistCollection";
|
|
|
|
NSString *const kOWSProfileManager_GroupWhitelistCollection = @"kOWSProfileManager_GroupWhitelistCollection";
|
2017-08-01 18:52:15 +02:00
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
/// The max bytes for a user's profile name, encoded in UTF8.
|
|
|
|
/// Before encrypting and submitting we NULL pad the name data to this length.
|
|
|
|
static const NSUInteger kOWSProfileManager_NameDataLength = 26;
|
2017-08-15 17:37:12 +02:00
|
|
|
const NSUInteger kOWSProfileManager_MaxAvatarWidth = 640;
|
2017-07-31 20:48:43 +02:00
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
@interface OWSProfileManager ()
|
2017-07-31 20:48:43 +02:00
|
|
|
|
|
|
|
@property (nonatomic, readonly) OWSMessageSender *messageSender;
|
2017-08-01 18:30:45 +02:00
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
|
2017-08-03 17:13:40 +02:00
|
|
|
@property (nonatomic, readonly) TSNetworkManager *networkManager;
|
2017-07-31 20:48:43 +02:00
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
@property (atomic, nullable) UserProfile *localUserProfile;
|
2017-08-02 16:36:54 +02:00
|
|
|
// 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.
|
2017-08-02 16:36:54 +02:00
|
|
|
//
|
|
|
|
// 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-01 19:53:51 +02:00
|
|
|
|
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 -
|
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
@implementation OWSProfileManager
|
2017-07-31 20:48:43 +02:00
|
|
|
|
|
|
|
+ (instancetype)sharedManager
|
|
|
|
{
|
2017-08-02 19:12:26 +02:00
|
|
|
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];
|
2017-08-02 19:12:26 +02:00
|
|
|
OWSMessageSender *messageSender = [Environment getCurrent].messageSender;
|
2017-08-03 17:13:40 +02:00
|
|
|
TSNetworkManager *networkManager = [Environment getCurrent].networkManager;
|
2017-07-31 20:48:43 +02:00
|
|
|
|
2017-08-03 17:13:40 +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
|
2017-08-03 17:13:40 +02:00
|
|
|
networkManager:(TSNetworkManager *)networkManager
|
2017-07-31 20:48:43 +02:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:53:51 +02:00
|
|
|
OWSAssert([NSThread isMainThread]);
|
2017-07-31 20:48:43 +02:00
|
|
|
OWSAssert(storageManager);
|
|
|
|
OWSAssert(messageSender);
|
2017-08-03 17:13:40 +02:00
|
|
|
OWSAssert(messageSender);
|
2017-07-31 20:48:43 +02:00
|
|
|
|
|
|
|
_messageSender = messageSender;
|
2017-08-01 18:30:45 +02:00
|
|
|
_dbConnection = storageManager.newDatabaseConnection;
|
2017-08-03 17:13:40 +02:00
|
|
|
_networkManager = networkManager;
|
|
|
|
|
2017-08-01 20:56:53 +02:00
|
|
|
_userProfileWhitelistCache = [NSMutableDictionary new];
|
|
|
|
_groupProfileWhitelistCache = [NSMutableDictionary new];
|
2017-08-02 16:36:54 +02:00
|
|
|
_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];
|
2017-08-02 18:03:06 +02:00
|
|
|
OWSAssert(self.localUserProfile);
|
|
|
|
if (!self.localUserProfile.profileKey) {
|
2017-08-14 17:31:43 +02:00
|
|
|
DDLogInfo(@"%@ Generating local profile key", self.tag);
|
|
|
|
self.localUserProfile.profileKey = [OWSAES128Key generateRandomKey];
|
2017-08-02 18:03:06 +02:00
|
|
|
// 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
|
|
|
}
|
2017-08-14 17:31:43 +02:00
|
|
|
OWSAssert(self.localUserProfile.profileKey.keyData.length == kAES128_KeyByteLength);
|
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];
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
- (AFHTTPSessionManager *)avatarHTTPManager
|
|
|
|
{
|
|
|
|
return [OWSSignalService sharedInstance].cdnSessionManager;
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
#pragma mark - User Profile Accessor
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
// This method can be safely called from any thread.
|
2017-08-04 16:16:17 +02:00
|
|
|
- (UserProfile *)getOrBuildUserProfileForRecipientId:(NSString *)recipientId
|
2017-08-02 16:36:54 +02:00
|
|
|
{
|
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
|
|
|
|
__block UserProfile *instance;
|
|
|
|
// Make sure to read on the local db connection for consistency.
|
2017-08-02 18:03:06 +02:00
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2017-08-02 16:36:54 +02:00
|
|
|
instance = [UserProfile fetchObjectWithUniqueID:recipientId transaction:transaction];
|
|
|
|
}];
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
if (!instance) {
|
|
|
|
instance = [[UserProfile alloc] initWithRecipientId:recipientId];
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
OWSAssert(instance);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
// All writes to user profiles should occur on the main thread.
|
|
|
|
- (void)saveUserProfile:(UserProfile *)userProfile
|
2017-08-02 16:36:54 +02:00
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
2017-08-02 18:03:06 +02:00
|
|
|
OWSAssert(userProfile);
|
2017-08-02 16:36:54 +02:00
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
// Make sure to save on the local db connection for consistency.
|
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
[userProfile saveWithTransaction:transaction];
|
|
|
|
}];
|
2017-08-03 17:13:40 +02:00
|
|
|
|
|
|
|
if (userProfile == self.localUserProfile) {
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_LocalProfileDidChange
|
|
|
|
object:nil
|
|
|
|
userInfo:nil];
|
|
|
|
} else {
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_OtherUsersProfileDidChange
|
|
|
|
object:nil
|
|
|
|
userInfo:nil];
|
|
|
|
}
|
2017-08-02 16:36:54 +02:00
|
|
|
}
|
|
|
|
|
2017-07-31 23:49:52 +02:00
|
|
|
#pragma mark - Local Profile
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
- (OWSAES128Key *)localProfileKey
|
2017-08-02 18:03:06 +02:00
|
|
|
{
|
2017-08-14 17:31:43 +02:00
|
|
|
OWSAssert(self.localUserProfile.profileKey.keyData.length == kAES128_KeyByteLength);
|
2017-08-02 18:03:06 +02:00
|
|
|
|
|
|
|
return self.localUserProfile.profileKey;
|
|
|
|
}
|
|
|
|
|
2017-08-03 23:43:21 +02:00
|
|
|
- (BOOL)hasLocalProfile
|
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
|
|
|
|
return (self.localProfileName.length > 0 || self.localProfileAvatarImage != nil);
|
|
|
|
}
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
- (nullable NSString *)localProfileName
|
2017-08-01 16:51:01 +02:00
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
return self.localUserProfile.profileName;
|
2017-08-02 16:36:54 +02:00
|
|
|
}
|
2017-08-01 16:51:01 +02:00
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
- (nullable UIImage *)localProfileAvatarImage
|
2017-08-02 16:36:54 +02:00
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
2017-08-01 16:51:01 +02:00
|
|
|
|
2017-08-04 18:55:40 +02:00
|
|
|
if (!self.localCachedAvatarImage) {
|
|
|
|
if (self.localUserProfile.avatarFileName) {
|
|
|
|
self.localCachedAvatarImage = [self loadProfileAvatarWithFilename:self.localUserProfile.avatarFileName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
return self.localCachedAvatarImage;
|
2017-08-01 16:51:01 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
- (void)updateLocalProfileName:(nullable NSString *)profileName
|
|
|
|
avatarImage:(nullable UIImage *)avatarImage
|
2017-08-01 17:31:00 +02:00
|
|
|
success:(void (^)())successBlock
|
|
|
|
failure:(void (^)())failureBlockParameter
|
2017-08-01 16:51:01 +02:00
|
|
|
{
|
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
OWSAssert(successBlock);
|
2017-08-01 17:31:00 +02:00
|
|
|
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.
|
2017-08-11 17:26:59 +02:00
|
|
|
void (^tryToUpdateService)(NSString *_Nullable, NSString *_Nullable) = ^(
|
2017-08-14 20:51:51 +02:00
|
|
|
NSString *_Nullable avatarUrlPath, NSString *_Nullable avatarFileName) {
|
2017-08-14 17:31:43 +02:00
|
|
|
[self updateServiceWithProfileName:profileName
|
2017-08-01 16:51:01 +02:00
|
|
|
success:^{
|
2017-08-02 16:36:54 +02:00
|
|
|
// All reads and writes to user profiles should happen on the main thread.
|
2017-08-01 17:31:00 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2017-08-02 18:03:06 +02:00
|
|
|
UserProfile *userProfile = self.localUserProfile;
|
2017-08-02 16:36:54 +02:00
|
|
|
OWSAssert(userProfile);
|
|
|
|
userProfile.profileName = profileName;
|
2017-08-14 17:31:43 +02:00
|
|
|
|
2017-08-14 20:51:51 +02:00
|
|
|
// TODO remote avatarUrlPath changes as result of fetching form -
|
2017-08-14 17:31:43 +02:00
|
|
|
// we should probably invalidate it at that point, and refresh again when uploading file completes.
|
2017-08-14 20:51:51 +02:00
|
|
|
userProfile.avatarUrlPath = avatarUrlPath;
|
2017-08-02 16:36:54 +02:00
|
|
|
userProfile.avatarFileName = avatarFileName;
|
2017-08-02 18:03:06 +02:00
|
|
|
|
|
|
|
[self saveUserProfile:userProfile];
|
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
self.localCachedAvatarImage = avatarImage;
|
|
|
|
|
2017-08-01 17:31:00 +02:00
|
|
|
successBlock();
|
|
|
|
});
|
2017-08-01 16:51:01 +02:00
|
|
|
}
|
|
|
|
failure:^{
|
|
|
|
failureBlock();
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
UserProfile *userProfile = self.localUserProfile;
|
2017-08-02 16:36:54 +02:00
|
|
|
OWSAssert(userProfile);
|
|
|
|
|
|
|
|
if (avatarImage) {
|
2017-08-09 04:24:52 +02:00
|
|
|
|
|
|
|
// If we have a new avatar image, we must first:
|
|
|
|
//
|
|
|
|
// * Encode it to JPEG.
|
|
|
|
// * Write it to disk.
|
|
|
|
// * Encrypt it
|
|
|
|
// * Upload it to asset service
|
|
|
|
// * Send asset service info to Signal Service
|
2017-08-02 16:36:54 +02:00
|
|
|
if (self.localCachedAvatarImage == avatarImage) {
|
2017-08-14 20:51:51 +02:00
|
|
|
OWSAssert(userProfile.avatarUrlPath.length > 0);
|
2017-08-02 16:36:54 +02:00
|
|
|
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.
|
2017-08-14 20:51:51 +02:00
|
|
|
tryToUpdateService(userProfile.avatarUrlPath, userProfile.avatarFileName);
|
2017-08-01 16:51:01 +02:00
|
|
|
} else {
|
|
|
|
DDLogVerbose(@"%@ Updating local profile on service with new avatar.", self.tag);
|
2017-08-02 16:36:54 +02:00
|
|
|
[self writeAvatarToDisk:avatarImage
|
2017-08-01 16:51:01 +02:00
|
|
|
success:^(NSData *data, NSString *fileName) {
|
|
|
|
[self uploadAvatarToService:data
|
2017-08-14 20:51:51 +02:00
|
|
|
success:^(NSString *avatarUrlPath) {
|
|
|
|
tryToUpdateService(avatarUrlPath, 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);
|
2017-08-11 17:26:59 +02:00
|
|
|
tryToUpdateService(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) {
|
2017-08-15 17:37:12 +02:00
|
|
|
NSData *data = [self processedImageDataForRawAvatar:avatar];
|
2017-08-01 16:51:01 +02:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-15 17:37:12 +02:00
|
|
|
- (NSData *)processedImageDataForRawAvatar:(UIImage *)image
|
|
|
|
{
|
|
|
|
NSUInteger kMaxAvatarBytes = 5 * 1000 * 1000;
|
|
|
|
|
|
|
|
if (image.size.width != kOWSProfileManager_MaxAvatarWidth
|
|
|
|
|| image.size.height != kOWSProfileManager_MaxAvatarWidth) {
|
|
|
|
// To help ensure the user is being shown the same cropping of their avatar as
|
|
|
|
// everyone else will see, we want to be sure that the image was resized before this point.
|
|
|
|
OWSFail(@"Avatar image should have been resized before trying to upload");
|
|
|
|
image = [image resizedImageToFillPixelSize:CGSizeMake(kOWSProfileManager_MaxAvatarWidth,
|
|
|
|
kOWSProfileManager_MaxAvatarWidth)];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSData *_Nullable data;
|
|
|
|
for (NSUInteger attempts = 0; attempts < 5; attempts++) {
|
|
|
|
CGFloat quality = (CGFloat)0.95 - attempts * (CGFloat)0.1;
|
|
|
|
data = UIImageJPEGRepresentation(image, quality);
|
|
|
|
if (data.length <= kMaxAvatarBytes) {
|
|
|
|
return data;
|
|
|
|
} else {
|
|
|
|
// This for-loop is really just paranoia. Our avatar dimensions are so small that
|
|
|
|
// it's incredibly unlikely we wouldn't be able to fit our profile photo with even
|
|
|
|
// our highest quality.
|
|
|
|
OWSFail(@"Suprised to find profile avatar was too large. Was it scaled properly? image: %@", image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-08-09 04:24:52 +02:00
|
|
|
- (void)uploadAvatarToService:(NSData *)avatarData
|
2017-08-14 20:51:51 +02:00
|
|
|
success:(void (^)(NSString *avatarUrlPath))successBlock
|
2017-08-01 16:51:01 +02:00
|
|
|
failure:(void (^)())failureBlock
|
|
|
|
{
|
2017-08-09 04:24:52 +02:00
|
|
|
OWSAssert(avatarData.length > 0);
|
2017-08-01 16:51:01 +02:00
|
|
|
OWSAssert(successBlock);
|
|
|
|
OWSAssert(failureBlock);
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
2017-08-14 17:31:43 +02:00
|
|
|
NSData *encryptedAvatarData = [self encryptProfileData:avatarData];
|
2017-08-09 04:24:52 +02:00
|
|
|
OWSAssert(encryptedAvatarData.length > 0);
|
2017-08-04 23:29:48 +02:00
|
|
|
|
|
|
|
// See: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-UsingHTTPPOST.html
|
|
|
|
TSProfileAvatarUploadFormRequest *formRequest = [TSProfileAvatarUploadFormRequest new];
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
// TODO: Since this form request causes the server to reset my avatar URL, if the update fails
|
|
|
|
// at some point from here on out, we want the user to understand they probably no longer have
|
|
|
|
// a profile avatar on the server.
|
|
|
|
|
2017-08-04 23:29:48 +02:00
|
|
|
[self.networkManager makeRequest:formRequest
|
|
|
|
success:^(NSURLSessionDataTask *task, id formResponseObject) {
|
|
|
|
|
|
|
|
if (![formResponseObject isKindOfClass:[NSDictionary class]]) {
|
|
|
|
OWSProdFail(@"profile_manager_error_avatar_upload_form_invalid_response");
|
|
|
|
failureBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSDictionary *responseMap = formResponseObject;
|
|
|
|
DDLogError(@"responseObject: %@", formResponseObject);
|
|
|
|
|
|
|
|
NSString *formAcl = responseMap[@"acl"];
|
|
|
|
if (![formAcl isKindOfClass:[NSString class]] || formAcl.length < 1) {
|
|
|
|
OWSProdFail(@"profile_manager_error_avatar_upload_form_invalid_acl");
|
|
|
|
failureBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSString *formKey = responseMap[@"key"];
|
|
|
|
if (![formKey isKindOfClass:[NSString class]] || formKey.length < 1) {
|
|
|
|
OWSProdFail(@"profile_manager_error_avatar_upload_form_invalid_key");
|
|
|
|
failureBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSString *formPolicy = responseMap[@"policy"];
|
|
|
|
if (![formPolicy isKindOfClass:[NSString class]] || formPolicy.length < 1) {
|
|
|
|
OWSProdFail(@"profile_manager_error_avatar_upload_form_invalid_policy");
|
|
|
|
failureBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSString *formAlgorithm = responseMap[@"algorithm"];
|
|
|
|
if (![formAlgorithm isKindOfClass:[NSString class]] || formAlgorithm.length < 1) {
|
|
|
|
OWSProdFail(@"profile_manager_error_avatar_upload_form_invalid_algorithm");
|
|
|
|
failureBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSString *formCredential = responseMap[@"credential"];
|
|
|
|
if (![formCredential isKindOfClass:[NSString class]] || formCredential.length < 1) {
|
|
|
|
OWSProdFail(@"profile_manager_error_avatar_upload_form_invalid_credential");
|
|
|
|
failureBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSString *formDate = responseMap[@"date"];
|
|
|
|
if (![formDate isKindOfClass:[NSString class]] || formDate.length < 1) {
|
|
|
|
OWSProdFail(@"profile_manager_error_avatar_upload_form_invalid_date");
|
|
|
|
failureBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSString *formSignature = responseMap[@"signature"];
|
|
|
|
if (![formSignature isKindOfClass:[NSString class]] || formSignature.length < 1) {
|
|
|
|
OWSProdFail(@"profile_manager_error_avatar_upload_form_invalid_signature");
|
|
|
|
failureBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
[self.avatarHTTPManager POST:@""
|
2017-08-09 04:24:52 +02:00
|
|
|
parameters:nil
|
|
|
|
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
|
|
|
|
NSData * (^formDataForString)(NSString *formString) = ^(NSString *formString) {
|
|
|
|
return [formString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
};
|
|
|
|
|
|
|
|
// We have to build up the form manually vs. simply passing in a paramaters dict
|
|
|
|
// because AWS is sensitive to the order of the order of the form params (at least
|
|
|
|
// the "key" field must occur early on).
|
|
|
|
// For consistency, all fields are ordered here in a known working order.
|
|
|
|
[formData appendPartWithFormData:formDataForString(formKey) name:@"key"];
|
|
|
|
[formData appendPartWithFormData:formDataForString(formAcl) name:@"acl"];
|
|
|
|
[formData appendPartWithFormData:formDataForString(formAlgorithm) name:@"x-amz-algorithm"];
|
|
|
|
[formData appendPartWithFormData:formDataForString(formCredential) name:@"x-amz-credential"];
|
|
|
|
[formData appendPartWithFormData:formDataForString(formDate) name:@"x-amz-date"];
|
|
|
|
[formData appendPartWithFormData:formDataForString(formPolicy) name:@"policy"];
|
|
|
|
[formData appendPartWithFormData:formDataForString(formSignature) name:@"x-amz-signature"];
|
|
|
|
[formData appendPartWithFormData:formDataForString(OWSMimeTypeApplicationOctetStream)
|
|
|
|
name:@"Content-Type"];
|
|
|
|
[formData appendPartWithFormData:encryptedAvatarData name:@"file"];
|
|
|
|
|
|
|
|
DDLogVerbose(@"%@ constructed body", self.tag);
|
|
|
|
}
|
2017-08-04 23:29:48 +02:00
|
|
|
progress:^(NSProgress *_Nonnull uploadProgress) {
|
2017-08-09 04:24:52 +02:00
|
|
|
DDLogVerbose(
|
|
|
|
@"%@ avatar upload progress: %.2f%%", self.tag, uploadProgress.fractionCompleted * 100);
|
2017-08-04 23:29:48 +02:00
|
|
|
}
|
2017-08-09 04:24:52 +02:00
|
|
|
success:^(NSURLSessionDataTask *_Nonnull uploadTask, id _Nullable responseObject) {
|
|
|
|
OWSAssert([uploadTask.response isKindOfClass:[NSHTTPURLResponse class]]);
|
|
|
|
NSHTTPURLResponse *response = (NSHTTPURLResponse *)uploadTask.response;
|
|
|
|
|
|
|
|
// We could also construct this URL locally from manager.baseUrl + formKey
|
|
|
|
// but the approach of getting it from the remote provider seems a more
|
|
|
|
// robust way to ensure we've actually created the resource where we
|
|
|
|
// think we have.
|
2017-08-14 20:51:51 +02:00
|
|
|
NSString *avatarUrlPath = response.allHeaderFields[@"Location"];
|
|
|
|
if (avatarUrlPath.length == 0) {
|
2017-08-09 04:24:52 +02:00
|
|
|
OWSProdFail(@"profile_manager_error_avatar_upload_no_location_in_response");
|
2017-08-04 23:29:48 +02:00
|
|
|
failureBlock();
|
2017-08-09 04:24:52 +02:00
|
|
|
return;
|
2017-08-04 23:29:48 +02:00
|
|
|
}
|
2017-08-09 04:24:52 +02:00
|
|
|
|
2017-08-14 20:51:51 +02:00
|
|
|
DDLogVerbose(@"%@ successfully uploaded avatar url: %@", self.tag, avatarUrlPath);
|
|
|
|
successBlock(avatarUrlPath);
|
2017-08-09 04:24:52 +02:00
|
|
|
}
|
|
|
|
failure:^(NSURLSessionDataTask *_Nullable uploadTask, NSError *_Nonnull error) {
|
|
|
|
DDLogVerbose(@"%@ uploading avatar failed with error: %@", self.tag, error);
|
|
|
|
failureBlock();
|
2017-08-04 23:29:48 +02:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
failure:^(NSURLSessionDataTask *task, NSError *error) {
|
|
|
|
DDLogError(@"%@ Failed to get profile avatar upload form: %@", self.tag, error);
|
|
|
|
failureBlock();
|
|
|
|
}];
|
2017-08-01 16:51:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: The exact API & encryption scheme for profiles is not yet settled.
|
2017-08-14 17:31:43 +02:00
|
|
|
- (void)updateServiceWithProfileName:(nullable NSString *)localProfileName
|
|
|
|
success:(void (^)())successBlock
|
|
|
|
failure:(void (^)())failureBlock
|
2017-08-01 16:51:01 +02:00
|
|
|
{
|
|
|
|
OWSAssert(successBlock);
|
|
|
|
OWSAssert(failureBlock);
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
2017-08-14 17:31:43 +02:00
|
|
|
NSData *_Nullable encryptedPaddedName = [self encryptProfileNameWithUnpaddedName:localProfileName];
|
|
|
|
|
|
|
|
TSRequest *request = [OWSRequestBuilder profileNameSetRequestWithEncryptedPaddedName:encryptedPaddedName];
|
|
|
|
[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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:52:15 +02:00
|
|
|
#pragma mark - Profile Whitelist
|
|
|
|
|
|
|
|
- (void)addUserToProfileWhitelist:(NSString *)recipientId
|
|
|
|
{
|
2017-08-03 18:05:53 +02:00
|
|
|
OWSAssert([NSThread isMainThread]);
|
2017-08-01 18:52:15 +02:00
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
[self.dbConnection setBool:YES forKey:recipientId inCollection:kOWSProfileManager_UserWhitelistCollection];
|
2017-08-01 20:56:53 +02:00
|
|
|
self.userProfileWhitelistCache[recipientId] = @(YES);
|
2017-08-01 18:52:15 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2017-08-02 19:12:26 +02:00
|
|
|
[transaction setObject:@(YES) forKey:recipientId inCollection:kOWSProfileManager_UserWhitelistCollection];
|
2017-08-03 18:05:53 +02:00
|
|
|
self.userProfileWhitelistCache[recipientId] = @(YES);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:52:15 +02:00
|
|
|
- (BOOL)isUserInProfileWhitelist:(NSString *)recipientId
|
|
|
|
{
|
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
|
2017-08-01 20:56:53 +02:00
|
|
|
NSNumber *_Nullable value = self.userProfileWhitelistCache[recipientId];
|
2017-08-01 19:53:51 +02:00
|
|
|
if (value) {
|
|
|
|
return [value boolValue];
|
|
|
|
}
|
2017-08-01 20:56:53 +02:00
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
value = @([self.dbConnection hasObjectForKey:recipientId inCollection:kOWSProfileManager_UserWhitelistCollection]);
|
2017-08-01 20:56:53 +02:00
|
|
|
self.userProfileWhitelistCache[recipientId] = value;
|
|
|
|
return [value boolValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addGroupIdToProfileWhitelist:(NSData *)groupId
|
|
|
|
{
|
|
|
|
OWSAssert(groupId.length > 0);
|
|
|
|
|
|
|
|
NSString *groupIdKey = [groupId hexadecimalString];
|
2017-08-02 19:12:26 +02:00
|
|
|
[self.dbConnection setObject:@(1) forKey:groupIdKey inCollection:kOWSProfileManager_GroupWhitelistCollection];
|
2017-08-01 20:56:53 +02:00
|
|
|
self.groupProfileWhitelistCache[groupIdKey] = @(YES);
|
|
|
|
}
|
|
|
|
|
2017-08-03 23:43:21 +02:00
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:56:53 +02:00
|
|
|
- (BOOL)isGroupIdInProfileWhitelist:(NSData *)groupId
|
|
|
|
{
|
|
|
|
OWSAssert(groupId.length > 0);
|
|
|
|
|
|
|
|
NSString *groupIdKey = [groupId hexadecimalString];
|
|
|
|
NSNumber *_Nullable value = self.groupProfileWhitelistCache[groupIdKey];
|
|
|
|
if (value) {
|
|
|
|
return [value boolValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
value =
|
2017-08-02 19:12:26 +02:00
|
|
|
@(nil != [self.dbConnection objectForKey:groupIdKey inCollection:kOWSProfileManager_GroupWhitelistCollection]);
|
2017-08-01 20:56:53 +02:00
|
|
|
self.groupProfileWhitelistCache[groupIdKey] = value;
|
2017-08-01 19:53:51 +02:00
|
|
|
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;
|
2017-08-02 19:12:26 +02:00
|
|
|
return [self isGroupIdInProfileWhitelist:groupId];
|
2017-08-03 18:05:53 +02:00
|
|
|
} else {
|
|
|
|
NSString *recipientId = thread.contactIdentifier;
|
2017-08-02 19:12:26 +02:00
|
|
|
return [self isUserInProfileWhitelist:recipientId];
|
2017-08-03 18:05:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 19:53:51 +02:00
|
|
|
- (void)setContactRecipientIds:(NSArray<NSString *> *)contactRecipientIds
|
|
|
|
{
|
2017-08-03 18:05:53 +02:00
|
|
|
OWSAssert([NSThread isMainThread]);
|
2017-08-01 19:53:51 +02:00
|
|
|
OWSAssert(contactRecipientIds);
|
2017-08-02 19:12:26 +02:00
|
|
|
|
2017-08-01 19:53:51 +02:00
|
|
|
// 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];
|
2017-08-01 18:52:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 15:27:29 +02:00
|
|
|
#pragma mark - Other User's Profiles
|
2017-08-01 18:52:15 +02:00
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
- (void)setProfileKeyData:(NSData *)profileKeyData forRecipientId:(NSString *)recipientId;
|
2017-08-01 18:52:15 +02:00
|
|
|
{
|
2017-08-14 17:31:43 +02:00
|
|
|
OWSAES128Key *_Nullable profileKey = [OWSAES128Key keyWithData:profileKeyData];
|
|
|
|
if (profileKey == nil) {
|
|
|
|
OWSFail(@"Failed to make profile key for key data");
|
2017-08-01 19:53:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-08-14 17:31:43 +02:00
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2017-08-04 16:16:17 +02:00
|
|
|
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
|
2017-08-02 19:12:26 +02:00
|
|
|
OWSAssert(userProfile);
|
2017-08-14 17:31:43 +02:00
|
|
|
if (userProfile.profileKey && [userProfile.profileKey.keyData isEqual:profileKey.keyData]) {
|
2017-08-02 19:12:26 +02:00
|
|
|
// Ignore redundant update.
|
|
|
|
return;
|
|
|
|
}
|
2017-08-02 15:27:29 +02:00
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
userProfile.profileKey = profileKey;
|
2017-08-02 18:03:06 +02:00
|
|
|
|
2017-08-04 18:57:38 +02:00
|
|
|
// Clear profile state.
|
|
|
|
userProfile.profileName = nil;
|
2017-08-14 20:51:51 +02:00
|
|
|
userProfile.avatarUrlPath = nil;
|
2017-08-04 18:57:38 +02:00
|
|
|
userProfile.avatarFileName = nil;
|
|
|
|
|
2017-08-02 19:12:26 +02:00
|
|
|
[self saveUserProfile:userProfile];
|
2017-08-03 17:13:40 +02:00
|
|
|
|
|
|
|
[self refreshProfileForRecipientId:recipientId ignoreThrottling:YES];
|
2017-08-02 19:12:26 +02:00
|
|
|
});
|
2017-08-01 18:52:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
- (nullable OWSAES128Key *)profileKeyForRecipientId:(NSString *)recipientId
|
2017-08-01 18:52:15 +02:00
|
|
|
{
|
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
|
2017-08-04 16:16:17 +02:00
|
|
|
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
|
2017-08-02 18:03:06 +02:00
|
|
|
OWSAssert(userProfile);
|
|
|
|
return userProfile.profileKey;
|
2017-08-01 18:52:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 15:27:29 +02:00
|
|
|
- (nullable NSString *)profileNameForRecipientId:(NSString *)recipientId
|
|
|
|
{
|
2017-08-02 16:36:54 +02:00
|
|
|
OWSAssert([NSThread isMainThread]);
|
2017-08-02 15:27:29 +02:00
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
[self refreshProfileForRecipientId:recipientId];
|
2017-08-02 15:27:29 +02:00
|
|
|
|
2017-08-04 16:16:17 +02:00
|
|
|
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
|
2017-08-02 16:36:54 +02:00
|
|
|
return userProfile.profileName;
|
2017-08-02 15:27:29 +02:00
|
|
|
}
|
|
|
|
|
2017-08-15 18:49:27 +02:00
|
|
|
- (nullable NSData *)profileAvatarDataForRecipientId:(NSString *)recipientId
|
|
|
|
{
|
|
|
|
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
|
|
|
|
if (userProfile.avatarFileName.length > 0) {
|
|
|
|
return [self loadProfileDataWithFilename:userProfile.avatarFileName];
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2017-08-02 15:27:29 +02:00
|
|
|
- (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId
|
|
|
|
{
|
2017-08-02 16:36:54 +02:00
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
[self refreshProfileForRecipientId:recipientId];
|
2017-08-02 16:36:54 +02:00
|
|
|
|
|
|
|
UIImage *_Nullable image = [self.otherUsersProfileAvatarImageCache objectForKey:recipientId];
|
|
|
|
if (image) {
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:16:17 +02:00
|
|
|
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
|
2017-08-14 17:31:43 +02:00
|
|
|
if (userProfile.avatarFileName.length > 0) {
|
2017-08-02 16:36:54 +02:00
|
|
|
image = [self loadProfileAvatarWithFilename:userProfile.avatarFileName];
|
|
|
|
if (image) {
|
|
|
|
[self.otherUsersProfileAvatarImageCache setObject:image forKey:recipientId];
|
|
|
|
}
|
2017-08-14 20:51:51 +02:00
|
|
|
} else if (userProfile.avatarUrlPath.length > 0) {
|
2017-08-04 21:20:33 +02:00
|
|
|
[self downloadAvatarForUserProfile:userProfile];
|
2017-08-02 16:36:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return image;
|
2017-08-02 15:27:29 +02:00
|
|
|
}
|
|
|
|
|
2017-08-04 21:20:33 +02:00
|
|
|
- (void)downloadAvatarForUserProfile:(UserProfile *)userProfile
|
2017-08-03 17:13:40 +02:00
|
|
|
{
|
2017-08-04 21:20:33 +02:00
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
OWSAssert(userProfile);
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-14 20:51:51 +02:00
|
|
|
if (userProfile.avatarUrlPath.length < 1) {
|
|
|
|
OWSFail(@"%@ Malformed avatar URL: %@", self.tag, userProfile.avatarUrlPath);
|
2017-08-04 21:20:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-14 20:51:51 +02:00
|
|
|
if (userProfile.profileKey.keyData.length < 1 || userProfile.avatarUrlPath.length < 1) {
|
2017-08-04 21:20:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
OWSAES128Key *profileKeyAtStart = userProfile.profileKey;
|
|
|
|
|
|
|
|
NSString *fileName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:@"jpg"];
|
2017-08-04 21:20:33 +02:00
|
|
|
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];
|
|
|
|
|
2017-08-14 20:51:51 +02:00
|
|
|
NSURL *avatarUrlPath = [NSURL URLWithString:userProfile.avatarUrlPath relativeToURL:self.avatarHTTPManager.baseURL];
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:avatarUrlPath];
|
2017-08-14 17:31:43 +02:00
|
|
|
NSURLSessionDownloadTask *downloadTask = [self.avatarHTTPManager downloadTaskWithRequest:request
|
|
|
|
progress:^(NSProgress *_Nonnull downloadProgress) {
|
|
|
|
DDLogVerbose(@"%@ Downloading avatar for %@", self.tag, userProfile.recipientId);
|
|
|
|
}
|
|
|
|
destination:^NSURL *_Nonnull(NSURL *_Nonnull targetPath, NSURLResponse *_Nonnull response) {
|
2017-08-04 21:20:33 +02:00
|
|
|
return [NSURL fileURLWithPath:tempFilePath];
|
|
|
|
}
|
2017-08-14 17:31:43 +02:00
|
|
|
completionHandler:^(
|
|
|
|
NSURLResponse *_Nonnull response, NSURL *_Nullable filePathParam, NSError *_Nullable error) {
|
2017-08-04 21:20:33 +02:00
|
|
|
// 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]);
|
2017-08-14 17:31:43 +02:00
|
|
|
NSData *_Nullable decryptedData = [self decryptProfileData:encryptedData profileKey:profileKeyAtStart];
|
2017-08-04 21:20:33 +02:00
|
|
|
UIImage *_Nullable image = nil;
|
|
|
|
if (decryptedData) {
|
|
|
|
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];
|
2017-08-14 17:31:43 +02:00
|
|
|
if (currentUserProfile.profileKey.keyData.length < 1
|
2017-08-04 21:20:33 +02:00
|
|
|
|| ![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];
|
2017-08-03 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 18:03:06 +02:00
|
|
|
- (void)refreshProfileForRecipientId:(NSString *)recipientId
|
2017-08-03 17:13:40 +02:00
|
|
|
{
|
|
|
|
[self refreshProfileForRecipientId:recipientId ignoreThrottling:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)refreshProfileForRecipientId:(NSString *)recipientId ignoreThrottling:(BOOL)ignoreThrottling
|
2017-08-02 15:27:29 +02:00
|
|
|
{
|
2017-08-02 18:03:06 +02:00
|
|
|
OWSAssert([NSThread isMainThread]);
|
2017-08-02 15:27:29 +02:00
|
|
|
OWSAssert(recipientId.length > 0);
|
|
|
|
|
2017-08-04 16:16:17 +02:00
|
|
|
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
|
2017-08-02 15:27:29 +02:00
|
|
|
|
2017-08-03 17:13:40 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-02 16:36:54 +02:00
|
|
|
// 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];
|
2017-08-02 18:03:06 +02:00
|
|
|
|
|
|
|
[self saveUserProfile:userProfile];
|
2017-08-02 16:36:54 +02:00
|
|
|
|
2017-08-03 17:13:40 +02:00
|
|
|
[ProfileFetcherJob runWithRecipientId:recipientId
|
|
|
|
networkManager:self.networkManager
|
|
|
|
ignoreThrottling:ignoreThrottling];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateProfileForRecipientId:(NSString *)recipientId
|
2017-08-14 17:31:43 +02:00
|
|
|
profileNameEncrypted:(nullable NSData *)profileNameEncrypted
|
2017-08-14 20:51:51 +02:00
|
|
|
avatarUrlPath:(nullable NSString *)avatarUrlPath;
|
2017-08-03 17:13:40 +02:00
|
|
|
{
|
|
|
|
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-03 17:13:40 +02:00
|
|
|
|
2017-08-04 18:46:16 +02:00
|
|
|
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
|
|
|
|
if (!userProfile.profileKey) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-04 18:46:16 +02:00
|
|
|
NSString *_Nullable profileName =
|
2017-08-14 17:31:43 +02:00
|
|
|
[self decryptProfileNameData:profileNameEncrypted profileKey:userProfile.profileKey];
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-14 20:51:51 +02:00
|
|
|
BOOL isAvatarSame = [self isNullableStringEqual:userProfile.avatarUrlPath toString:avatarUrlPath];
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-04 18:46:16 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
userProfile.profileName = profileName;
|
2017-08-14 20:51:51 +02:00
|
|
|
userProfile.avatarUrlPath = avatarUrlPath;
|
2017-08-04 18:46:16 +02:00
|
|
|
|
|
|
|
if (!isAvatarSame) {
|
|
|
|
// Evacuate avatar image cache.
|
|
|
|
[self.otherUsersProfileAvatarImageCache removeObjectForKey:recipientId];
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-14 20:51:51 +02:00
|
|
|
if (avatarUrlPath) {
|
2017-08-04 21:20:33 +02:00
|
|
|
[self downloadAvatarForUserProfile:userProfile];
|
2017-08-04 18:46:16 +02:00
|
|
|
}
|
2017-08-03 19:43:03 +02:00
|
|
|
}
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-04 18:46:16 +02:00
|
|
|
userProfile.lastUpdateDate = [NSDate new];
|
2017-08-03 19:43:03 +02:00
|
|
|
|
2017-08-04 18:46:16 +02:00
|
|
|
[self saveUserProfile:userProfile];
|
|
|
|
});
|
2017-08-03 17:13:40 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
- (nullable NSData *)encryptProfileData:(nullable NSData *)encryptedData profileKey:(OWSAES128Key *)profileKey
|
2017-08-03 17:13:40 +02:00
|
|
|
{
|
2017-08-14 17:31:43 +02:00
|
|
|
OWSAssert(profileKey.keyData.length == kAES128_KeyByteLength);
|
2017-08-03 17:13:40 +02:00
|
|
|
|
|
|
|
if (!encryptedData) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
return [Cryptography encryptAESGCMWithData:encryptedData key:profileKey];
|
2017-08-03 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
- (nullable NSData *)decryptProfileData:(nullable NSData *)encryptedData profileKey:(OWSAES128Key *)profileKey
|
2017-08-03 17:13:40 +02:00
|
|
|
{
|
2017-08-14 17:31:43 +02:00
|
|
|
OWSAssert(profileKey.keyData.length == kAES128_KeyByteLength);
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
if (!encryptedData) {
|
2017-08-03 17:13:40 +02:00
|
|
|
return nil;
|
|
|
|
}
|
2017-08-14 17:31:43 +02:00
|
|
|
|
|
|
|
return [Cryptography decryptAESGCMWithData:encryptedData key:profileKey];
|
2017-08-03 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
- (nullable NSString *)decryptProfileNameData:(nullable NSData *)encryptedData profileKey:(OWSAES128Key *)profileKey
|
2017-08-03 17:13:40 +02:00
|
|
|
{
|
2017-08-14 17:31:43 +02:00
|
|
|
OWSAssert(profileKey.keyData.length == kAES128_KeyByteLength);
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
NSData *_Nullable decryptedData = [self decryptProfileData:encryptedData profileKey:profileKey];
|
|
|
|
if (decryptedData.length < 1) {
|
2017-08-03 17:13:40 +02:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
// Unpad profile name.
|
|
|
|
NSUInteger unpaddedLength = 0;
|
|
|
|
const char *bytes = decryptedData.bytes;
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
// Work through the bytes until we encounter our first
|
|
|
|
// padding byte (our padding scheme is NULL bytes)
|
|
|
|
for (NSUInteger i = 0; i < decryptedData.length; i++) {
|
|
|
|
if (bytes[i] == 0x00) {
|
|
|
|
break;
|
2017-08-03 17:13:40 +02:00
|
|
|
}
|
2017-08-14 17:31:43 +02:00
|
|
|
unpaddedLength = i + 1;
|
2017-08-03 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
NSData *unpaddedData = [decryptedData subdataWithRange:NSMakeRange(0, unpaddedLength)];
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
return [[NSString alloc] initWithData:unpaddedData encoding:NSUTF8StringEncoding];
|
2017-08-03 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
- (nullable NSData *)encryptProfileData:(nullable NSData *)data
|
2017-08-03 17:13:40 +02:00
|
|
|
{
|
2017-08-14 17:31:43 +02:00
|
|
|
return [self encryptProfileData:data profileKey:self.localProfileKey];
|
2017-08-03 17:13:40 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
- (nullable NSData *)encryptProfileNameWithUnpaddedName:(NSString *)name
|
2017-08-03 17:13:40 +02:00
|
|
|
{
|
2017-08-14 17:31:43 +02:00
|
|
|
if (name.length == 0) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSData *nameData = [name dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
if (nameData.length > kOWSProfileManager_NameDataLength) {
|
|
|
|
OWSFail(@"%@ name data is too long with length:%lu", self.tag, (unsigned long)nameData.length);
|
|
|
|
return nil;
|
|
|
|
}
|
2017-08-03 17:13:40 +02:00
|
|
|
|
2017-08-14 17:31:43 +02:00
|
|
|
NSUInteger paddingByteCount = kOWSProfileManager_NameDataLength - nameData.length;
|
|
|
|
|
|
|
|
NSMutableData *paddedNameData = [nameData mutableCopy];
|
2017-08-14 20:51:51 +02:00
|
|
|
// Since we want all encrypted profile names to be the same length on the server, we use `increaseLengthBy`
|
|
|
|
// to pad out any remaining length with 0 bytes.
|
2017-08-14 17:31:43 +02:00
|
|
|
[paddedNameData increaseLengthBy:paddingByteCount];
|
|
|
|
OWSAssert(paddedNameData.length == kOWSProfileManager_NameDataLength);
|
|
|
|
|
|
|
|
return [self encryptProfileData:[paddedNameData copy] profileKey:self.localProfileKey];
|
2017-08-02 16:36:54 +02:00
|
|
|
}
|
2017-08-02 15:27:29 +02:00
|
|
|
|
2017-07-31 23:49:52 +02:00
|
|
|
#pragma mark - Avatar Disk Cache
|
|
|
|
|
2017-08-15 18:49:27 +02:00
|
|
|
- (nullable NSData *)loadProfileDataWithFilename:(NSString *)filename
|
2017-07-31 23:49:52 +02:00
|
|
|
{
|
2017-08-15 18:49:27 +02:00
|
|
|
OWSAssert(filename.length > 0);
|
2017-08-01 16:51:01 +02:00
|
|
|
|
2017-08-15 18:49:27 +02:00
|
|
|
NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:filename];
|
|
|
|
return [NSData dataWithContentsOfFile:filePath];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)filename
|
|
|
|
{
|
|
|
|
OWSAssert(filename.length > 0);
|
|
|
|
|
|
|
|
NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:filename];
|
|
|
|
UIImage *_Nullable image = [UIImage imageWithData:[self loadProfileDataWithFilename:filename]];
|
2017-07-31 23:49:52 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-01 17:58:51 +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
|
2017-08-03 19:39:34 +02:00
|
|
|
{
|
|
|
|
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
|