session-ios/SignalUtilitiesKit/Utilities/VersionMigrations.m

137 lines
4.9 KiB
Mathematica
Raw Normal View History

//
2019-01-08 17:25:03 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
#import "VersionMigrations.h"
#import "OWSDatabaseMigrationRunner.h"
2020-11-20 04:04:56 +01:00
#import <SessionUtilitiesKit/AppContext.h>
#import <SignalUtilitiesKit/AppVersion.h>
2020-11-25 06:15:16 +01:00
#import <SessionUtilitiesKit/NSUserDefaults+OWS.h>
#import <SignalUtilitiesKit/OWSPrimaryStorage+Loki.h>
2020-11-25 06:15:16 +01:00
#import <SessionMessagingKit/TSAccountManager.h>
#import <SessionMessagingKit/TSThread.h>
#import <SessionMessagingKit/TSGroupThread.h>
2017-12-19 03:42:50 +01:00
#import <YapDatabase/YapDatabase.h>
#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN
#define NEEDS_TO_REGISTER_PUSH_KEY @"Register For Push"
#define NEEDS_TO_REGISTER_ATTRIBUTES @"Register Attributes"
@implementation VersionMigrations
2018-11-26 16:24:36 +01:00
#pragma mark - Dependencies
+ (TSAccountManager *)tsAccountManager
{
OWSAssertDebug(SSKEnvironment.shared.tsAccountManager);
return SSKEnvironment.shared.tsAccountManager;
}
#pragma mark - Utility methods
2015-04-25 16:59:32 +02:00
+ (void)performUpdateCheckWithCompletion:(VersionMigrationCompletion)completion
{
OWSLogInfo(@"");
// performUpdateCheck must be invoked after Environment has been initialized because
// upgrade process may depend on Environment.
OWSAssertDebug(Environment.shared);
OWSAssertDebug(completion);
2018-08-02 21:18:40 +02:00
NSString *previousVersion = AppVersion.sharedInstance.lastAppVersion;
NSString *currentVersion = AppVersion.sharedInstance.currentAppVersion;
OWSLogInfo(@"Checking migrations. currentVersion: %@, lastRanVersion: %@", currentVersion, previousVersion);
2015-04-25 16:59:32 +02:00
if (!previousVersion) {
OWSLogInfo(@"No previous version found. Probably first launch since install - nothing to migrate.");
2018-11-27 22:59:38 +01:00
OWSDatabaseMigrationRunner *runner = [[OWSDatabaseMigrationRunner alloc] init];
2021-01-22 04:25:23 +01:00
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
2015-04-25 16:59:32 +02:00
return;
}
2018-11-26 16:24:36 +01:00
if ([self isVersion:previousVersion atLeast:@"2.0.0" andLessThan:@"2.1.70"] && [self.tsAccountManager isRegistered]) {
2015-04-25 16:59:32 +02:00
[self clearVideoCache];
}
2018-11-26 16:24:36 +01:00
if ([self isVersion:previousVersion atLeast:@"2.0.0" andLessThan:@"2.3.0"] && [self.tsAccountManager isRegistered]) {
[self clearBloomFilterCache];
}
2018-04-24 19:15:11 +02:00
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
2018-11-27 22:59:38 +01:00
[[[OWSDatabaseMigrationRunner alloc] init] runAllOutstandingWithCompletion:completion];
2018-04-24 19:15:11 +02:00
});
}
+ (BOOL)isVersion:(NSString *)thisVersionString
atLeast:(NSString *)openLowerBoundVersionString
2017-12-04 18:38:44 +01:00
andLessThan:(NSString *)closedUpperBoundVersionString
{
return [self isVersion:thisVersionString atLeast:openLowerBoundVersionString] &&
2017-12-04 18:38:44 +01:00
[self isVersion:thisVersionString lessThan:closedUpperBoundVersionString];
2015-04-25 16:59:32 +02:00
}
2017-12-04 18:38:44 +01:00
+ (BOOL)isVersion:(NSString *)thisVersionString atLeast:(NSString *)thatVersionString
{
2015-04-25 16:59:32 +02:00
return [thisVersionString compare:thatVersionString options:NSNumericSearch] != NSOrderedAscending;
}
2017-12-04 18:38:44 +01:00
+ (BOOL)isVersion:(NSString *)thisVersionString lessThan:(NSString *)thatVersionString
{
2015-04-25 16:59:32 +02:00
return [thisVersionString compare:thatVersionString options:NSNumericSearch] == NSOrderedAscending;
}
2017-07-24 17:06:19 +02:00
#pragma mark Upgrading to 2.1 - Removing video cache folder
2017-12-04 18:38:44 +01:00
+ (void)clearVideoCache
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
2015-04-25 16:59:32 +02:00
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
2017-12-04 18:38:44 +01:00
basePath = [basePath stringByAppendingPathComponent:@"videos"];
2015-04-25 16:59:32 +02:00
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:basePath]) {
2015-04-25 16:59:32 +02:00
[NSFileManager.defaultManager removeItemAtPath:basePath error:&error];
}
if (error) {
OWSLogError(
2017-12-04 18:38:44 +01:00
@"An error occured while removing the videos cache folder from old location: %@", error.debugDescription);
}
}
#pragma mark Upgrading to 2.3.0
// We removed bloom filter contact discovery. Clean up any local bloom filter data.
2017-12-04 18:38:44 +01:00
+ (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]) {
OWSLogInfo(@"Successfully removed bloom filter cache.");
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[transaction removeAllObjectsInCollection:@"TSRecipient"];
2020-11-04 01:46:30 +01:00
}];
OWSLogInfo(@"Removed all TSRecipient records - will be replaced by SignalRecipients at next address sync.");
} else {
OWSLogError(@"Failed to remove bloom filter cache with error: %@", deleteError.localizedDescription);
}
} else {
OWSLogDebug(@"No bloom filter cache to remove.");
}
}
@end
NS_ASSUME_NONNULL_END