mirror of https://github.com/oxen-io/session-ios
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.
60 lines
1.8 KiB
Swift
60 lines
1.8 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
import GRDB
|
|
|
|
// MARK: - General.Cache
|
|
|
|
public enum General {
|
|
public class Cache: GeneralCacheType {
|
|
public var sessionId: SessionId? = nil
|
|
public var recentReactionTimestamps: [Int64] = []
|
|
}
|
|
}
|
|
|
|
public extension Cache {
|
|
static let general: CacheConfig<GeneralCacheType, ImmutableGeneralCacheType> = Dependencies.create(
|
|
identifier: "general",
|
|
createInstance: { _ in General.Cache() },
|
|
mutableInstance: { $0 },
|
|
immutableInstance: { $0 }
|
|
)
|
|
}
|
|
|
|
// MARK: - GeneralError
|
|
|
|
public enum GeneralError: Error {
|
|
case invalidSeed
|
|
case keyGenerationFailed
|
|
case randomGenerationFailed
|
|
}
|
|
|
|
// MARK: - Convenience
|
|
|
|
public func getUserSessionId(_ db: Database? = nil, using dependencies: Dependencies = Dependencies()) -> SessionId {
|
|
if let cachedSessionId: SessionId = dependencies[cache: .general].sessionId { return cachedSessionId }
|
|
|
|
// Can be nil under some circumstances
|
|
if let publicKey: Data = Identity.fetchUserPublicKey(db, using: dependencies) {
|
|
let sessionId: SessionId = SessionId(.standard, publicKey: publicKey.bytes)
|
|
|
|
dependencies.mutate(cache: .general) { $0.sessionId = sessionId }
|
|
return sessionId
|
|
}
|
|
|
|
return SessionId.invalid
|
|
}
|
|
|
|
// MARK: - GeneralCacheType
|
|
|
|
/// This is a read-only version of the Cache designed to avoid unintentionally mutating the instance in a non-thread-safe way
|
|
public protocol ImmutableGeneralCacheType: ImmutableCacheType {
|
|
var sessionId: SessionId? { get }
|
|
var recentReactionTimestamps: [Int64] { get }
|
|
}
|
|
|
|
public protocol GeneralCacheType: ImmutableGeneralCacheType, MutableCacheType {
|
|
var sessionId: SessionId? { get set }
|
|
var recentReactionTimestamps: [Int64] { get set }
|
|
}
|