mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
7.9 KiB
Matlab
198 lines
7.9 KiB
Matlab
11 years ago
|
//
|
||
8 years ago
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||
11 years ago
|
//
|
||
|
|
||
|
#import "VersionMigrations.h"
|
||
10 years ago
|
#import "Environment.h"
|
||
10 years ago
|
#import "LockInteractionController.h"
|
||
9 years ago
|
#import "OWSDatabaseMigrationRunner.h"
|
||
10 years ago
|
#import "SignalKeyingStorage.h"
|
||
8 years ago
|
#import <SignalServiceKit/AppVersion.h>
|
||
8 years ago
|
#import <SignalServiceKit/NSUserDefaults+OWS.h>
|
||
|
#import <SignalServiceKit/TSAccountManager.h>
|
||
|
#import <SignalServiceKit/TSNetworkManager.h>
|
||
10 years ago
|
|
||
10 years ago
|
#define NEEDS_TO_REGISTER_PUSH_KEY @"Register For Push"
|
||
10 years ago
|
#define NEEDS_TO_REGISTER_ATTRIBUTES @"Register Attributes"
|
||
10 years ago
|
|
||
10 years ago
|
@interface SignalKeyingStorage (VersionMigrations)
|
||
10 years ago
|
|
||
10 years ago
|
+ (void)storeString:(NSString *)string forKey:(NSString *)key;
|
||
|
+ (void)storeData:(NSData *)data forKey:(NSString *)key;
|
||
9 years ago
|
|
||
10 years ago
|
@end
|
||
|
|
||
11 years ago
|
@implementation VersionMigrations
|
||
|
|
||
10 years ago
|
#pragma mark Utility methods
|
||
|
|
||
9 years ago
|
+ (void)performUpdateCheck
|
||
|
{
|
||
8 years ago
|
// performUpdateCheck must be invoked after Environment has been initialized because
|
||
|
// upgrade process may depend on Environment.
|
||
8 years ago
|
OWSAssert([Environment current]);
|
||
8 years ago
|
|
||
8 years ago
|
NSString *previousVersion = AppVersion.instance.lastAppVersion;
|
||
|
NSString *currentVersion = AppVersion.instance.currentAppVersion;
|
||
9 years ago
|
|
||
8 years ago
|
DDLogInfo(@"%@ Checking migrations. currentVersion: %@, lastRanVersion: %@",
|
||
|
self.logTag,
|
||
|
currentVersion,
|
||
|
previousVersion);
|
||
9 years ago
|
|
||
10 years ago
|
if (!previousVersion) {
|
||
9 years ago
|
DDLogInfo(@"No previous version found. Probably first launch since install - nothing to migrate.");
|
||
9 years ago
|
OWSDatabaseMigrationRunner *runner =
|
||
|
[[OWSDatabaseMigrationRunner alloc] initWithStorageManager:[TSStorageManager sharedManager]];
|
||
|
[runner assumeAllExistingMigrationsRun];
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if ([self isVersion:previousVersion atLeast:@"1.0.2" andLessThan:@"2.0"]) {
|
||
|
DDLogError(@"Migrating from RedPhone no longer supported. Quitting.");
|
||
|
// Not translating these as so few are affected.
|
||
|
UIAlertController *alertController = [UIAlertController
|
||
|
alertControllerWithTitle:@"You must reinstall Signal"
|
||
|
message:
|
||
|
@"Sorry, your installation is too old for us to update. You'll have to start fresh."
|
||
|
preferredStyle:UIAlertControllerStyleAlert];
|
||
|
|
||
|
UIAlertAction *quitAction = [UIAlertAction actionWithTitle:@"Quit"
|
||
|
style:UIAlertActionStyleDefault
|
||
|
handler:^(UIAlertAction *_Nonnull action) {
|
||
8 years ago
|
[DDLog flushLog];
|
||
9 years ago
|
exit(0);
|
||
|
}];
|
||
|
[alertController addAction:quitAction];
|
||
|
|
||
|
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController
|
||
|
animated:YES
|
||
|
completion:nil];
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if ([self isVersion:previousVersion atLeast:@"2.0.0" andLessThan:@"2.1.70"] && [TSAccountManager isRegistered]) {
|
||
10 years ago
|
[self clearVideoCache];
|
||
10 years ago
|
[self blockingAttributesUpdate];
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if ([self isVersion:previousVersion atLeast:@"2.0.0" andLessThan:@"2.3.0"] && [TSAccountManager isRegistered]) {
|
||
9 years ago
|
[self clearBloomFilterCache];
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
[[[OWSDatabaseMigrationRunner alloc] initWithStorageManager:[TSStorageManager sharedManager]] runAllOutstanding];
|
||
8 years ago
|
}
|
||
|
|
||
|
+ (void)runSafeBlockingMigrations
|
||
|
{
|
||
|
[[[OWSDatabaseMigrationRunner alloc] initWithStorageManager:[TSStorageManager sharedManager]]
|
||
|
runSafeBlockingMigrations];
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
+ (BOOL)isVersion:(NSString *)thisVersionString
|
||
|
atLeast:(NSString *)openLowerBoundVersionString
|
||
8 years ago
|
andLessThan:(NSString *)closedUpperBoundVersionString
|
||
|
{
|
||
10 years ago
|
return [self isVersion:thisVersionString atLeast:openLowerBoundVersionString] &&
|
||
8 years ago
|
[self isVersion:thisVersionString lessThan:closedUpperBoundVersionString];
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
+ (BOOL)isVersion:(NSString *)thisVersionString atLeast:(NSString *)thatVersionString
|
||
|
{
|
||
10 years ago
|
return [thisVersionString compare:thatVersionString options:NSNumericSearch] != NSOrderedAscending;
|
||
|
}
|
||
|
|
||
8 years ago
|
+ (BOOL)isVersion:(NSString *)thisVersionString lessThan:(NSString *)thatVersionString
|
||
|
{
|
||
10 years ago
|
return [thisVersionString compare:thatVersionString options:NSNumericSearch] == NSOrderedAscending;
|
||
|
}
|
||
|
|
||
8 years ago
|
#pragma mark Upgrading to 2.1 - Removing video cache folder
|
||
10 years ago
|
|
||
8 years ago
|
+ (void)clearVideoCache
|
||
|
{
|
||
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||
10 years ago
|
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
|
||
8 years ago
|
basePath = [basePath stringByAppendingPathComponent:@"videos"];
|
||
10 years ago
|
|
||
10 years ago
|
NSError *error;
|
||
10 years ago
|
if ([[NSFileManager defaultManager] fileExistsAtPath:basePath]) {
|
||
10 years ago
|
[NSFileManager.defaultManager removeItemAtPath:basePath error:&error];
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if (error) {
|
||
8 years ago
|
DDLogError(
|
||
|
@"An error occured while removing the videos cache folder from old location: %@", error.debugDescription);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#pragma mark Upgrading to 2.1.3 - Adding VOIP flag on TS Server
|
||
|
|
||
8 years ago
|
+ (void)blockingAttributesUpdate
|
||
|
{
|
||
10 years ago
|
LIControllerBlockingOperation blockingOperation = ^BOOL(void) {
|
||
8 years ago
|
[[NSUserDefaults appUserDefaults] setObject:@YES forKey:NEEDS_TO_REGISTER_ATTRIBUTES];
|
||
10 years ago
|
|
||
8 years ago
|
__block dispatch_semaphore_t sema = dispatch_semaphore_create(0);
|
||
10 years ago
|
|
||
8 years ago
|
__block BOOL success;
|
||
10 years ago
|
|
||
8 years ago
|
TSUpdateAttributesRequest *request = [[TSUpdateAttributesRequest alloc] initWithManualMessageFetching:NO];
|
||
|
[[TSNetworkManager sharedManager] makeRequest:request
|
||
|
success:^(NSURLSessionDataTask *task, id responseObject) {
|
||
|
success = YES;
|
||
|
dispatch_semaphore_signal(sema);
|
||
|
}
|
||
|
failure:^(NSURLSessionDataTask *task, NSError *error) {
|
||
|
if (!IsNSErrorNetworkFailure(error)) {
|
||
|
OWSProdError([OWSAnalyticsEvents errorUpdateAttributesRequestFailed]);
|
||
|
}
|
||
|
success = NO;
|
||
|
DDLogError(@"Updating attributess failed with error: %@", error.description);
|
||
|
dispatch_semaphore_signal(sema);
|
||
|
}];
|
||
10 years ago
|
|
||
|
|
||
8 years ago
|
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
|
||
10 years ago
|
|
||
8 years ago
|
return success;
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
10 years ago
|
LIControllerRetryBlock retryBlock = [LockInteractionController defaultNetworkRetry];
|
||
10 years ago
|
|
||
10 years ago
|
[LockInteractionController performBlock:blockingOperation
|
||
|
completionBlock:^{
|
||
8 years ago
|
[[NSUserDefaults appUserDefaults] removeObjectForKey:NEEDS_TO_REGISTER_ATTRIBUTES];
|
||
|
DDLogWarn(@"Successfully updated attributes.");
|
||
10 years ago
|
}
|
||
|
retryBlock:retryBlock
|
||
|
usesNetwork:YES];
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
#pragma mark Upgrading to 2.3.0
|
||
|
|
||
|
// We removed bloom filter contact discovery. Clean up any local bloom filter data.
|
||
8 years ago
|
+ (void)clearBloomFilterCache
|
||
|
{
|
||
|
NSFileManager *fm = [NSFileManager defaultManager];
|
||
|
NSArray *cachesDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
||
9 years ago
|
NSString *bloomFilterPath = [[cachesDir objectAtIndex:0] stringByAppendingPathComponent:@"bloomfilter"];
|
||
|
|
||
|
if ([fm fileExistsAtPath:bloomFilterPath]) {
|
||
|
NSError *deleteError;
|
||
|
if ([fm removeItemAtPath:bloomFilterPath error:&deleteError]) {
|
||
|
DDLogInfo(@"Successfully removed bloom filter cache.");
|
||
8 years ago
|
[[TSStorageManager sharedManager].dbReadWriteConnection
|
||
|
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
||
|
[transaction removeAllObjectsInCollection:@"TSRecipient"];
|
||
|
}];
|
||
9 years ago
|
DDLogInfo(@"Removed all TSRecipient records - will be replaced by SignalRecipients at next address sync.");
|
||
|
} else {
|
||
|
DDLogError(@"Failed to remove bloom filter cache with error: %@", deleteError.localizedDescription);
|
||
|
}
|
||
|
} else {
|
||
|
DDLogDebug(@"No bloom filter cache to remove.");
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
@end
|