From d7f275ce736cb9e874a6e17f13cd709dc4eb931b Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 2 Aug 2017 09:27:29 -0400 Subject: [PATCH] =?UTF-8?q?Add=20accessor=20for=20other=20users=E2=80=99?= =?UTF-8?q?=20profile=20names.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit // FREEBIE --- .../Messages/Interactions/TSOutgoingMessage.m | 2 - .../src/Messages/TSMessagesManager.m | 2 - .../src/Profiles/OWSProfilesManager.h | 10 +-- .../src/Profiles/OWSProfilesManager.m | 71 ++++++++++++++++--- 4 files changed, 67 insertions(+), 18 deletions(-) diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index 4682fce83..c22437580 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -464,7 +464,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec OWSAssert(contentBuilder); OWSAssert(recipient); -#ifndef SKIP_PROFILE_KEYS OWSAssert(OWSProfilesManager.sharedManager.localProfileKey.length > 0); BOOL shouldIncludeProfileKey = NO; @@ -498,7 +497,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec if (shouldIncludeProfileKey) { [contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey]; } -#endif } - (NSData *)buildPlainTextData:(SignalRecipient *)recipient diff --git a/SignalServiceKit/src/Messages/TSMessagesManager.m b/SignalServiceKit/src/Messages/TSMessagesManager.m index 9873c0818..e0b154dfa 100644 --- a/SignalServiceKit/src/Messages/TSMessagesManager.m +++ b/SignalServiceKit/src/Messages/TSMessagesManager.m @@ -496,14 +496,12 @@ NS_ASSUME_NONNULL_BEGIN OWSSignalServiceProtosContent *content = [OWSSignalServiceProtosContent parseFromData:plaintextData]; DDLogInfo(@"%@ handling content: ", self.tag, [self descriptionForContent:content]); -#ifndef SKIP_PROFILE_KEYS if ([content hasProfileKey]) { NSData *profileKey = [content profileKey]; NSString *recipientId = envelope.source; [OWSProfilesManager.sharedManager setProfileKey:profileKey forRecipientId:recipientId]; } -#endif if (content.hasSyncMessage) { [self handleIncomingEnvelope:envelope withSyncMessage:content.syncMessage]; diff --git a/SignalServiceKit/src/Profiles/OWSProfilesManager.h b/SignalServiceKit/src/Profiles/OWSProfilesManager.h index 0faa6ac13..d7cc6f9fe 100644 --- a/SignalServiceKit/src/Profiles/OWSProfilesManager.h +++ b/SignalServiceKit/src/Profiles/OWSProfilesManager.h @@ -5,9 +5,7 @@ NS_ASSUME_NONNULL_BEGIN extern NSString *const kNSNotificationName_LocalProfileDidChange; - -// TODO: Remove feature flag -//#define SKIP_PROFILE_KEYS +extern NSString *const kNSNotificationName_OtherUsersProfileDidChange; // This class can be safely accessed and used from any thread. @interface OWSProfilesManager : NSObject @@ -44,12 +42,16 @@ extern NSString *const kNSNotificationName_LocalProfileDidChange; - (void)setContactRecipientIds:(NSArray *)contactRecipientIds; -#pragma mark - Known Profile Keys +#pragma mark - Other User's Profiles - (void)setProfileKey:(NSData *)profileKey forRecipientId:(NSString *)recipientId; - (nullable NSData *)profileKeyForRecipientId:(NSString *)recipientId; +- (nullable NSString *)profileNameForRecipientId:(NSString *)recipientId; + +- (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId; + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Profiles/OWSProfilesManager.m b/SignalServiceKit/src/Profiles/OWSProfilesManager.m index 7474b7e62..097186b95 100644 --- a/SignalServiceKit/src/Profiles/OWSProfilesManager.m +++ b/SignalServiceKit/src/Profiles/OWSProfilesManager.m @@ -74,6 +74,7 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - NSString *const kNSNotificationName_LocalProfileDidChange = @"kNSNotificationName_LocalProfileDidChange"; +NSString *const kNSNotificationName_OtherUsersProfileDidChange = @"kNSNotificationName_OtherUsersProfileDidChange"; NSString *const kOWSProfilesManager_Collection = @"kOWSProfilesManager_Collection"; // This key is used to persist the local user's profile key. @@ -85,7 +86,12 @@ NSString *const kOWSProfilesManager_LocalProfileAvatarMetadataKey NSString *const kOWSProfilesManager_UserWhitelistCollection = @"kOWSProfilesManager_UserWhitelistCollection"; NSString *const kOWSProfilesManager_GroupWhitelistCollection = @"kOWSProfilesManager_GroupWhitelistCollection"; -NSString *const kOWSProfilesManager_KnownProfileKeysCollection = @"kOWSProfilesManager_KnownProfileKeysCollection"; +NSString *const kOWSProfilesManager_OtherUsersProfileKeysCollection + = @"kOWSProfilesManager_OtherUsersProfileKeysCollection"; +NSString *const kOWSProfilesManager_OtherUsersProfileNamesCollection + = @"kOWSProfilesManager_OtherUsersProfileNamesCollection"; +NSString *const kOWSProfilesManager_OtherUsersProfileAvatarMetadataCollection + = @"kOWSProfilesManager_OtherUsersProfileAvatarMetadataCollection"; // TODO: static const NSInteger kProfileKeyLength = 16; @@ -104,7 +110,10 @@ static const NSInteger kProfileKeyLength = 16; // These caches are lazy-populated. The single point truth is the database. @property (nonatomic, readonly) NSMutableDictionary *userProfileWhitelistCache; @property (nonatomic, readonly) NSMutableDictionary *groupProfileWhitelistCache; -@property (nonatomic, readonly) NSMutableDictionary*knownProfileKeyCache; +@property (nonatomic, readonly) NSMutableDictionary *otherUsersProfileKeyCache; +@property (nonatomic, readonly) NSMutableDictionary *otherUsersProfileNameCache; +// TODO: Replace with NSCache. +@property (nonatomic, readonly) NSMutableDictionary *otherUsersProfileAvatarImageCache; @end @@ -147,7 +156,9 @@ static const NSInteger kProfileKeyLength = 16; _dbConnection = storageManager.newDatabaseConnection; _userProfileWhitelistCache = [NSMutableDictionary new]; _groupProfileWhitelistCache = [NSMutableDictionary new]; - _knownProfileKeyCache = [NSMutableDictionary new]; + _otherUsersProfileKeyCache = [NSMutableDictionary new]; + _otherUsersProfileNameCache = [NSMutableDictionary new]; + _otherUsersProfileAvatarImageCache = [NSMutableDictionary new]; OWSSingletonAssert(); @@ -477,7 +488,7 @@ static const NSInteger kProfileKeyLength = 16; } } -#pragma mark - Known Profile Keys +#pragma mark - Other User's Profiles - (void)setProfileKey:(NSData *)profileKey forRecipientId:(NSString *)recipientId { @@ -493,31 +504,71 @@ static const NSInteger kProfileKeyLength = 16; // Ignore redundant update. return; } - + [self.dbConnection setObject:profileKey forKey:recipientId - inCollection:kOWSProfilesManager_KnownProfileKeysCollection]; - self.knownProfileKeyCache[recipientId] = profileKey; + inCollection:kOWSProfilesManager_OtherUsersProfileKeysCollection]; + self.otherUsersProfileKeyCache[recipientId] = profileKey; } - (nullable NSData *)profileKeyForRecipientId:(NSString *)recipientId { OWSAssert(recipientId.length > 0); - NSData *_Nullable profileKey = self.knownProfileKeyCache[recipientId]; + NSData *_Nullable profileKey = self.otherUsersProfileKeyCache[recipientId]; if (profileKey.length > 0) { return profileKey; } profileKey = - [self.dbConnection objectForKey:recipientId inCollection:kOWSProfilesManager_KnownProfileKeysCollection]; + [self.dbConnection objectForKey:recipientId inCollection:kOWSProfilesManager_OtherUsersProfileKeysCollection]; if (profileKey) { OWSAssert(profileKey.length == kProfileKeyLength); - self.knownProfileKeyCache[recipientId] = profileKey; + self.otherUsersProfileKeyCache[recipientId] = profileKey; } return profileKey; } +- (nullable NSString *)profileNameForRecipientId:(NSString *)recipientId +{ + OWSAssert(recipientId.length > 0); + + NSString *_Nullable profileName = self.otherUsersProfileNameCache[recipientId]; + if (profileName.length > 0) { + return profileName; + } + + profileName = + [self.dbConnection objectForKey:recipientId inCollection:kOWSProfilesManager_OtherUsersProfileNamesCollection]; + if (profileName) { + OWSAssert(profileName.length == kProfileKeyLength); + self.otherUsersProfileNameCache[recipientId] = profileName; + } else { + [self fetchProfileForRecipientId:recipientId]; + } + return profileName; +} + +- (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId +{ + // TODO: + return nil; +} + +- (void)fetchProfileForRecipientId:(NSString *)recipientId +{ + OWSAssert(recipientId.length > 0); + + // TODO: +} + +// NSString *const kOWSProfilesManager_OtherUsersProfileNamesCollection = +// @"kOWSProfilesManager_OtherUsersProfileNamesCollection"; NSString *const +// kOWSProfilesManager_OtherUsersProfileAvatarMetadataCollection = +// @"kOWSProfilesManager_OtherUsersProfileAvatarMetadataCollection"; kNSNotificationName_OtherUsersProfileDidChange +//@property (nonatomic, readonly) NSMutableDictionary *otherUsersProfileNameCache; +//@property (nonatomic, readonly) NSMutableDictionary *otherUsersProfileAvatarImageCache; + #pragma mark - Avatar Disk Cache - (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)fileName