Respond to CR.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent c603a2651d
commit 539490ee19

@ -294,16 +294,7 @@ NS_ASSUME_NONNULL_BEGIN
}]]; }]];
} }
BOOL isOnProfileWhitelist = NO; if ([OWSProfilesManager.sharedManager isThreadInProfileWhitelist:self.thread]) {
if (self.isGroupThread) {
TSGroupThread *groupThread = (TSGroupThread *)self.thread;
NSData *groupId = groupThread.groupModel.groupId;
isOnProfileWhitelist = [OWSProfilesManager.sharedManager isGroupIdInProfileWhitelist:groupId];
} else {
NSString *recipientId = self.thread.contactIdentifier;
isOnProfileWhitelist = [OWSProfilesManager.sharedManager isUserInProfileWhitelist:recipientId];
}
if (isOnProfileWhitelist) {
[mainSection addItem:[OWSTableItem itemWithCustomCellBlock:^{ [mainSection addItem:[OWSTableItem itemWithCustomCellBlock:^{
return [weakSelf return [weakSelf
labelCellWithName:(self.isGroupThread labelCellWithName:(self.isGroupThread

@ -213,14 +213,14 @@ NS_ASSUME_NONNULL_BEGIN
{ {
__weak ProfileViewController *weakSelf = self; __weak ProfileViewController *weakSelf = self;
[OWSProfilesManager.sharedManager [OWSProfilesManager.sharedManager
updateLocalProfileName:self.nameTextField.text updateLocalProfileName:self.nameTextField.text
localProfileAvatarImage:self.avatar avatarImage:self.avatar
success:^{ success:^{
[weakSelf.navigationController popViewControllerAnimated:YES]; [weakSelf.navigationController popViewControllerAnimated:YES];
} }
failure:^{ failure:^{
// <#code#> // <#code#>
}]; }];
} }
#pragma mark - UITextFieldDelegate #pragma mark - UITextFieldDelegate

@ -480,17 +480,8 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
// recipient OR the group is in the whitelist. // recipient OR the group is in the whitelist.
if ([OWSProfilesManager.sharedManager isUserInProfileWhitelist:recipient.recipientId]) { if ([OWSProfilesManager.sharedManager isUserInProfileWhitelist:recipient.recipientId]) {
shouldIncludeProfileKey = YES; shouldIncludeProfileKey = YES;
} else if (self.thread.isGroupThread) { } else if ([OWSProfilesManager.sharedManager isThreadInProfileWhitelist:self.thread]) {
TSGroupThread *groupThread = (TSGroupThread *)self.thread; shouldIncludeProfileKey = YES;
NSData *groupId = groupThread.groupModel.groupId;
if ([OWSProfilesManager.sharedManager isGroupIdInProfileWhitelist:groupId]) {
[contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey];
}
} else {
TSContactThread *contactThread = (TSContactThread *)self.thread;
NSString *recipientId = contactThread.contactIdentifier;
OWSAssert([recipientId isEqualToString:recipient.recipientId]);
} }
} }

@ -7,6 +7,8 @@ NS_ASSUME_NONNULL_BEGIN
extern NSString *const kNSNotificationName_LocalProfileDidChange; extern NSString *const kNSNotificationName_LocalProfileDidChange;
extern NSString *const kNSNotificationName_OtherUsersProfileDidChange; extern NSString *const kNSNotificationName_OtherUsersProfileDidChange;
@class TSThread;
// 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
@ -43,10 +45,10 @@ extern NSString *const kNSNotificationName_OtherUsersProfileDidChange;
- (void)addGroupIdToProfileWhitelist:(NSData *)groupId; - (void)addGroupIdToProfileWhitelist:(NSData *)groupId;
- (BOOL)isGroupIdInProfileWhitelist:(NSData *)groupId;
- (void)setContactRecipientIds:(NSArray<NSString *> *)contactRecipientIds; - (void)setContactRecipientIds:(NSArray<NSString *> *)contactRecipientIds;
- (BOOL)isThreadInProfileWhitelist:(TSThread *)thread;
#pragma mark - Other User's Profiles #pragma mark - Other User's Profiles
- (void)setProfileKey:(NSData *)profileKey forRecipientId:(NSString *)recipientId; - (void)setProfileKey:(NSData *)profileKey forRecipientId:(NSString *)recipientId;

@ -8,7 +8,9 @@
#import "OWSMessageSender.h" #import "OWSMessageSender.h"
#import "SecurityUtils.h" #import "SecurityUtils.h"
#import "TSAccountManager.h" #import "TSAccountManager.h"
#import "TSGroupThread.h"
#import "TSStorageManager.h" #import "TSStorageManager.h"
#import "TSThread.h"
#import "TSYapDatabaseObject.h" #import "TSYapDatabaseObject.h"
#import "TextSecureKitEnv.h" #import "TextSecureKitEnv.h"
@ -60,11 +62,6 @@ NS_ASSUME_NONNULL_BEGIN
return self; return self;
} }
+ (NSString *)collection
{
return @"UserProfile";
}
#pragma mark - NSObject #pragma mark - NSObject
- (BOOL)isEqual:(UserProfile *)other - (BOOL)isEqual:(UserProfile *)other
@ -113,7 +110,7 @@ static const NSInteger kProfileKeyLength = 16;
// This property should only be mutated on the main thread, // This property should only be mutated on the main thread,
@property (nonatomic, nullable) UIImage *localCachedAvatarImage; @property (nonatomic, nullable) UIImage *localCachedAvatarImage;
// These caches are lazy-populated. The single point truth is the database. // These caches are lazy-populated. The single point of truth is the database.
// //
// These three properties can be accessed on any thread. // These three properties can be accessed on any thread.
@property (atomic, readonly) NSMutableDictionary<NSString *, NSNumber *> *userProfileWhitelistCache; @property (atomic, readonly) NSMutableDictionary<NSString *, NSNumber *> *userProfileWhitelistCache;
@ -440,12 +437,37 @@ static const NSInteger kProfileKeyLength = 16;
- (void)addUserToProfileWhitelist:(NSString *)recipientId - (void)addUserToProfileWhitelist:(NSString *)recipientId
{ {
OWSAssert([NSThread isMainThread]);
OWSAssert(recipientId.length > 0); OWSAssert(recipientId.length > 0);
[self.dbConnection setObject:@(1) forKey:recipientId inCollection:kOWSProfilesManager_UserWhitelistCollection]; [self.dbConnection setBool:YES forKey:recipientId inCollection:kOWSProfilesManager_UserWhitelistCollection];
self.userProfileWhitelistCache[recipientId] = @(YES); self.userProfileWhitelistCache[recipientId] = @(YES);
} }
- (void)addUsersToProfileWhitelist:(NSArray<NSString *> *)recipientIds
{
OWSAssert([NSThread isMainThread]);
OWSAssert(recipientIds);
NSMutableArray<NSString *> *newRecipientIds = [NSMutableArray new];
for (NSString *recipientId in recipientIds) {
if (!self.userProfileWhitelistCache[recipientId]) {
[newRecipientIds addObject:recipientId];
}
}
if (newRecipientIds.count < 1) {
return;
}
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (NSString *recipientId in recipientIds) {
[transaction setObject:@(YES) forKey:recipientId inCollection:kOWSProfilesManager_UserWhitelistCollection];
self.userProfileWhitelistCache[recipientId] = @(YES);
}
}];
}
- (BOOL)isUserInProfileWhitelist:(NSString *)recipientId - (BOOL)isUserInProfileWhitelist:(NSString *)recipientId
{ {
OWSAssert(recipientId.length > 0); OWSAssert(recipientId.length > 0);
@ -455,8 +477,7 @@ static const NSInteger kProfileKeyLength = 16;
return [value boolValue]; return [value boolValue];
} }
value = value = @([self.dbConnection hasObjectForKey:recipientId inCollection:kOWSProfilesManager_UserWhitelistCollection]);
@(nil != [self.dbConnection objectForKey:recipientId inCollection:kOWSProfilesManager_UserWhitelistCollection]);
self.userProfileWhitelistCache[recipientId] = value; self.userProfileWhitelistCache[recipientId] = value;
return [value boolValue]; return [value boolValue];
} }
@ -486,8 +507,23 @@ static const NSInteger kProfileKeyLength = 16;
return [value boolValue]; return [value boolValue];
} }
- (BOOL)isThreadInProfileWhitelist:(TSThread *)thread
{
OWSAssert(thread);
if (thread.isGroupThread) {
TSGroupThread *groupThread = (TSGroupThread *)thread;
NSData *groupId = groupThread.groupModel.groupId;
return [OWSProfilesManager.sharedManager isGroupIdInProfileWhitelist:groupId];
} else {
NSString *recipientId = thread.contactIdentifier;
return [OWSProfilesManager.sharedManager isUserInProfileWhitelist:recipientId];
}
}
- (void)setContactRecipientIds:(NSArray<NSString *> *)contactRecipientIds - (void)setContactRecipientIds:(NSArray<NSString *> *)contactRecipientIds
{ {
OWSAssert([NSThread isMainThread]);
OWSAssert(contactRecipientIds); OWSAssert(contactRecipientIds);
// TODO: The persisted whitelist could either be: // TODO: The persisted whitelist could either be:
@ -495,9 +531,7 @@ static const NSInteger kProfileKeyLength = 16;
// * Just users manually added to the whitelist. // * Just users manually added to the whitelist.
// * Also include users auto-added by, for example, being in the user's // * Also include users auto-added by, for example, being in the user's
// contacts or when the user initiates a 1:1 conversation with them, etc. // contacts or when the user initiates a 1:1 conversation with them, etc.
for (NSString *recipientId in contactRecipientIds) { [self addUsersToProfileWhitelist:contactRecipientIds];
[self addUserToProfileWhitelist:recipientId];
}
} }
#pragma mark - Other User's Profiles #pragma mark - Other User's Profiles
@ -533,7 +567,7 @@ static const NSInteger kProfileKeyLength = 16;
} }
profileKey = profileKey =
[self.dbConnection objectForKey:recipientId inCollection:kOWSProfilesManager_OtherUsersProfileKeysCollection]; [self.dbConnection dataForKey:recipientId inCollection:kOWSProfilesManager_OtherUsersProfileKeysCollection];
if (profileKey) { if (profileKey) {
OWSAssert(profileKey.length == kProfileKeyLength); OWSAssert(profileKey.length == kProfileKeyLength);
self.otherUsersProfileKeyCache[recipientId] = profileKey; self.otherUsersProfileKeyCache[recipientId] = profileKey;

@ -12,6 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface YapDatabaseConnection (OWS) @interface YapDatabaseConnection (OWS)
- (BOOL)hasObjectForKey:(NSString *)key inCollection:(NSString *)collection;
- (BOOL)boolForKey:(NSString *)key inCollection:(NSString *)collection; - (BOOL)boolForKey:(NSString *)key inCollection:(NSString *)collection;
- (int)intForKey:(NSString *)key inCollection:(NSString *)collection; - (int)intForKey:(NSString *)key inCollection:(NSString *)collection;
- (nullable id)objectForKey:(NSString *)key inCollection:(NSString *)collection; - (nullable id)objectForKey:(NSString *)key inCollection:(NSString *)collection;
@ -26,6 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - #pragma mark -
- (void)setObject:(id)object forKey:(NSString *)key inCollection:(NSString *)collection; - (void)setObject:(id)object forKey:(NSString *)key inCollection:(NSString *)collection;
- (void)setBool:(BOOL)value forKey:(NSString *)key inCollection:(NSString *)collection;
- (void)removeObjectForKey:(NSString *)string inCollection:(NSString *)collection; - (void)removeObjectForKey:(NSString *)string inCollection:(NSString *)collection;
- (void)setInt:(int)integer forKey:(NSString *)key inCollection:(NSString *)collection; - (void)setInt:(int)integer forKey:(NSString *)key inCollection:(NSString *)collection;
- (void)setDate:(NSDate *)value forKey:(NSString *)key inCollection:(NSString *)collection; - (void)setDate:(NSDate *)value forKey:(NSString *)key inCollection:(NSString *)collection;

@ -12,6 +12,14 @@ NS_ASSUME_NONNULL_BEGIN
@implementation YapDatabaseConnection (OWS) @implementation YapDatabaseConnection (OWS)
- (BOOL)hasObjectForKey:(NSString *)key inCollection:(NSString *)collection
{
OWSAssert(key.length > 0);
OWSAssert(collection.length > 0);
return nil != [self objectForKey:key inCollection:collection];
}
- (nullable id)objectForKey:(NSString *)key inCollection:(NSString *)collection - (nullable id)objectForKey:(NSString *)key inCollection:(NSString *)collection
{ {
OWSAssert(key.length > 0); OWSAssert(key.length > 0);
@ -104,6 +112,14 @@ NS_ASSUME_NONNULL_BEGIN
}]; }];
} }
- (void)setBool:(BOOL)value forKey:(NSString *)key inCollection:(NSString *)collection
{
OWSAssert(key.length > 0);
OWSAssert(collection.length > 0);
[self setObject:@(value) forKey:key inCollection:collection];
}
- (void)removeObjectForKey:(NSString *)key inCollection:(NSString *)collection - (void)removeObjectForKey:(NSString *)key inCollection:(NSString *)collection
{ {
OWSAssert(key.length > 0); OWSAssert(key.length > 0);
@ -114,12 +130,12 @@ NS_ASSUME_NONNULL_BEGIN
}]; }];
} }
- (void)setInt:(int)integer forKey:(NSString *)key inCollection:(NSString *)collection - (void)setInt:(int)value forKey:(NSString *)key inCollection:(NSString *)collection
{ {
OWSAssert(key.length > 0); OWSAssert(key.length > 0);
OWSAssert(collection.length > 0); OWSAssert(collection.length > 0);
[self setObject:@(integer) forKey:key inCollection:collection]; [self setObject:@(value) forKey:key inCollection:collection];
} }
- (int)incrementIntForKey:(NSString *)key inCollection:(NSString *)collection - (int)incrementIntForKey:(NSString *)key inCollection:(NSString *)collection

Loading…
Cancel
Save