Ask what a backend supports instead of what it is

This commit is contained in:
Arkadiusz Fal
2026-09-07 18:48:50 +02:00
parent dca8e2fbe4
commit 5ac6b7188d
13 changed files with 187 additions and 124 deletions

View File

@@ -31,6 +31,19 @@ struct AccountCapabilities: Hashable, Sendable {
supportsPlaylistPrivacy: false
)
/// Whether the backend has accounts at all — true as soon as an account
/// could do anything on it. This is what decides whether an instance offers
/// a sign-in; it is deliberately broader than ``isSelectableContext``, which
/// asks the narrower question of whether an account is worth switching *to*.
var supportsAccounts: Bool {
supportsSubscriptions
|| supportsFeed
|| supportsPlaylistReads
|| supportsPlaylistWrites
|| supportsWatchHistory
|| supportsPlaylistPrivacy
}
/// Whether this account can act as a `DataContext` at all. An account with
/// nothing to offer is never listed as a switch target.
var isSelectableContext: Bool {

View File

@@ -67,9 +67,6 @@ struct Instance: Identifiable, Codable, Hashable, Sendable {
/// The date this instance was added.
let dateAdded: Date
/// Optional API key if required by the instance.
var apiKey: String?
/// Whether to allow invalid/self-signed SSL certificates.
var allowInvalidCertificates: Bool
@@ -82,8 +79,21 @@ struct Instance: Identifiable, Codable, Hashable, Sendable {
/// Yattee Server always requires basic auth regardless of this flag.
var usesBasicAuth: Bool
/// Whether the user has logged into an account on this instance (Invidious/Piped).
/// Set on login, so a missing Keychain credential can be detected after reinstall.
/// Whether a pre-revamp single login existed on this instance.
///
/// Migration-only state. Signing in no longer sets it (see
/// `AccountSignInHandler`) — accounts describe logins now. It survives
/// because `AccountsManager.reconcileLegacyAccounts()` reads it to
/// synthesize the account an upgrading install should have, and because a
/// missing Keychain credential on an instance that has no accounts yet can
/// still only be detected through it.
///
/// Everything that writes it is migration machinery:
/// `LegacyDataMigrationService` raises it for a login imported from
/// Yattee 1, `AppEnvironment.backfillCredentialFlags()` revives it when the
/// flag was lost but its instance-keyed credential was not, and
/// `AppEnvironment.signOutAndRemoveAccount(_:)` lowers it once the last
/// account on the instance is gone.
var usesAccountLogin: Bool
// MARK: - Initialization
@@ -95,7 +105,6 @@ struct Instance: Identifiable, Codable, Hashable, Sendable {
name: String? = nil,
isEnabled: Bool = true,
dateAdded: Date = Date(),
apiKey: String? = nil,
allowInvalidCertificates: Bool = false,
proxiesVideos: Bool = false,
usesBasicAuth: Bool = false,
@@ -107,7 +116,6 @@ struct Instance: Identifiable, Codable, Hashable, Sendable {
self.name = name
self.isEnabled = isEnabled
self.dateAdded = dateAdded
self.apiKey = apiKey
self.allowInvalidCertificates = allowInvalidCertificates
self.proxiesVideos = proxiesVideos
self.usesBasicAuth = usesBasicAuth
@@ -122,7 +130,6 @@ struct Instance: Identifiable, Codable, Hashable, Sendable {
name = try container.decodeIfPresent(String.self, forKey: .name)
isEnabled = try container.decode(Bool.self, forKey: .isEnabled)
dateAdded = try container.decode(Date.self, forKey: .dateAdded)
apiKey = try container.decodeIfPresent(String.self, forKey: .apiKey)
allowInvalidCertificates = try container.decode(Bool.self, forKey: .allowInvalidCertificates)
proxiesVideos = try container.decodeIfPresent(Bool.self, forKey: .proxiesVideos) ?? false
usesBasicAuth = try container.decodeIfPresent(Bool.self, forKey: .usesBasicAuth) ?? false
@@ -175,13 +182,18 @@ extension Instance {
}
/// Whether this instance supports user authentication/login.
///
/// Answered by the capability table rather than a type list: an instance
/// offers a sign-in exactly when an account on it can do something, so a
/// backend that gains accounts gets its login by adding one row in
/// ``InstanceType/accountCapabilities``.
var supportsAuthentication: Bool {
type == .invidious || type == .piped
type.accountCapabilities.supportsAccounts
}
/// Whether this instance supports subscription feed.
var supportsFeed: Bool {
type == .invidious || type == .piped
type.accountCapabilities.supportsFeed
}
/// Whether this instance supports search suggestions/autocomplete.

View File

@@ -2,79 +2,35 @@
// SubscriptionAccountType.swift
// Yattee
//
// Subscription account type and configuration for managing channel subscriptions.
// Pre-revamp subscription account state, kept only so it can be decoded.
//
import Foundation
/// The type of account used for managing subscriptions.
///
/// Legacy: superseded by `DataContext` and `Account`. Nothing writes this type
/// any more — it survives so `AccountsManager.legacySubscriptionAccount()` can
/// read the selection an upgrading install persisted, and
/// `DataContext.init(legacy:)` can map it onto a context. Do not add members.
enum SubscriptionAccountType: String, Codable, Sendable, Equatable, CaseIterable {
/// Local subscriptions synced via iCloud (requires Yattee Server for feed).
case local
/// Subscriptions managed by an Invidious instance account.
case invidious
/// Subscriptions managed by a Piped instance account.
case piped
/// Display name for the account type.
var displayName: String {
switch self {
case .local:
return String(localized: "subscriptions.account.local")
case .invidious:
return String(localized: "subscriptions.account.invidious")
case .piped:
return String(localized: "subscriptions.account.piped")
}
}
}
/// Represents a configured subscription account.
/// Combines the account type with an optional instance identifier.
/// A configured subscription account as the previous release persisted it.
/// Decode-only; see `SubscriptionAccountType`.
struct SubscriptionAccount: Codable, Sendable, Equatable, Hashable {
/// The type of subscription account.
let type: SubscriptionAccountType
/// The instance UUID for instance-based accounts (Invidious/Piped).
/// Nil for local accounts.
let instanceID: UUID?
/// Local subscription account (iCloud-synced, requires Yattee Server).
static let local = SubscriptionAccount(type: .local, instanceID: nil)
/// Creates an Invidious subscription account for the given instance.
/// - Parameter instanceID: The UUID of the Invidious instance.
/// - Returns: A subscription account configured for Invidious.
static func invidious(_ instanceID: UUID) -> SubscriptionAccount {
SubscriptionAccount(type: .invidious, instanceID: instanceID)
}
/// Creates a Piped subscription account for the given instance.
/// - Parameter instanceID: The UUID of the Piped instance.
/// - Returns: A subscription account configured for Piped.
static func piped(_ instanceID: UUID) -> SubscriptionAccount {
SubscriptionAccount(type: .piped, instanceID: instanceID)
}
/// Whether this account requires an authenticated instance.
var requiresAuthentication: Bool {
switch type {
case .local:
return false
case .invidious, .piped:
return true
}
}
/// Whether this account requires Yattee Server for feed fetching.
var requiresYatteeServer: Bool {
switch type {
case .local:
return true
case .invidious, .piped:
return false
}
}
}