diff --git a/SessionMessagingKit/Configuration.swift b/SessionMessagingKit/Configuration.swift index 0be9fa4f0..de6575edb 100644 --- a/SessionMessagingKit/Configuration.swift +++ b/SessionMessagingKit/Configuration.swift @@ -33,7 +33,7 @@ public enum SNMessagingKit { // Just to make the external API nice // Wait until the feature is turned on before doing the migration that generates // the config dump data // FIXME: Remove this once `useSharedUtilForUserConfig` is permanent - (Features.useSharedUtilForUserConfig ? + (Features.useSharedUtilForUserConfig() ? _014_GenerateInitialUserConfigDumps.self : (nil as Migration.Type?) ) diff --git a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ConfigurationMessages.swift b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ConfigurationMessages.swift index d2814eeea..92385940f 100644 --- a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ConfigurationMessages.swift +++ b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ConfigurationMessages.swift @@ -9,7 +9,7 @@ import SessionUtilitiesKit extension MessageReceiver { internal static func handleLegacyConfigurationMessage(_ db: Database, message: ConfigurationMessage) throws { // FIXME: Remove this once `useSharedUtilForUserConfig` is permanent - guard !SessionUtil.userConfigsEnabled else { + guard !SessionUtil.userConfigsEnabled(db) else { TopBannerController.show(warning: .outdatedUserConfig) return } diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift index a524634df..d4ab2e3c7 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift @@ -189,7 +189,7 @@ public enum MessageReceiver { // the config then the message will be dropped) guard !Message.requiresExistingConversation(message: message, threadVariant: threadVariant) || - SessionUtil.conversationInConfig(threadId: threadId, threadVariant: threadVariant, visibleOnly: false) + SessionUtil.conversationInConfig(db, threadId: threadId, threadVariant: threadVariant, visibleOnly: false) else { throw MessageReceiverError.requiredThreadNotInConfig } switch message { diff --git a/SessionMessagingKit/SessionUtil/Config Handling/SessionUtil+Shared.swift b/SessionMessagingKit/SessionUtil/Config Handling/SessionUtil+Shared.swift index 6444f8744..4a8c7e9d2 100644 --- a/SessionMessagingKit/SessionUtil/Config Handling/SessionUtil+Shared.swift +++ b/SessionMessagingKit/SessionUtil/Config Handling/SessionUtil+Shared.swift @@ -299,12 +299,13 @@ internal extension SessionUtil { public extension SessionUtil { static func conversationInConfig( + _ db: Database? = nil, threadId: String, threadVariant: SessionThread.Variant, visibleOnly: Bool ) -> Bool { // FIXME: Remove this once `useSharedUtilForUserConfig` is permanent - guard SessionUtil.userConfigsEnabled else { return true } + guard SessionUtil.userConfigsEnabled(db) else { return true } let configVariant: ConfigDump.Variant = { switch threadVariant { diff --git a/SessionMessagingKit/SessionUtil/SessionUtil.swift b/SessionMessagingKit/SessionUtil/SessionUtil.swift index 16c7cf5a8..a5ffd851b 100644 --- a/SessionMessagingKit/SessionUtil/SessionUtil.swift +++ b/SessionMessagingKit/SessionUtil/SessionUtil.swift @@ -6,6 +6,28 @@ import SessionSnodeKit import SessionUtil import SessionUtilitiesKit +// MARK: - Features + +public extension Features { + static func useSharedUtilForUserConfig(_ db: Database? = nil) -> Bool { + // TODO: Need to set this timestamp to the correct date + guard Date().timeIntervalSince1970 < 1893456000 else { return true } + guard !SessionUtil.hasCheckedMigrationsCompleted.wrappedValue else { + return SessionUtil.userConfigsEnabledIgnoringFeatureFlag + } + + if let db: Database = db { + return SessionUtil.refreshingUserConfigsEnabled(db) + } + + return Storage.shared + .read { db in SessionUtil.refreshingUserConfigsEnabled(db) } + .defaulting(to: false) + } +} + +// MARK: - SessionUtil + public enum SessionUtil { public struct ConfResult { let needsPush: Bool @@ -63,6 +85,7 @@ public enum SessionUtil { public static var libSessionVersion: String { String(cString: LIBSESSION_UTIL_VERSION_STR) } + fileprivate static let hasCheckedMigrationsCompleted: Atomic = Atomic(false) private static let requiredMigrationsCompleted: Atomic = Atomic(false) private static let requiredMigrationIdentifiers: Set = [ TargetMigrations.Identifier.messagingKit.key(with: _013_SessionUtilChanges.self), @@ -70,8 +93,16 @@ public enum SessionUtil { ] public static var userConfigsEnabled: Bool { - Features.useSharedUtilForUserConfig && - requiredMigrationsCompleted.wrappedValue + return userConfigsEnabled(nil) + } + + public static func userConfigsEnabled(_ db: Database?) -> Bool { + Features.useSharedUtilForUserConfig(db) && + SessionUtil.userConfigsEnabledIgnoringFeatureFlag + } + + public static var userConfigsEnabledIgnoringFeatureFlag: Bool { + SessionUtil.requiredMigrationsCompleted.wrappedValue } internal static func userConfigsEnabled( @@ -80,9 +111,9 @@ public enum SessionUtil { ) -> Bool { // First check if we are enabled regardless of what we want to ignore guard - Features.useSharedUtilForUserConfig, - !requiredMigrationsCompleted.wrappedValue, - !refreshingUserConfigsEnabled(db), + Features.useSharedUtilForUserConfig(db), + !SessionUtil.requiredMigrationsCompleted.wrappedValue, + !SessionUtil.refreshingUserConfigsEnabled(db), ignoreRequirementsForRunningMigrations, let currentlyRunningMigration: (identifier: TargetMigrations.Identifier, migration: Migration.Type) = Storage.shared.currentlyRunningMigration else { return true } @@ -99,6 +130,7 @@ public enum SessionUtil { .isSuperset(of: SessionUtil.requiredMigrationIdentifiers) requiredMigrationsCompleted.mutate { $0 = result } + hasCheckedMigrationsCompleted.mutate { $0 = true } return result } @@ -375,7 +407,7 @@ public enum SessionUtil { publicKey: String ) throws { // FIXME: Remove this once `useSharedUtilForUserConfig` is permanent - guard SessionUtil.userConfigsEnabled else { return } + guard SessionUtil.userConfigsEnabled(db) else { return } guard !messages.isEmpty else { return } guard !publicKey.isEmpty else { throw MessageReceiverError.noThread } diff --git a/SessionMessagingKit/Utilities/ProfileManager.swift b/SessionMessagingKit/Utilities/ProfileManager.swift index 14f748f9a..259e928d3 100644 --- a/SessionMessagingKit/Utilities/ProfileManager.swift +++ b/SessionMessagingKit/Utilities/ProfileManager.swift @@ -593,7 +593,7 @@ public struct ProfileManager { ) } // FIXME: Remove this once `useSharedUtilForUserConfig` is permanent - else if !SessionUtil.userConfigsEnabled { + else if !SessionUtil.userConfigsEnabled(db) { // If we have a contact record for the profile (ie. it's a synced profile) then // should should send an updated config message, otherwise we should just update // the local state (the shared util has this logic build in to it's handling) diff --git a/SessionUtilitiesKit/General/Features.swift b/SessionUtilitiesKit/General/Features.swift index 3cc3d8e83..970315b2d 100644 --- a/SessionUtilitiesKit/General/Features.swift +++ b/SessionUtilitiesKit/General/Features.swift @@ -5,6 +5,4 @@ import Foundation public final class Features { public static let useOnionRequests: Bool = true public static let useTestnet: Bool = false - - public static let useSharedUtilForUserConfig: Bool = true // TODO: Base this off a timestamp }