mirror of
https://github.com/lux-tel/session-ios.git
synced 2026-10-30 20:38:32 +00:00
The `SessionThreadViewModel` has been renamed to `ConversationInfoViewModel` and is now a simple struct (as opposed to a FetchableRecord with a handful of behemoth database queries) The `ConversationInfoViewModel` and `MessageViewModel` now get initialised with a `ConversationDataCache` which is an object that holds a bunch of [ID -> Data] stores for various types that are needed to construct the aforementioned view models. The `ConversationDataCache` will generally be populated via the `ConversationDataHelper` which, when combined with our `ObservableEvent` system, is where the majority of the work for this refactoring has gone. This helper essentially has 4 parts: - `determineFetchRequirements`: Reviews any events which have come through and determines whether anything needs to be fetched from the database - `applyNonDatabaseEvents`: Updates the cached data directly based on delta information included in the `ObservableEvent` instances - `fetchFromDatabase`: Fetches any data from the database based on the info returned from `determineFetchRequirements`, this function will also resolve any dependant data that newly fetched objects need if they aren't already in the cache - `fetchFromLibSession`: Retrieves requested data from libSession Using the above setup all screens which contain conversation or message data now use these two view models, and can share the state of the view models between each other to reduce the need to refetch data when entering a new screen Some additional changes included due to the above are: • No longer need to pass `displayNameRetriever` throughout • Removed deprecated display name retrieval functions • Removed some duplicate code • Removed some synchronous database query hacks we needed until refactoring had been completed • Refactored mentions to be handled by tags • Refactored and optimised the searching logic (also added some caching) • Refactored the ThreadSettingsViewModel to use the new ObservationBuilder system • Refactored the ThreadPickerViewModel to use the new ObservationBuilder system • Refactored the HomeViewModel to retrieve data using the `ConversationDataHelper` • Refactored the ConversationViewModel to retrieve data using the `ConversationDataHelper` • Fixed an issue where communities weren't getting the correct "joined" timestamp
161 lines
5.0 KiB
Swift
161 lines
5.0 KiB
Swift
// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
import GRDB
|
|
|
|
public enum Authentication {}
|
|
public protocol AuthenticationMethod: Sendable, SignatureGenerator {
|
|
var info: Authentication.Info { get }
|
|
}
|
|
|
|
public extension AuthenticationMethod {
|
|
var isInvalid: Bool {
|
|
switch info {
|
|
case .standard(let sessionId, let ed25519PublicKey):
|
|
return (sessionId == .invalid || ed25519PublicKey.isEmpty)
|
|
|
|
default: return false
|
|
}
|
|
}
|
|
|
|
var swarmPublicKey: String {
|
|
get throws {
|
|
switch info {
|
|
case .standard(let sessionId, _), .groupAdmin(let sessionId, _), .groupMember(let sessionId, _):
|
|
return sessionId.hexString
|
|
|
|
case .community: throw CryptoError.invalidAuthentication
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public extension Authentication {
|
|
static let invalid: AuthenticationMethod = Invalid()
|
|
|
|
struct Invalid: AuthenticationMethod {
|
|
public var info: Authentication.Info = .standard(sessionId: .invalid, ed25519PublicKey: [])
|
|
|
|
public func generateSignature(with verificationBytes: [UInt8], using dependencies: Dependencies) throws -> Authentication.Signature {
|
|
throw CryptoError.invalidAuthentication
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct EquatableAuthenticationMethod: Sendable, Equatable {
|
|
public let value: AuthenticationMethod
|
|
|
|
public init(value: AuthenticationMethod) {
|
|
self.value = value
|
|
}
|
|
|
|
public static func ==(lhs: EquatableAuthenticationMethod, rhs: EquatableAuthenticationMethod) -> Bool {
|
|
return (lhs.value.info == rhs.value.info)
|
|
}
|
|
}
|
|
|
|
// MARK: - SignatureGenerator
|
|
|
|
public protocol SignatureGenerator {
|
|
func generateSignature(
|
|
with verificationBytes: [UInt8],
|
|
using dependencies: Dependencies
|
|
) throws -> Authentication.Signature
|
|
}
|
|
|
|
// MARK: - Signature Verification
|
|
|
|
public extension Authentication {
|
|
static func verify(
|
|
signature: Authentication.Signature,
|
|
publicKey: [UInt8],
|
|
verificationBytes: [UInt8],
|
|
using dependencies: Dependencies
|
|
) -> Bool {
|
|
switch signature {
|
|
case .standard(let signature):
|
|
return dependencies[singleton: .crypto].verify(
|
|
.signature(message: verificationBytes, publicKey: publicKey, signature: signature)
|
|
)
|
|
|
|
// Currently we never sign anything with a subaccount which requires verification
|
|
case .subaccount: return false
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Authentication.Info
|
|
|
|
public extension Authentication {
|
|
enum Info: Sendable, Equatable {
|
|
/// Used when interacting as the current user
|
|
case standard(sessionId: SessionId, ed25519PublicKey: [UInt8])
|
|
|
|
/// Used when interacting as a group admin
|
|
case groupAdmin(groupSessionId: SessionId, ed25519SecretKey: [UInt8])
|
|
|
|
/// Used when interacting as a group member
|
|
case groupMember(groupSessionId: SessionId, authData: Data)
|
|
|
|
/// Used when interacting with a community
|
|
case community(
|
|
server: String,
|
|
publicKey: String,
|
|
hasCapabilities: Bool,
|
|
supportsBlinding: Bool,
|
|
forceBlinded: Bool
|
|
)
|
|
}
|
|
}
|
|
|
|
// MARK: - Authentication.Signature
|
|
|
|
public extension Authentication {
|
|
enum Signature: Equatable, CustomStringConvertible {
|
|
/// Used for signing standard requests
|
|
case standard(signature: [UInt8])
|
|
|
|
/// Used for signing standard requests
|
|
case subaccount(subaccount: [UInt8], subaccountSig: [UInt8], signature: [UInt8])
|
|
|
|
// MARK: - CustomStringConvertible
|
|
|
|
public var description: String {
|
|
switch self {
|
|
case .standard(let signature):
|
|
return """
|
|
standard(
|
|
signature: \(signature.toHexString())
|
|
)
|
|
"""
|
|
|
|
case .subaccount(let subaccount, let subaccountSig, let signature):
|
|
return """
|
|
subaccount(
|
|
subaccount: \(subaccount.toHexString()),
|
|
subaccountSig: \(subaccountSig.toHexString()),
|
|
signature: \(signature.toHexString())
|
|
)
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public extension Optional where Wrapped == Authentication.Signature {
|
|
var nullIfEmpty: Authentication.Signature? {
|
|
switch self {
|
|
case .none: return nil
|
|
case .standard(let signature):
|
|
guard !signature.isEmpty else { return nil }
|
|
|
|
return self
|
|
|
|
case .subaccount(let subaccount, let subaccountSig, let signature):
|
|
guard !subaccount.isEmpty, !subaccountSig.isEmpty, !signature.isEmpty else { return nil }
|
|
|
|
return self
|
|
}
|
|
}
|
|
}
|