2014-07-30 08:05:36 +02:00
|
|
|
//
|
|
|
|
// VersionMigrations.m
|
|
|
|
// Signal
|
|
|
|
//
|
|
|
|
// Created by Frederic Jacobs on 29/07/14.
|
|
|
|
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "VersionMigrations.h"
|
|
|
|
|
2015-01-11 17:48:18 +01:00
|
|
|
#import "Environment.h"
|
2015-08-23 00:00:39 +02:00
|
|
|
#import "LockInteractionController.h"
|
2015-01-11 17:48:18 +01:00
|
|
|
#import "PreferencesUtil.h"
|
|
|
|
#import "PushManager.h"
|
|
|
|
#import "RecentCallManager.h"
|
|
|
|
#import "SignalKeyingStorage.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import "TSAccountManager.h"
|
|
|
|
#import "TSNetworkManager.h"
|
2016-08-01 00:25:07 +02:00
|
|
|
#import <SignalServiceKit/OWSOrphanedDataCleaner.h>
|
2015-03-12 00:46:31 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
#define NEEDS_TO_REGISTER_PUSH_KEY @"Register For Push"
|
2015-08-23 00:00:39 +02:00
|
|
|
#define NEEDS_TO_REGISTER_ATTRIBUTES @"Register Attributes"
|
2015-03-12 00:46:31 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
@interface SignalKeyingStorage (VersionMigrations)
|
2015-01-11 17:48:18 +01:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
+ (void)storeString:(NSString *)string forKey:(NSString *)key;
|
|
|
|
+ (void)storeData:(NSData *)data forKey:(NSString *)key;
|
2015-01-11 17:48:18 +01:00
|
|
|
@end
|
|
|
|
|
2014-07-30 08:05:36 +02:00
|
|
|
@implementation VersionMigrations
|
|
|
|
|
2015-04-25 16:59:32 +02:00
|
|
|
#pragma mark Utility methods
|
|
|
|
|
2016-08-01 00:25:07 +02:00
|
|
|
+ (void)performUpdateCheck
|
|
|
|
{
|
2015-12-22 12:45:09 +01:00
|
|
|
NSString *previousVersion = Environment.preferences.lastRanVersion;
|
2015-04-25 16:59:32 +02:00
|
|
|
if (!previousVersion) {
|
2016-08-01 00:25:07 +02:00
|
|
|
DDLogInfo(@"No previous version found. Probably first launch since install - nothing to migrate.");
|
2016-08-21 20:15:50 +02:00
|
|
|
[Environment.preferences setAndGetCurrentVersion];
|
2015-04-25 16:59:32 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
|
|
|
if (([self isVersion:previousVersion atLeast:@"1.0.2" andLessThan:@"2.0"])) {
|
2015-08-23 00:00:39 +02:00
|
|
|
// We don't migrate from RedPhone anymore, too painful to maintain.
|
2016-08-01 00:25:07 +02:00
|
|
|
DDLogError(@"Migrating from RedPhone no longer supported. Resetting app data and quitting.");
|
2015-08-23 00:00:39 +02:00
|
|
|
[Environment resetAppData];
|
|
|
|
exit(0);
|
2015-04-25 16:59:32 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2016-08-01 00:25:07 +02:00
|
|
|
BOOL VOIPRegistration =
|
|
|
|
[[PushManager sharedManager] supportsVOIPPush] && ![Environment.preferences hasRegisteredVOIPPush];
|
|
|
|
|
2015-08-23 00:00:39 +02:00
|
|
|
// VOIP Push might need to be enabled because 1) user ran old version 2) Update to compatible iOS version
|
|
|
|
if (VOIPRegistration && [TSAccountManager isRegistered]) {
|
|
|
|
[self nonBlockingPushRegistration];
|
2015-04-25 16:59:32 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-09-01 19:22:08 +02:00
|
|
|
if ([self isVersion:previousVersion atLeast:@"2.0.0" andLessThan:@"2.1.70"] && [TSAccountManager isRegistered]) {
|
2015-04-25 16:59:32 +02:00
|
|
|
[self clearVideoCache];
|
2015-08-23 00:00:39 +02:00
|
|
|
[self blockingAttributesUpdate];
|
2015-04-28 14:41:50 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-11-29 01:14:49 +01:00
|
|
|
if ([self isVersion:previousVersion atLeast:@"2.0.0" andLessThan:@"2.3.0"] && [TSAccountManager isRegistered]) {
|
2016-04-18 20:17:08 +02:00
|
|
|
[self clearBloomFilterCache];
|
2015-11-29 01:14:49 +01:00
|
|
|
}
|
2016-08-01 00:25:07 +02:00
|
|
|
|
2016-08-01 19:41:10 +02:00
|
|
|
if ([self isVersion:previousVersion atLeast:@"2.0.0" andLessThan:@"2.4.1"] && [TSAccountManager isRegistered]) {
|
2016-08-15 14:06:34 +02:00
|
|
|
// Cleaning orphaned data can take a while, so let's run it in the background.
|
|
|
|
// This means this migration is not resiliant to failures - we'll only run it once
|
|
|
|
// regardless of its success.
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
DDLogInfo(@"OWSMigration: beginning removing orphaned data.");
|
|
|
|
[[OWSOrphanedDataCleaner new] removeOrphanedData];
|
|
|
|
DDLogInfo(@"OWSMigration: completed removing orphaned data.");
|
|
|
|
});
|
2016-08-01 19:41:10 +02:00
|
|
|
}
|
2016-08-01 00:25:07 +02:00
|
|
|
|
2016-09-27 03:57:34 +02:00
|
|
|
if ([self isVersion:previousVersion atLeast:@"2.0.0" andLessThan:@"2.5.2"] && [TSAccountManager isRegistered]) {
|
|
|
|
[[TSStorageManager sharedManager].dbConnection
|
|
|
|
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
NSUInteger legacyRecipientCount = [transaction numberOfKeysInCollection:@"TSRecipient"];
|
|
|
|
DDLogWarn(@"Removing %lu objects from TSRecipient collection", (unsigned long)legacyRecipientCount);
|
|
|
|
[transaction removeAllObjectsInCollection:@"TSRecipient"];
|
|
|
|
}];
|
|
|
|
}
|
2016-08-01 00:25:07 +02:00
|
|
|
[Environment.preferences setAndGetCurrentVersion];
|
2015-04-25 16:59:32 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
+ (BOOL)isVersion:(NSString *)thisVersionString
|
|
|
|
atLeast:(NSString *)openLowerBoundVersionString
|
|
|
|
andLessThan:(NSString *)closedUpperBoundVersionString {
|
|
|
|
return [self isVersion:thisVersionString atLeast:openLowerBoundVersionString] &&
|
|
|
|
[self isVersion:thisVersionString lessThan:closedUpperBoundVersionString];
|
2015-04-25 16:59:32 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
+ (BOOL)isVersion:(NSString *)thisVersionString atLeast:(NSString *)thatVersionString {
|
2015-04-25 16:59:32 +02:00
|
|
|
return [thisVersionString compare:thatVersionString options:NSNumericSearch] != NSOrderedAscending;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
+ (BOOL)isVersion:(NSString *)thisVersionString lessThan:(NSString *)thatVersionString {
|
2015-04-25 16:59:32 +02:00
|
|
|
return [thisVersionString compare:thatVersionString options:NSNumericSearch] == NSOrderedAscending;
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:00:39 +02:00
|
|
|
#pragma mark Upgrading to 2.1 - Needs to register VOIP token + Removing video cache folder
|
2015-04-25 16:59:32 +02:00
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
+ (void)nonBlockingPushRegistration {
|
|
|
|
__block failedBlock failedBlock = ^(NSError *error) {
|
|
|
|
DDLogError(@"Failed to register VOIP push token: %@", error.debugDescription);
|
2015-08-23 00:00:39 +02:00
|
|
|
};
|
2015-12-22 12:45:09 +01:00
|
|
|
[[PushManager sharedManager] requestPushTokenWithSuccess:^(NSString *pushToken, NSString *voipToken) {
|
|
|
|
[TSAccountManager registerForPushNotifications:pushToken
|
|
|
|
voipToken:voipToken
|
|
|
|
success:^{
|
|
|
|
DDLogWarn(@"Registered for VOIP Push.");
|
|
|
|
}
|
|
|
|
failure:failedBlock];
|
|
|
|
}
|
|
|
|
failure:failedBlock];
|
2015-03-12 00:46:31 +01:00
|
|
|
}
|
|
|
|
|
2015-04-25 16:59:32 +02:00
|
|
|
+ (void)clearVideoCache {
|
2015-12-22 12:45:09 +01:00
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
2015-04-25 16:59:32 +02:00
|
|
|
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
|
2015-12-22 12:45:09 +01:00
|
|
|
basePath = [basePath stringByAppendingPathComponent:@"videos"];
|
|
|
|
|
2015-04-25 16:59:32 +02:00
|
|
|
NSError *error;
|
2015-12-22 12:45:09 +01:00
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath:basePath]) {
|
2015-04-25 16:59:32 +02:00
|
|
|
[NSFileManager.defaultManager removeItemAtPath:basePath error:&error];
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-08-23 00:00:39 +02:00
|
|
|
if (error) {
|
|
|
|
DDLogError(@"An error occured while removing the videos cache folder from old location: %@",
|
|
|
|
error.debugDescription);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Upgrading to 2.1.3 - Adding VOIP flag on TS Server
|
|
|
|
|
|
|
|
+ (void)blockingAttributesUpdate {
|
2015-12-22 12:45:09 +01:00
|
|
|
LIControllerBlockingOperation blockingOperation = ^BOOL(void) {
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:@YES forKey:NEEDS_TO_REGISTER_ATTRIBUTES];
|
|
|
|
|
|
|
|
__block dispatch_semaphore_t sema = dispatch_semaphore_create(0);
|
|
|
|
|
|
|
|
__block BOOL success;
|
|
|
|
|
2016-06-28 04:51:57 +02:00
|
|
|
TSUpdateAttributesRequest *request = [[TSUpdateAttributesRequest alloc] initWithUpdatedAttributesWithVoice];
|
2015-12-22 12:45:09 +01:00
|
|
|
[[TSNetworkManager sharedManager] makeRequest:request
|
|
|
|
success:^(NSURLSessionDataTask *task, id responseObject) {
|
2015-08-23 00:00:39 +02:00
|
|
|
success = YES;
|
|
|
|
dispatch_semaphore_signal(sema);
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
|
|
|
failure:^(NSURLSessionDataTask *task, NSError *error) {
|
2015-08-23 00:00:39 +02:00
|
|
|
success = NO;
|
|
|
|
DDLogError(@"Updating attributess failed with error: %@", error.description);
|
|
|
|
dispatch_semaphore_signal(sema);
|
2015-12-22 12:45:09 +01:00
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
|
|
|
|
|
|
|
|
return success;
|
2015-08-23 00:00:39 +02:00
|
|
|
};
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-08-23 00:00:39 +02:00
|
|
|
LIControllerRetryBlock retryBlock = [LockInteractionController defaultNetworkRetry];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2015-08-23 00:00:39 +02:00
|
|
|
[LockInteractionController performBlock:blockingOperation
|
|
|
|
completionBlock:^{
|
2015-12-22 12:45:09 +01:00
|
|
|
[[NSUserDefaults standardUserDefaults] removeObjectForKey:NEEDS_TO_REGISTER_ATTRIBUTES];
|
|
|
|
DDLogWarn(@"Successfully updated attributes.");
|
2015-08-23 00:00:39 +02:00
|
|
|
}
|
|
|
|
retryBlock:retryBlock
|
|
|
|
usesNetwork:YES];
|
2015-04-25 16:59:32 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 20:17:08 +02:00
|
|
|
#pragma mark Upgrading to 2.3.0
|
|
|
|
|
|
|
|
// We removed bloom filter contact discovery. Clean up any local bloom filter data.
|
|
|
|
+ (void)clearBloomFilterCache {
|
|
|
|
NSFileManager *fm = [NSFileManager defaultManager];
|
|
|
|
NSArray *cachesDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
|
|
|
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.");
|
|
|
|
[[TSStorageManager sharedManager]
|
|
|
|
.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
[transaction removeAllObjectsInCollection:@"TSRecipient"];
|
|
|
|
}];
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 08:05:36 +02:00
|
|
|
@end
|