|
|
|
@ -26,20 +26,20 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
// UserProfile properties may be read from any thread, but should
|
|
|
|
|
// only be mutated when synchronized on the profile manager.
|
|
|
|
|
@interface UserProfile : TSYapDatabaseObject
|
|
|
|
|
@interface UserProfile : TSYapDatabaseObject <NSCopying>
|
|
|
|
|
|
|
|
|
|
@property (atomic, readonly) NSString *recipientId;
|
|
|
|
|
@property (atomic, nullable) OWSAES256Key *profileKey;
|
|
|
|
|
@property (nonatomic, nullable) NSString *profileName;
|
|
|
|
|
@property (nonatomic, nullable) NSString *avatarUrlPath;
|
|
|
|
|
@property (atomic, nullable) NSString *profileName;
|
|
|
|
|
@property (atomic, nullable) NSString *avatarUrlPath;
|
|
|
|
|
// This filename is relative to OWSProfileManager.profileAvatarsDirPath.
|
|
|
|
|
@property (nonatomic, nullable) NSString *avatarFileName;
|
|
|
|
|
@property (atomic, nullable) NSString *avatarFileName;
|
|
|
|
|
|
|
|
|
|
// This should reflect when either:
|
|
|
|
|
//
|
|
|
|
|
// * The last successful update finished.
|
|
|
|
|
// * The current in-flight update began.
|
|
|
|
|
@property (nonatomic, nullable) NSDate *lastUpdateDate;
|
|
|
|
|
@property (atomic, nullable) NSDate *lastUpdateDate;
|
|
|
|
|
|
|
|
|
|
- (instancetype)init NS_UNAVAILABLE;
|
|
|
|
|
|
|
|
|
@ -49,6 +49,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@implementation UserProfile
|
|
|
|
|
|
|
|
|
|
@synthesize profileName = _profileName;
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithRecipientId:(NSString *)recipientId
|
|
|
|
|
{
|
|
|
|
|
self = [super initWithUniqueId:recipientId];
|
|
|
|
@ -63,23 +65,67 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (nullable NSString *)profileName
|
|
|
|
|
{
|
|
|
|
|
@synchronized(self)
|
|
|
|
|
{
|
|
|
|
|
return _profileName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setProfileName:(nullable NSString *)profileName
|
|
|
|
|
{
|
|
|
|
|
@synchronized(self)
|
|
|
|
|
{
|
|
|
|
|
_profileName = [profileName ows_stripped];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - NSCopying
|
|
|
|
|
|
|
|
|
|
- (id)copyWithZone:(nullable NSZone *)zone
|
|
|
|
|
{
|
|
|
|
|
UserProfile *copy = [[UserProfile alloc] initWithRecipientId:self.recipientId];
|
|
|
|
|
copy.profileKey = self.profileKey;
|
|
|
|
|
copy.profileName = self.profileName;
|
|
|
|
|
copy.avatarUrlPath = self.avatarUrlPath;
|
|
|
|
|
copy.avatarFileName = self.avatarFileName;
|
|
|
|
|
copy.lastUpdateDate = self.lastUpdateDate;
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - NSObject
|
|
|
|
|
|
|
|
|
|
- (BOOL)isEqual:(UserProfile *)other
|
|
|
|
|
- (BOOL)areEqualStrings:(NSString *)left other:(NSString *)right
|
|
|
|
|
{
|
|
|
|
|
if (!left && !right) {
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
if (!left || !right) {
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return [left isEqualToString:right];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)areEqualKeys:(OWSAES256Key *)left other:(OWSAES256Key *)right
|
|
|
|
|
{
|
|
|
|
|
return ([other isKindOfClass:[UserProfile class]] && [self.recipientId isEqualToString:other.recipientId] &&
|
|
|
|
|
[self.profileName isEqualToString:other.profileName] && [self.avatarUrlPath isEqualToString:other.avatarUrlPath] &&
|
|
|
|
|
[self.avatarFileName isEqualToString:other.avatarFileName]);
|
|
|
|
|
if (!left && !right) {
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
if (!left || !right) {
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return [left.keyData isEqual:right.keyData];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSUInteger)hash
|
|
|
|
|
- (BOOL)isEqual:(UserProfile *)other
|
|
|
|
|
{
|
|
|
|
|
return self.recipientId.hash ^ self.profileName.hash ^ self.avatarUrlPath.hash ^ self.avatarFileName.hash;
|
|
|
|
|
// Don't bother comparing lastUpdateDate property.
|
|
|
|
|
return ([other isKindOfClass:[UserProfile class]] && [self areEqualStrings:self.recipientId other:other.recipientId]
|
|
|
|
|
&& [self areEqualStrings:self.profileName other:other.profileName] &&
|
|
|
|
|
[self areEqualKeys:self.profileKey other:other.profileKey] &&
|
|
|
|
|
[self areEqualStrings:self.avatarUrlPath other:other.avatarUrlPath] &&
|
|
|
|
|
[self areEqualStrings:self.avatarFileName other:other.avatarFileName]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
@ -234,11 +280,19 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(userProfile);
|
|
|
|
|
|
|
|
|
|
// Make a copy to use inside the transaction.
|
|
|
|
|
// To avoid deadlock, we want to avoid creating a new transaction while sync'd on self.
|
|
|
|
|
UserProfile *userProfileCopy;
|
|
|
|
|
@synchronized(self)
|
|
|
|
|
{
|
|
|
|
|
userProfileCopy = [userProfile copy];
|
|
|
|
|
// Other threads may modify this profile's properties
|
|
|
|
|
OWSAssert([userProfile isEqual:userProfileCopy]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure to save on the local db connection for consistency.
|
|
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
[userProfile saveWithTransaction:transaction];
|
|
|
|
|
[userProfileCopy saveWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
BOOL isLocalUserProfile = userProfile == self.localUserProfile;
|
|
|
|
@ -250,8 +304,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
|
|
|
|
|
messageSender:self.messageSender
|
|
|
|
|
profileManager:self];
|
|
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationNameAsync:kNSNotificationName_LocalProfileDidChange
|
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:kNSNotificationName_LocalProfileDidChange
|
|
|
|
|
object:nil
|
|
|
|
|
userInfo:nil];
|
|
|
|
|
} else {
|
|
|
|
@ -270,7 +323,6 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)ensureLocalProfileCached
|
|
|
|
|
{
|
|
|
|
@ -295,9 +347,6 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
|
|
|
|
|
DDLogInfo(@"%@ Building local profile.", self.tag);
|
|
|
|
|
_localUserProfile = [[UserProfile alloc] initWithRecipientId:kLocalProfileUniqueId];
|
|
|
|
|
_localUserProfile.profileKey = [OWSAES256Key generateRandomKey];
|
|
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
|
[_localUserProfile saveWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
// Sync local profile to any linked device
|
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
|
@synchronized(self)
|
|
|
|
|