Handle receiving of sender display name

pull/26/head
Niels Andriesse 6 years ago
parent c60f4cda0d
commit 1462a5cb6b

@ -17,6 +17,7 @@ extern const NSUInteger kOWSProfileManager_MaxAvatarDiameter;
@class OWSPrimaryStorage; @class OWSPrimaryStorage;
@class TSNetworkManager; @class TSNetworkManager;
@class TSThread; @class TSThread;
@class YapDatabaseReadWriteTransaction;
// This class can be safely accessed and used from any thread. // This class can be safely accessed and used from any thread.
@interface OWSProfileManager : NSObject <ProfileManagerProtocol> @interface OWSProfileManager : NSObject <ProfileManagerProtocol>
@ -81,6 +82,8 @@ extern const NSUInteger kOWSProfileManager_MaxAvatarDiameter;
profileNameEncrypted:(nullable NSData *)profileNameEncrypted profileNameEncrypted:(nullable NSData *)profileNameEncrypted
avatarUrlPath:(nullable NSString *)avatarUrlPath; avatarUrlPath:(nullable NSString *)avatarUrlPath;
- (void)setDisplayNameForContactWithID:(NSString *)contactID to:(NSString *)displayName with:(YapDatabaseReadWriteTransaction *)transaction;
#pragma mark - User Interface #pragma mark - User Interface
- (void)presentAddThreadToProfileWhitelist:(TSThread *)thread - (void)presentAddThreadToProfileWhitelist:(TSThread *)thread

@ -1325,6 +1325,12 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
}); });
} }
- (void)setDisplayNameForContactWithID:(NSString *)contactID to:(NSString *)displayName with:(YapDatabaseReadWriteTransaction *)transaction
{
OWSUserProfile *userProfile = [OWSUserProfile getOrBuildUserProfileForRecipientId:contactID transaction:transaction];
[userProfile updateWithProfileName:displayName avatarUrlPath:@"" avatarFileName:@"" transaction:transaction completion:nil];
}
- (BOOL)isNullableDataEqual:(NSData *_Nullable)left toData:(NSData *_Nullable)right - (BOOL)isNullableDataEqual:(NSData *_Nullable)left toData:(NSData *_Nullable)right
{ {
if (left == nil && right == nil) { if (left == nil && right == nil) {

@ -37,10 +37,18 @@ extern NSString *const kLocalProfileUniqueId;
+ (OWSUserProfile *)getOrBuildUserProfileForRecipientId:(NSString *)recipientId + (OWSUserProfile *)getOrBuildUserProfileForRecipientId:(NSString *)recipientId
dbConnection:(YapDatabaseConnection *)dbConnection; dbConnection:(YapDatabaseConnection *)dbConnection;
+ (OWSUserProfile *)getOrBuildUserProfileForRecipientId:(NSString *)recipientId transaction:(YapDatabaseReadTransaction *)transaction;
+ (BOOL)localUserProfileExists:(YapDatabaseConnection *)dbConnection; + (BOOL)localUserProfileExists:(YapDatabaseConnection *)dbConnection;
#pragma mark - Update With... Methods #pragma mark - Update With... Methods
- (void)updateWithProfileName:(nullable NSString *)profileName
avatarUrlPath:(nullable NSString *)avatarUrlPath
avatarFileName:(nullable NSString *)avatarFileName
transaction:(YapDatabaseReadWriteTransaction *)transaction
completion:(nullable OWSUserProfileCompletion)completion;
- (void)updateWithProfileName:(nullable NSString *)profileName - (void)updateWithProfileName:(nullable NSString *)profileName
avatarUrlPath:(nullable NSString *)avatarUrlPath avatarUrlPath:(nullable NSString *)avatarUrlPath
avatarFileName:(nullable NSString *)avatarFileName avatarFileName:(nullable NSString *)avatarFileName

@ -54,19 +54,25 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
+ (OWSUserProfile *)getOrBuildUserProfileForRecipientId:(NSString *)recipientId + (OWSUserProfile *)getOrBuildUserProfileForRecipientId:(NSString *)recipientId
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
{ {
OWSAssertDebug(recipientId.length > 0);
__block OWSUserProfile *userProfile; __block OWSUserProfile *userProfile;
[dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
userProfile = [OWSUserProfile fetchObjectWithUniqueID:recipientId transaction:transaction]; userProfile = [OWSUserProfile getOrBuildUserProfileForRecipientId:recipientId transaction:transaction];
}]; }];
return userProfile;
}
+ (OWSUserProfile *)getOrBuildUserProfileForRecipientId:(NSString *)recipientId transaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssertDebug(recipientId.length > 0);
OWSUserProfile *userProfile = [OWSUserProfile fetchObjectWithUniqueID:recipientId transaction:transaction];
if (!userProfile) { if (!userProfile) {
userProfile = [[OWSUserProfile alloc] initWithRecipientId:recipientId]; userProfile = [[OWSUserProfile alloc] initWithRecipientId:recipientId];
if ([recipientId isEqualToString:kLocalProfileUniqueId]) { if ([recipientId isEqualToString:kLocalProfileUniqueId]) {
[userProfile updateWithProfileKey:[OWSAES256Key generateRandomKey] [userProfile updateWithProfileKey:[OWSAES256Key generateRandomKey]
dbConnection:dbConnection dbConnection:transaction.connection
completion:nil]; completion:nil];
} }
} }
@ -180,38 +186,46 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
functionName:(const char *)functionName functionName:(const char *)functionName
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion completion:(nullable OWSUserProfileCompletion)completion
{ {
[dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChanges:changeBlock functionName:functionName transaction:transaction completion:completion];
}];
}
- (void)applyChanges:(void (^)(id))changeBlock
functionName:(const char *)functionName
transaction:(YapDatabaseReadWriteTransaction *)transaction
completion:(nullable OWSUserProfileCompletion)completion
{
// self might be the latest instance, so take a "before" snapshot // self might be the latest instance, so take a "before" snapshot
// before any changes have been made. // before any changes have been made.
__block NSDictionary *beforeSnapshot = [self.dictionaryValue copy]; __block NSDictionary *beforeSnapshot = [self.dictionaryValue copy];
changeBlock(self); changeBlock(self);
__block BOOL didChange = YES; BOOL didChange = YES;
[dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { NSString *collection = [[self class] collection];
NSString *collection = [[self class] collection]; OWSUserProfile *_Nullable latestInstance = [transaction objectForKey:self.uniqueId inCollection:collection];
OWSUserProfile *_Nullable latestInstance = [transaction objectForKey:self.uniqueId inCollection:collection]; if (latestInstance) {
if (latestInstance) { // If self is NOT the latest instance, take a new "before" snapshot
// If self is NOT the latest instance, take a new "before" snapshot // before updating.
// before updating. if (self != latestInstance) {
if (self != latestInstance) { beforeSnapshot = [latestInstance.dictionaryValue copy];
beforeSnapshot = [latestInstance.dictionaryValue copy]; }
}
changeBlock(latestInstance); changeBlock(latestInstance);
NSDictionary *afterSnapshot = [latestInstance.dictionaryValue copy]; NSDictionary *afterSnapshot = [latestInstance.dictionaryValue copy];
if ([beforeSnapshot isEqual:afterSnapshot]) { if ([beforeSnapshot isEqual:afterSnapshot]) {
OWSLogVerbose(@"Ignoring redundant update in %s: %@", functionName, self.debugDescription); OWSLogVerbose(@"Ignoring redundant update in %s: %@", functionName, self.debugDescription);
didChange = NO; didChange = NO;
} else {
[latestInstance saveWithTransaction:transaction];
}
} else { } else {
[self saveWithTransaction:transaction]; [latestInstance saveWithTransaction:transaction];
} }
}]; } else {
[self saveWithTransaction:transaction];
}
if (completion) { if (completion) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), completion); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), completion);
@ -252,12 +266,30 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
}); });
} }
- (void)updateWithProfileName:(nullable NSString *)profileName
avatarUrlPath:(nullable NSString *)avatarUrlPath
avatarFileName:(nullable NSString *)avatarFileName
transaction:(YapDatabaseReadWriteTransaction *)transaction
completion:(nullable OWSUserProfileCompletion)completion
{
[self applyChanges:^(OWSUserProfile *userProfile) {
[userProfile setProfileName:[profileName ows_stripped]];
// Always setAvatarUrlPath: before you setAvatarFileName: since
// setAvatarUrlPath: may clear the avatar filename.
[userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setAvatarFileName:avatarFileName];
}
functionName:__PRETTY_FUNCTION__
transaction:transaction
completion:completion];
}
- (void)updateWithProfileName:(nullable NSString *)profileName - (void)updateWithProfileName:(nullable NSString *)profileName
avatarUrlPath:(nullable NSString *)avatarUrlPath avatarUrlPath:(nullable NSString *)avatarUrlPath
avatarFileName:(nullable NSString *)avatarFileName avatarFileName:(nullable NSString *)avatarFileName
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion completion:(nullable OWSUserProfileCompletion)completion
{ {
[self applyChanges:^(OWSUserProfile *userProfile) { [self applyChanges:^(OWSUserProfile *userProfile) {
[userProfile setProfileName:[profileName ows_stripped]]; [userProfile setProfileName:[profileName ows_stripped]];
// Always setAvatarUrlPath: before you setAvatarFileName: since // Always setAvatarUrlPath: before you setAvatarFileName: since

@ -8,6 +8,7 @@
#import "OWSMessageSender.h" #import "OWSMessageSender.h"
#import "OWSOutgoingSyncMessage.h" #import "OWSOutgoingSyncMessage.h"
#import "OWSPrimaryStorage.h" #import "OWSPrimaryStorage.h"
#import "ProfileManagerProtocol.h"
#import "ProtoUtils.h" #import "ProtoUtils.h"
#import "SSKEnvironment.h" #import "SSKEnvironment.h"
#import "SignalRecipient.h" #import "SignalRecipient.h"
@ -1102,13 +1103,17 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
[ProtoUtils addLocalProfileKeyIfNecessary:self.thread recipientId:recipientId dataMessageBuilder:builder]; [ProtoUtils addLocalProfileKeyIfNecessary:self.thread recipientId:recipientId dataMessageBuilder:builder];
SSKProtoDataMessageContactBuilder *profileBuilder = [SSKProtoDataMessageContact builder]; id<ProfileManagerProtocol> profileManager = SSKEnvironment.shared.profileManager;
SSKProtoDataMessageContactNameBuilder *nameBuilder = [SSKProtoDataMessageContactName builder]; NSString *displayName = [profileManager localProfileName];
[nameBuilder setDisplayName:@"Test"]; // TODO: Use actual name if (displayName != nil) {
SSKProtoDataMessageContactName *name = [nameBuilder buildIgnoringErrors]; SSKProtoDataMessageContactBuilder *profileBuilder = [SSKProtoDataMessageContact builder];
[profileBuilder setName:name]; SSKProtoDataMessageContactNameBuilder *nameBuilder = [SSKProtoDataMessageContactName builder];
SSKProtoDataMessageContact *profile = [profileBuilder buildIgnoringErrors]; [nameBuilder setDisplayName:displayName];
[builder setProfile:profile]; SSKProtoDataMessageContactName *name = [nameBuilder buildIgnoringErrors];
[profileBuilder setName:name];
SSKProtoDataMessageContact *profile = [profileBuilder buildIgnoringErrors];
[builder setProfile:profile];
}
NSError *error; NSError *error;
SSKProtoDataMessage *_Nullable dataProto = [builder buildAndReturnError:&error]; SSKProtoDataMessage *_Nullable dataProto = [builder buildAndReturnError:&error];

@ -1445,6 +1445,9 @@ NS_ASSUME_NONNULL_BEGIN
serverTimestamp:serverTimestamp serverTimestamp:serverTimestamp
wasReceivedByUD:wasReceivedByUD]; wasReceivedByUD:wasReceivedByUD];
NSString *displayName = dataMessage.profile.name.displayName;
[self.profileManager setDisplayNameForContactWithID:thread.contactIdentifier to:displayName with:transaction];
if (envelope.isPtpMessage) { incomingMessage.isP2P = YES; } if (envelope.isPtpMessage) { incomingMessage.isP2P = YES; }
NSArray<TSAttachmentPointer *> *attachmentPointers = NSArray<TSAttachmentPointer *> *attachmentPointers =
@ -1478,6 +1481,7 @@ NS_ASSUME_NONNULL_BEGIN
thread:thread thread:thread
envelope:envelope envelope:envelope
transaction:transaction]; transaction:transaction];
return incomingMessage; return incomingMessage;
} }
} }

@ -4,6 +4,7 @@
@class OWSAES256Key; @class OWSAES256Key;
@class TSThread; @class TSThread;
@class YapDatabaseReadWriteTransaction;
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -11,6 +12,8 @@ NS_ASSUME_NONNULL_BEGIN
- (OWSAES256Key *)localProfileKey; - (OWSAES256Key *)localProfileKey;
- (nullable NSString *)localProfileName;
- (nullable NSData *)profileKeyDataForRecipientId:(NSString *)recipientId; - (nullable NSData *)profileKeyDataForRecipientId:(NSString *)recipientId;
- (void)setProfileKeyData:(NSData *)profileKeyData forRecipientId:(NSString *)recipientId; - (void)setProfileKeyData:(NSData *)profileKeyData forRecipientId:(NSString *)recipientId;
@ -25,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)fetchProfileForRecipientId:(NSString *)recipientId; - (void)fetchProfileForRecipientId:(NSString *)recipientId;
- (void)setDisplayNameForContactWithID:(NSString *)contactID to:(NSString *)displayName with:(YapDatabaseReadWriteTransaction *)transaction;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

Loading…
Cancel
Save