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.
session-ios/SessionMessagingKit/Database/Migrations/_015_BlockCommunityMessageR...

48 lines
2.2 KiB
Swift

// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
import SessionUtilitiesKit
/// This migration adds a flag indicating whether a profile has indicated it is blocking community message requests
enum _015_BlockCommunityMessageRequests: Migration {
static let target: TargetMigrations.Identifier = .messagingKit
static let identifier: String = "BlockCommunityMessageRequests"
static let minExpectedRunDuration: TimeInterval = 0.01
static var requirements: [MigrationRequirement] = [.sessionIdCached, .libSessionStateLoaded]
static let fetchedTables: [(TableRecord & FetchableRecord).Type] = [
Identity.self, Setting.self
]
static let createdOrAlteredTables: [(TableRecord & FetchableRecord).Type] = [Profile.self]
static let droppedTables: [(TableRecord & FetchableRecord).Type] = []
static func migrate(_ db: Database, using dependencies: Dependencies) throws {
// Add the new 'Profile' properties
try db.alter(table: Profile.self) { t in
t.add(.blocksCommunityMessageRequests, .boolean)
t.add(.lastBlocksCommunityMessageRequests, .integer).defaults(to: 0)
}
// If the user exists and the 'checkForCommunityMessageRequests' hasn't already been set then default it to "false"
if
Identity.userExists(db, using: dependencies),
(try Setting.exists(db, id: Setting.BoolKey.checkForCommunityMessageRequests.rawValue)) == false
{
let userSessionId: SessionId = dependencies[cache: .general].sessionId
let rawBlindedMessageRequestValue: Int32 = try dependencies.mutate(cache: .libSession) { cache in
try LibSession.rawBlindedMessageRequestValue(
in: cache.config(for: .userProfile, sessionId: userSessionId)
)
}
// Use the value in the config if we happen to have one, otherwise use the default
db[.checkForCommunityMessageRequests] = (rawBlindedMessageRequestValue < 0 ?
true :
(rawBlindedMessageRequestValue > 0)
)
}
Storage.update(progress: 1, for: self, in: target, using: dependencies)
}
}