2014-07-30 08:05:36 +02:00
|
|
|
//
|
2019-01-08 17:25:03 +01:00
|
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
2014-07-30 08:05:36 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "VersionMigrations.h"
|
2015-01-11 17:48:18 +01:00
|
|
|
#import "Environment.h"
|
2016-09-28 18:21:14 +02:00
|
|
|
#import "OWSDatabaseMigrationRunner.h"
|
2015-01-11 17:48:18 +01:00
|
|
|
#import "SignalKeyingStorage.h"
|
2019-03-21 15:55:04 +01:00
|
|
|
#import <SignalMessaging/SignalMessaging-Swift.h>
|
2020-06-05 02:38:44 +02:00
|
|
|
#import <SessionServiceKit/AppContext.h>
|
|
|
|
#import <SessionServiceKit/AppVersion.h>
|
|
|
|
#import <SessionServiceKit/NSUserDefaults+OWS.h>
|
|
|
|
#import <SessionServiceKit/OWSPrimaryStorage+Loki.h>
|
|
|
|
#import <SessionServiceKit/OWSRequestFactory.h>
|
|
|
|
#import <SessionServiceKit/TSAccountManager.h>
|
|
|
|
#import <SessionServiceKit/TSNetworkManager.h>
|
|
|
|
#import <SessionServiceKit/TSThread.h>
|
|
|
|
#import <SessionServiceKit/TSGroupThread.h>
|
2017-12-19 03:42:50 +01:00
|
|
|
#import <YapDatabase/YapDatabase.h>
|
2015-03-12 00:46:31 +01:00
|
|
|
|
2018-01-30 17:27:44 +01:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
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;
|
2016-09-28 18:21:14 +02:00
|
|
|
|
2015-01-11 17:48:18 +01:00
|
|
|
@end
|
|
|
|
|
2014-07-30 08:05:36 +02:00
|
|
|
@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
|
|
|
|
2018-01-30 17:27:44 +01:00
|
|
|
+ (void)performUpdateCheckWithCompletion:(VersionMigrationCompletion)completion
|
2016-08-01 00:25:07 +02:00
|
|
|
{
|
2018-08-27 18:09:39 +02:00
|
|
|
OWSLogInfo(@"");
|
2018-04-23 21:49:20 +02:00
|
|
|
|
2017-03-13 14:33:58 +01:00
|
|
|
// performUpdateCheck must be invoked after Environment has been initialized because
|
|
|
|
// upgrade process may depend on Environment.
|
2018-08-31 19:16:31 +02:00
|
|
|
OWSAssertDebug(Environment.shared);
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(completion);
|
2017-03-13 14:33:58 +01:00
|
|
|
|
2018-08-02 21:18:40 +02:00
|
|
|
NSString *previousVersion = AppVersion.sharedInstance.lastAppVersion;
|
|
|
|
NSString *currentVersion = AppVersion.sharedInstance.currentAppVersion;
|
2016-11-14 18:47:23 +01:00
|
|
|
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Checking migrations. currentVersion: %@, lastRanVersion: %@", currentVersion, previousVersion);
|
2016-11-14 18:47:23 +01:00
|
|
|
|
2015-04-25 16:59:32 +02:00
|
|
|
if (!previousVersion) {
|
2018-08-27 18:00:28 +02:00
|
|
|
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];
|
2016-09-28 18:21:14 +02:00
|
|
|
[runner assumeAllExistingMigrationsRun];
|
2018-01-30 17:27:44 +01:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
completion();
|
|
|
|
});
|
2015-04-25 16:59:32 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2019-08-30 04:12:32 +02:00
|
|
|
/*
|
2016-11-14 18:47:23 +01:00
|
|
|
if ([self isVersion:previousVersion atLeast:@"1.0.2" andLessThan:@"2.0"]) {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogError(@"Migrating from RedPhone no longer supported. Quitting.");
|
2016-11-14 18:47:23 +01:00
|
|
|
// Not translating these as so few are affected.
|
2019-03-21 15:55:04 +01:00
|
|
|
UIAlertController *alert = [UIAlertController
|
2016-11-14 18:47:23 +01:00
|
|
|
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) {
|
2018-09-27 20:54:29 +02:00
|
|
|
OWSFail(@"Obsolete install.");
|
2016-11-14 18:47:23 +01:00
|
|
|
}];
|
2019-03-21 15:55:04 +01:00
|
|
|
[alert addAction:quitAction];
|
2016-11-14 18:47:23 +01:00
|
|
|
|
2019-03-21 15:55:04 +01:00
|
|
|
[CurrentAppContext().frontmostViewController presentAlert:alert];
|
2015-04-25 16:59:32 +02:00
|
|
|
}
|
2019-08-30 04:12:32 +02:00
|
|
|
*/
|
2015-12-22 12:45:09 +01:00
|
|
|
|
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];
|
2015-04-28 14:41:50 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2018-11-26 16:24:36 +01:00
|
|
|
if ([self isVersion:previousVersion atLeast:@"2.0.0" andLessThan:@"2.3.0"] && [self.tsAccountManager isRegistered]) {
|
2016-04-18 20:17:08 +02:00
|
|
|
[self clearBloomFilterCache];
|
2015-11-29 01:14:49 +01:00
|
|
|
}
|
2019-10-09 03:40:39 +02:00
|
|
|
|
|
|
|
// Loki
|
|
|
|
if ([self isVersion:previousVersion lessThan:@"1.2.1"] && [self.tsAccountManager isRegistered]) {
|
|
|
|
[self updatePublicChatMapping];
|
|
|
|
}
|
2016-08-01 00:25:07 +02:00
|
|
|
|
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
|
|
|
});
|
2017-06-15 19:43:18 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
+ (BOOL)isVersion:(NSString *)thisVersionString
|
|
|
|
atLeast:(NSString *)openLowerBoundVersionString
|
2017-12-04 18:38:44 +01:00
|
|
|
andLessThan:(NSString *)closedUpperBoundVersionString
|
|
|
|
{
|
2015-12-22 12:45:09 +01:00
|
|
|
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
|
2015-03-12 00:46:31 +01:00
|
|
|
|
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-12-22 12:45:09 +01:00
|
|
|
|
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) {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogError(
|
2017-12-04 18:38:44 +01:00
|
|
|
@"An error occured while removing the videos cache folder from old location: %@", error.debugDescription);
|
2015-08-23 00:00:39 +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.
|
2017-12-04 18:38:44 +01:00
|
|
|
+ (void)clearBloomFilterCache
|
|
|
|
{
|
|
|
|
NSFileManager *fm = [NSFileManager defaultManager];
|
|
|
|
NSArray *cachesDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
2016-04-18 20:17:08 +02:00
|
|
|
NSString *bloomFilterPath = [[cachesDir objectAtIndex:0] stringByAppendingPathComponent:@"bloomfilter"];
|
|
|
|
|
|
|
|
if ([fm fileExistsAtPath:bloomFilterPath]) {
|
|
|
|
NSError *deleteError;
|
|
|
|
if ([fm removeItemAtPath:bloomFilterPath error:&deleteError]) {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogInfo(@"Successfully removed bloom filter cache.");
|
2018-03-05 15:30:58 +01:00
|
|
|
[OWSPrimaryStorage.dbReadWriteConnection
|
2017-07-05 22:58:25 +02:00
|
|
|
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
[transaction removeAllObjectsInCollection:@"TSRecipient"];
|
|
|
|
}];
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogInfo(@"Removed all TSRecipient records - will be replaced by SignalRecipients at next address sync.");
|
2016-04-18 20:17:08 +02:00
|
|
|
} else {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogError(@"Failed to remove bloom filter cache with error: %@", deleteError.localizedDescription);
|
2016-04-18 20:17:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogDebug(@"No bloom filter cache to remove.");
|
2016-04-18 20:17:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 05:40:18 +02:00
|
|
|
# pragma mark Loki - Upgrading to Public Chat Manager
|
2019-10-09 03:40:39 +02:00
|
|
|
|
2019-10-15 01:50:06 +02:00
|
|
|
// Versions less than or equal to 1.2.0 didn't store public chat mappings
|
2019-10-09 03:40:39 +02:00
|
|
|
+ (void)updatePublicChatMapping
|
|
|
|
{
|
|
|
|
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction * _Nonnull transaction) {
|
2019-10-15 01:50:06 +02:00
|
|
|
for (LKPublicChat *chat in LKPublicChatAPI.defaultChats) {
|
2020-01-28 05:08:42 +01:00
|
|
|
TSGroupThread *thread = [TSGroupThread threadWithGroupId:[LKGroupUtilities getEncodedOpenGroupIDAsData:chat.id] transaction:transaction];
|
2019-10-09 03:40:39 +02:00
|
|
|
if (thread != nil) {
|
2019-10-15 01:29:41 +02:00
|
|
|
[LKDatabaseUtilities setPublicChat:chat threadID:thread.uniqueId transaction:transaction];
|
2020-01-29 04:58:28 +01:00
|
|
|
} else {
|
|
|
|
// Update the group type and group ID for private group chat version.
|
|
|
|
// If the thread is still using the old group ID, it needs to be updated.
|
2020-01-22 00:23:35 +01:00
|
|
|
thread = [TSGroupThread threadWithGroupId:chat.idAsData transaction:transaction];
|
|
|
|
if (thread != nil) {
|
2020-01-28 05:08:42 +01:00
|
|
|
thread.groupModel.groupType = openGroup;
|
|
|
|
[thread.groupModel updateGroupId:[LKGroupUtilities getEncodedOpenGroupIDAsData:chat.id]];
|
2020-01-22 00:23:35 +01:00
|
|
|
[thread saveWithTransaction:transaction];
|
|
|
|
[LKDatabaseUtilities setPublicChat:chat threadID:thread.uniqueId transaction:transaction];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 04:58:28 +01:00
|
|
|
// Update RSS feeds here
|
2020-01-22 00:23:35 +01:00
|
|
|
LKRSSFeed *lokiNewsFeed = [[LKRSSFeed alloc] initWithId:@"loki.network.feed" server:@"https://loki.network/feed/" displayName:NSLocalizedString(@"Loki News", @"") isDeletable:true];
|
2020-02-04 03:03:09 +01:00
|
|
|
LKRSSFeed *lokiMessengerUpdatesFeed = [[LKRSSFeed alloc] initWithId:@"loki.network.messenger-updates.feed" server:@"https://loki.network/category/messenger-updates/feed/" displayName:NSLocalizedString(@"Session Updates", @"") isDeletable:false];
|
2020-01-22 00:23:35 +01:00
|
|
|
NSArray *feeds = @[ lokiNewsFeed, lokiMessengerUpdatesFeed ];
|
|
|
|
for (LKRSSFeed *feed in feeds) {
|
|
|
|
TSGroupThread *thread = [TSGroupThread threadWithGroupId:[feed.id dataUsingEncoding:NSUTF8StringEncoding] transaction:transaction];
|
|
|
|
if (thread != nil) {
|
2020-01-28 05:08:42 +01:00
|
|
|
thread.groupModel.groupType = rssFeed;
|
|
|
|
[thread.groupModel updateGroupId:[LKGroupUtilities getEncodedRSSFeedIDAsData:feed.id]];
|
2020-01-22 00:23:35 +01:00
|
|
|
[thread saveWithTransaction:transaction];
|
|
|
|
}
|
2019-10-09 03:40:39 +02:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2014-07-30 08:05:36 +02:00
|
|
|
@end
|
2018-01-30 17:27:44 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|