Add accessor for other users’ profile names.

// FREEBIE
This commit is contained in:
Matthew Chen 2017-08-02 09:27:29 -04:00
parent 4a54f1a99c
commit d7f275ce73
4 changed files with 67 additions and 18 deletions

View File

@ -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

View File

@ -496,14 +496,12 @@ NS_ASSUME_NONNULL_BEGIN
OWSSignalServiceProtosContent *content = [OWSSignalServiceProtosContent parseFromData:plaintextData];
DDLogInfo(@"%@ handling content: <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];

View File

@ -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<NSString *> *)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

View File

@ -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<NSString *, NSNumber *> *userProfileWhitelistCache;
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSNumber *> *groupProfileWhitelistCache;
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSData *>*knownProfileKeyCache;
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSData *> *otherUsersProfileKeyCache;
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSString *> *otherUsersProfileNameCache;
// TODO: Replace with NSCache.
@property (nonatomic, readonly) NSMutableDictionary<NSString *, UIImage *> *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<NSString *, NSString *> *otherUsersProfileNameCache;
//@property (nonatomic, readonly) NSMutableDictionary<NSString *, UIImage *> *otherUsersProfileAvatarImageCache;
#pragma mark - Avatar Disk Cache
- (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)fileName