Account attributes updates.

pull/1/head
Matthew Chen 7 years ago
parent f1d93d4472
commit eb7abdfc64

@ -114,10 +114,14 @@ public class AccountManager: NSObject {
}
func enableManualMessageFetching() -> Promise<Void> {
let (promise, fulfill, reject) = Promise<Void>.pending()
tsAccountManager.setIsManualMessageFetchEnabled(true)
// Try to update the account attributes to reflect this change.
return SignalServiceRestClient().updateAcountAttributes()
.then(execute: { (_) in
fulfill(())
}).catch(execute: { (error) in
reject(error)
})
return promise
}
// MARK: Turn Server

@ -8,6 +8,12 @@ import SignalServiceKit
@objc
public class OWS111UDAttributesMigration: OWSDatabaseMigration {
// MARK: - Dependencies
private var tsAccountManager: TSAccountManager {
return TSAccountManager.sharedInstance()
}
// MARK: -
// increment a similar constant for each migration.
@ -29,13 +35,10 @@ public class OWS111UDAttributesMigration: OWSDatabaseMigration {
}
private func doMigration(completion: @escaping OWSDatabaseMigrationCompletion) {
return SignalServiceRestClient().updateAcountAttributes().then(execute: { _ in
self.dbReadWriteConnection().readWrite { transaction in
self.save(with: transaction)
}
})
.always {
completion()
}.retainUntilComplete()
tsAccountManager.updateAccountAttributes()
self.dbReadWriteConnection().readWrite { transaction in
self.save(with: transaction)
}
}
}

@ -96,6 +96,8 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
[self rotateLocalProfileKeyIfNecessary];
}];
[self observeNotifications];
return self;
}
@ -621,8 +623,20 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
// No need to rotate the profile key.
return success();
}
[self rotateProfileKeyWithSuccess:success
failure:failure
intersectingRecipientIds:intersectingRecipientIds
intersectingGroupIds:intersectingGroupIds];
});
}
- (void)rotateProfileKeyWithSuccess:(dispatch_block_t)success
failure:(ProfileManagerFailureBlock)failure
intersectingRecipientIds:(NSSet<NSString *> *)intersectingRecipientIds
intersectingGroupIds:(NSSet<NSData *> *)intersectingGroupIds {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Rotate the profile key
OWSLogInfo(@"Rotating the profile key.");
// Make copies of the current local profile state.
OWSUserProfile *localUserProfile = self.localUserProfile;
@ -709,6 +723,13 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
return @(1);
});
// Update account attributes.
//
// This may fail.
promise = promise.then(^(id value) {
return [self.tsAccountManager updateAccountAttributes];
});
promise = promise.then(^(id value) {
success();
});

@ -12,6 +12,7 @@ extern NSString *const RegistrationStateDidChangeNotification;
extern NSString *const DeregistrationStateDidChangeNotification;
extern NSString *const kNSNotificationName_LocalNumberDidChange;
@class AnyPromise;
@class OWSPrimaryStorage;
@class TSNetworkManager;
@class YapDatabaseReadWriteTransaction;
@ -135,12 +136,14 @@ extern NSString *const kNSNotificationName_LocalNumberDidChange;
#pragma mark - Manual Message Fetch
- (BOOL)isManualMessageFetchEnabled;
- (void)setIsManualMessageFetchEnabled:(BOOL)value;
- (AnyPromise *)setIsManualMessageFetchEnabled:(BOOL)value;
#ifdef DEBUG
- (void)registerForTestsWithLocalNumber:(NSString *)localNumber;
#endif
- (AnyPromise *)updateAccountAttributes;
@end
NS_ASSUME_NONNULL_END

@ -4,6 +4,7 @@
#import "TSAccountManager.h"
#import "AppContext.h"
#import "AppReadiness.h"
#import "NSNotificationCenter+OWS.h"
#import "NSURLSessionDataTask+StatusCode.h"
#import "OWSError.h"
@ -14,8 +15,11 @@
#import "TSPreKeyManager.h"
#import "YapDatabaseConnection+OWS.h"
#import "YapDatabaseTransaction+OWS.h"
#import <PromiseKit/AnyPromise.h>
#import <Reachability/Reachability.h>
#import <SignalCoreKit/NSData+OWS.h>
#import <SignalCoreKit/Randomness.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
#import <YapDatabase/YapDatabase.h>
NS_ASSUME_NONNULL_BEGIN
@ -35,6 +39,7 @@ NSString *const TSAccountManager_UserAccountCollection = @"TSStorageUserAccountC
NSString *const TSAccountManager_ServerAuthToken = @"TSStorageServerAuthToken";
NSString *const TSAccountManager_ServerSignalingKey = @"TSStorageServerSignalingKey";
NSString *const TSAccountManager_ManualMessageFetchKey = @"TSAccountManager_ManualMessageFetchKey";
NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountManager_NeedsAccountAttributesUpdateKey";
@interface TSAccountManager ()
@ -50,6 +55,8 @@ NSString *const TSAccountManager_ManualMessageFetchKey = @"TSAccountManager_Manu
@property (nonatomic, nullable) NSNumber *cachedIsDeregistered;
@property (nonatomic) Reachability *reachability;
@end
#pragma mark -
@ -66,6 +73,7 @@ NSString *const TSAccountManager_ManualMessageFetchKey = @"TSAccountManager_Manu
}
_dbConnection = [primaryStorage newDatabaseConnection];
self.reachability = [Reachability reachabilityForInternetConnection];
OWSSingletonAssert();
@ -76,6 +84,15 @@ NSString *const TSAccountManager_ManualMessageFetchKey = @"TSAccountManager_Manu
object:nil];
}
[AppReadiness runNowOrWhenAppIsReady:^{
[self updateAccountAttributesIfNecessary];
}];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged)
name:kReachabilityChangedNotification
object:nil];
return self;
}
@ -596,11 +613,13 @@ NSString *const TSAccountManager_ManualMessageFetchKey = @"TSAccountManager_Manu
defaultValue:NO];
}
- (void)setIsManualMessageFetchEnabled:(BOOL)value
{
- (AnyPromise *)setIsManualMessageFetchEnabled:(BOOL)value {
[self.dbConnection setBool:value
forKey:TSAccountManager_ManualMessageFetchKey
inCollection:TSAccountManager_UserAccountCollection];
// Try to update the account attributes to reflect this change.
return [self updateAccountAttributes];
}
- (void)registerForTestsWithLocalNumber:(NSString *)localNumber
@ -610,6 +629,49 @@ NSString *const TSAccountManager_ManualMessageFetchKey = @"TSAccountManager_Manu
[self storeLocalNumber:localNumber];
}
#pragma mark - Account Attributes
- (AnyPromise *)updateAccountAttributes {
[self.dbConnection setObject:[NSDate new]
forKey:TSAccountManager_NeedsAccountAttributesUpdateKey
inCollection:TSAccountManager_UserAccountCollection];
return [self updateAccountAttributesIfNecessary];
}
- (AnyPromise *)updateAccountAttributesIfNecessary {
NSDate *_Nullable updateRequestDate =
[self.dbConnection objectForKey:TSAccountManager_NeedsAccountAttributesUpdateKey
inCollection:TSAccountManager_UserAccountCollection];
if (!updateRequestDate) {
return [AnyPromise promiseWithValue:@(1)];
}
AnyPromise *promise = [[SignalServiceRestClient new] updateAccountAttributes];
promise = promise.then(^(id value) {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
// Clear the update request unless a new update has been requested.
NSDate *_Nullable latestUpdateRequestDate =
[transaction objectForKey:TSAccountManager_NeedsAccountAttributesUpdateKey
inCollection:TSAccountManager_UserAccountCollection];
if (latestUpdateRequestDate && [latestUpdateRequestDate isEqual:updateRequestDate]) {
[transaction removeObjectForKey:TSAccountManager_NeedsAccountAttributesUpdateKey
inCollection:TSAccountManager_UserAccountCollection];
}
}];
});
[promise retainUntilComplete];
return promise;
}
- (void)reachabilityChanged {
OWSAssertIsOnMainThread();
[AppReadiness runNowOrWhenAppIsReady:^{
[self updateAccountAttributesIfNecessary];
}];
}
@end
NS_ASSUME_NONNULL_END

@ -316,5 +316,8 @@ public class OWSUDManagerImpl: NSObject, OWSUDManager {
@objc
public func setShouldAllowUnrestrictedAccessLocal(_ value: Bool) {
dbConnection.setBool(value, forKey: kUDUnrestrictedAccessKey, inCollection: kUDCollection)
// Try to update the account attributes to reflect this change.
tsAccountManager.updateAccountAttributes()
}
}

@ -8,12 +8,16 @@ import SignalMetadataKit
public typealias RecipientIdentifier = String
public protocol SignalServiceClient {
@objc
public protocol SignalServiceClientObjC {
@objc func updateAccountAttributes() -> AnyPromise
}
public protocol SignalServiceClient: SignalServiceClientObjC {
func getAvailablePreKeys() -> Promise<Int>
func registerPreKeys(identityKey: IdentityKey, signedPreKeyRecord: SignedPreKeyRecord, preKeyRecords: [PreKeyRecord]) -> Promise<Void>
func setCurrentSignedPreKey(_ signedPreKey: SignedPreKeyRecord) -> Promise<Void>
func requestUDSenderCertificate() -> Promise<Data>
func updateAcountAttributes() -> Promise<Void>
func retrieveProfile(recipientId: RecipientIdentifier, unidentifiedAccess: SSKUnidentifiedAccess?) -> Promise<SignalServiceProfile>
}
@ -77,7 +81,8 @@ public class SignalServiceRestClient: NSObject, SignalServiceClient {
return try parser.requiredBase64EncodedData(key: "certificate")
}
public func updateAcountAttributes() -> Promise<Void> {
@objc
public func updateAccountAttributes() -> AnyPromise {
let request = OWSRequestFactory.updateAttributesRequest()
let promise: Promise<Void> = networkManager.makePromise(request: request)
.then(execute: { (_, _) in
@ -85,12 +90,12 @@ public class SignalServiceRestClient: NSObject, SignalServiceClient {
}).catch(execute: { (error) in
Logger.error("failed to update account attributes on server with error: \(error)")
})
return promise
return AnyPromise(promise)
}
public func retrieveProfile(recipientId: RecipientIdentifier, unidentifiedAccess: SSKUnidentifiedAccess?) -> Promise<SignalServiceProfile> {
let request = OWSRequestFactory.getProfileRequest(recipientId: recipientId, unidentifiedAccess: unidentifiedAccess)
return networkManager.makePromise(request: request).then { (task: URLSessionDataTask, responseObject: Any?) in
return networkManager.makePromise(request: request).then { (_: URLSessionDataTask, responseObject: Any?) in
return try SignalServiceProfile(recipientId: recipientId, responseObject: responseObject)
}
}

@ -7,6 +7,7 @@
#import "OWSPrimaryStorage.h"
#import "OWSRequestFactory.h"
#import "SSKEnvironment.h"
#import "TSAccountManager.h"
#import "TSNetworkManager.h"
#import "YapDatabaseConnection+OWS.h"
@ -64,6 +65,10 @@ const NSUInteger kDaySecs = kHourSecs * 24;
return SSKEnvironment.shared.networkManager;
}
- (TSAccountManager *)tsAccountManager {
return TSAccountManager.sharedInstance;
}
#pragma mark -
- (nullable NSString *)pinCode
@ -83,6 +88,8 @@ const NSUInteger kDaySecs = kHourSecs * 24;
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:NSNotificationName_2FAStateDidChange
object:nil
userInfo:nil];
[self.tsAccountManager updateAccountAttributes];
}
- (void)mark2FAAsEnabledWithPin:(NSString *)pin
@ -97,6 +104,8 @@ const NSUInteger kDaySecs = kHourSecs * 24;
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:NSNotificationName_2FAStateDidChange
object:nil
userInfo:nil];
[self.tsAccountManager updateAccountAttributes];
}
- (void)requestEnable2FAWithPin:(NSString *)pin

Loading…
Cancel
Save