Add accessor for other users’ profile names.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 4a54f1a99c
commit d7f275ce73

@ -464,7 +464,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
OWSAssert(contentBuilder); OWSAssert(contentBuilder);
OWSAssert(recipient); OWSAssert(recipient);
#ifndef SKIP_PROFILE_KEYS
OWSAssert(OWSProfilesManager.sharedManager.localProfileKey.length > 0); OWSAssert(OWSProfilesManager.sharedManager.localProfileKey.length > 0);
BOOL shouldIncludeProfileKey = NO; BOOL shouldIncludeProfileKey = NO;
@ -498,7 +497,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
if (shouldIncludeProfileKey) { if (shouldIncludeProfileKey) {
[contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey]; [contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey];
} }
#endif
} }
- (NSData *)buildPlainTextData:(SignalRecipient *)recipient - (NSData *)buildPlainTextData:(SignalRecipient *)recipient

@ -496,14 +496,12 @@ NS_ASSUME_NONNULL_BEGIN
OWSSignalServiceProtosContent *content = [OWSSignalServiceProtosContent parseFromData:plaintextData]; OWSSignalServiceProtosContent *content = [OWSSignalServiceProtosContent parseFromData:plaintextData];
DDLogInfo(@"%@ handling content: <Content: %@>", self.tag, [self descriptionForContent:content]); DDLogInfo(@"%@ handling content: <Content: %@>", self.tag, [self descriptionForContent:content]);
#ifndef SKIP_PROFILE_KEYS
if ([content hasProfileKey]) { if ([content hasProfileKey]) {
NSData *profileKey = [content profileKey]; NSData *profileKey = [content profileKey];
NSString *recipientId = envelope.source; NSString *recipientId = envelope.source;
[OWSProfilesManager.sharedManager setProfileKey:profileKey [OWSProfilesManager.sharedManager setProfileKey:profileKey
forRecipientId:recipientId]; forRecipientId:recipientId];
} }
#endif
if (content.hasSyncMessage) { if (content.hasSyncMessage) {
[self handleIncomingEnvelope:envelope withSyncMessage:content.syncMessage]; [self handleIncomingEnvelope:envelope withSyncMessage:content.syncMessage];

@ -5,9 +5,7 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
extern NSString *const kNSNotificationName_LocalProfileDidChange; extern NSString *const kNSNotificationName_LocalProfileDidChange;
extern NSString *const kNSNotificationName_OtherUsersProfileDidChange;
// TODO: Remove feature flag
//#define SKIP_PROFILE_KEYS
// This class can be safely accessed and used from any thread. // This class can be safely accessed and used from any thread.
@interface OWSProfilesManager : NSObject @interface OWSProfilesManager : NSObject
@ -44,12 +42,16 @@ extern NSString *const kNSNotificationName_LocalProfileDidChange;
- (void)setContactRecipientIds:(NSArray<NSString *> *)contactRecipientIds; - (void)setContactRecipientIds:(NSArray<NSString *> *)contactRecipientIds;
#pragma mark - Known Profile Keys #pragma mark - Other User's Profiles
- (void)setProfileKey:(NSData *)profileKey forRecipientId:(NSString *)recipientId; - (void)setProfileKey:(NSData *)profileKey forRecipientId:(NSString *)recipientId;
- (nullable NSData *)profileKeyForRecipientId:(NSString *)recipientId; - (nullable NSData *)profileKeyForRecipientId:(NSString *)recipientId;
- (nullable NSString *)profileNameForRecipientId:(NSString *)recipientId;
- (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -74,6 +74,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - #pragma mark -
NSString *const kNSNotificationName_LocalProfileDidChange = @"kNSNotificationName_LocalProfileDidChange"; NSString *const kNSNotificationName_LocalProfileDidChange = @"kNSNotificationName_LocalProfileDidChange";
NSString *const kNSNotificationName_OtherUsersProfileDidChange = @"kNSNotificationName_OtherUsersProfileDidChange";
NSString *const kOWSProfilesManager_Collection = @"kOWSProfilesManager_Collection"; NSString *const kOWSProfilesManager_Collection = @"kOWSProfilesManager_Collection";
// This key is used to persist the local user's profile key. // 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_UserWhitelistCollection = @"kOWSProfilesManager_UserWhitelistCollection";
NSString *const kOWSProfilesManager_GroupWhitelistCollection = @"kOWSProfilesManager_GroupWhitelistCollection"; 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: // TODO:
static const NSInteger kProfileKeyLength = 16; 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. // 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 *> *userProfileWhitelistCache;
@property (nonatomic, readonly) NSMutableDictionary<NSString *, NSNumber *> *groupProfileWhitelistCache; @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 @end
@ -147,7 +156,9 @@ static const NSInteger kProfileKeyLength = 16;
_dbConnection = storageManager.newDatabaseConnection; _dbConnection = storageManager.newDatabaseConnection;
_userProfileWhitelistCache = [NSMutableDictionary new]; _userProfileWhitelistCache = [NSMutableDictionary new];
_groupProfileWhitelistCache = [NSMutableDictionary new]; _groupProfileWhitelistCache = [NSMutableDictionary new];
_knownProfileKeyCache = [NSMutableDictionary new]; _otherUsersProfileKeyCache = [NSMutableDictionary new];
_otherUsersProfileNameCache = [NSMutableDictionary new];
_otherUsersProfileAvatarImageCache = [NSMutableDictionary new];
OWSSingletonAssert(); 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 - (void)setProfileKey:(NSData *)profileKey forRecipientId:(NSString *)recipientId
{ {
@ -493,31 +504,71 @@ static const NSInteger kProfileKeyLength = 16;
// Ignore redundant update. // Ignore redundant update.
return; return;
} }
[self.dbConnection setObject:profileKey [self.dbConnection setObject:profileKey
forKey:recipientId forKey:recipientId
inCollection:kOWSProfilesManager_KnownProfileKeysCollection]; inCollection:kOWSProfilesManager_OtherUsersProfileKeysCollection];
self.knownProfileKeyCache[recipientId] = profileKey; self.otherUsersProfileKeyCache[recipientId] = profileKey;
} }
- (nullable NSData *)profileKeyForRecipientId:(NSString *)recipientId - (nullable NSData *)profileKeyForRecipientId:(NSString *)recipientId
{ {
OWSAssert(recipientId.length > 0); OWSAssert(recipientId.length > 0);
NSData *_Nullable profileKey = self.knownProfileKeyCache[recipientId]; NSData *_Nullable profileKey = self.otherUsersProfileKeyCache[recipientId];
if (profileKey.length > 0) { if (profileKey.length > 0) {
return profileKey; return profileKey;
} }
profileKey = profileKey =
[self.dbConnection objectForKey:recipientId inCollection:kOWSProfilesManager_KnownProfileKeysCollection]; [self.dbConnection objectForKey:recipientId inCollection:kOWSProfilesManager_OtherUsersProfileKeysCollection];
if (profileKey) { if (profileKey) {
OWSAssert(profileKey.length == kProfileKeyLength); OWSAssert(profileKey.length == kProfileKeyLength);
self.knownProfileKeyCache[recipientId] = profileKey; self.otherUsersProfileKeyCache[recipientId] = profileKey;
} }
return 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 #pragma mark - Avatar Disk Cache
- (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)fileName - (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)fileName

Loading…
Cancel
Save