2023-05-25 16:26:17 +02:00
|
|
|
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
|
2024-04-30 09:22:27 +02:00
|
|
|
public struct MastodonAuthentication: Codable, Hashable, UserIdentifier {
|
2024-06-21 12:09:49 +02:00
|
|
|
public enum InstanceConfiguration: Codable, Hashable {
|
2024-06-25 10:41:03 +02:00
|
|
|
case v1(Mastodon.Entity.Instance)
|
2024-06-21 12:09:49 +02:00
|
|
|
case v2(Mastodon.Entity.V2.Instance, TranslationLanguages)
|
2024-06-25 10:41:03 +02:00
|
|
|
|
|
|
|
public func canTranslateFrom(_ sourceLocale: String, to targetLanguage: String) -> Bool {
|
|
|
|
switch self {
|
|
|
|
case .v1:
|
|
|
|
return false
|
|
|
|
case let .v2(instance, translationLanguages):
|
|
|
|
guard instance.configuration?.translation?.enabled == true else { return false }
|
|
|
|
return translationLanguages[sourceLocale]?.contains(targetLanguage) == true
|
|
|
|
}
|
|
|
|
}
|
2024-06-25 11:04:38 +02:00
|
|
|
|
|
|
|
public var instanceConfigLimitingProperties: InstanceConfigLimitingPropertyContaining? {
|
|
|
|
switch self {
|
|
|
|
case let .v1(instance):
|
|
|
|
return instance.configuration
|
|
|
|
case let .v2(instance, _):
|
|
|
|
return instance.configuration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public var canFollowTags: Bool {
|
|
|
|
let version: String?
|
|
|
|
switch self {
|
|
|
|
case let .v1(instance):
|
|
|
|
version = instance.version
|
|
|
|
case let .v2(instance, _):
|
|
|
|
version = instance.version
|
|
|
|
}
|
|
|
|
return version?.majorServerVersion(greaterThanOrEquals: 4) ?? false // following Tags is support beginning with Mastodon v4.0.0
|
|
|
|
}
|
2024-06-21 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
2023-05-25 16:26:17 +02:00
|
|
|
public typealias ID = UUID
|
|
|
|
|
|
|
|
public private(set) var identifier: ID
|
|
|
|
public private(set) var domain: String
|
|
|
|
public private(set) var username: String
|
|
|
|
|
|
|
|
public private(set) var appAccessToken: String
|
|
|
|
public private(set) var userAccessToken: String
|
|
|
|
public private(set) var clientID: String
|
|
|
|
public private(set) var clientSecret: String
|
|
|
|
|
|
|
|
public private(set) var createdAt: Date
|
|
|
|
public private(set) var updatedAt: Date
|
|
|
|
public private(set) var activedAt: Date
|
|
|
|
|
|
|
|
public private(set) var userID: String
|
2024-06-25 11:04:38 +02:00
|
|
|
|
2024-06-21 12:09:49 +02:00
|
|
|
public private(set) var instanceConfiguration: InstanceConfiguration?
|
2023-05-25 16:26:17 +02:00
|
|
|
|
2024-04-30 09:22:27 +02:00
|
|
|
public var persistenceIdentifier: String {
|
2023-05-26 11:36:08 +02:00
|
|
|
"\(username)@\(domain)"
|
|
|
|
}
|
|
|
|
|
2023-05-25 16:26:17 +02:00
|
|
|
public static func createFrom(
|
|
|
|
domain: String,
|
|
|
|
userID: String,
|
|
|
|
username: String,
|
|
|
|
appAccessToken: String,
|
|
|
|
userAccessToken: String,
|
|
|
|
clientID: String,
|
|
|
|
clientSecret: String
|
|
|
|
) -> Self {
|
|
|
|
let now = Date()
|
|
|
|
return MastodonAuthentication(
|
2023-05-26 11:36:08 +02:00
|
|
|
identifier: .init(),
|
2023-05-25 16:26:17 +02:00
|
|
|
domain: domain,
|
|
|
|
username: username,
|
|
|
|
appAccessToken: appAccessToken,
|
|
|
|
userAccessToken: userAccessToken,
|
|
|
|
clientID: clientID,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
createdAt: now,
|
|
|
|
updatedAt: now,
|
|
|
|
activedAt: now,
|
|
|
|
userID: userID,
|
2024-06-21 12:09:49 +02:00
|
|
|
instanceConfiguration: nil
|
2023-05-25 16:26:17 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func copy(
|
|
|
|
identifier: ID? = nil,
|
|
|
|
domain: String? = nil,
|
|
|
|
username: String? = nil,
|
|
|
|
appAccessToken: String? = nil,
|
|
|
|
userAccessToken: String? = nil,
|
|
|
|
clientID: String? = nil,
|
|
|
|
clientSecret: String? = nil,
|
|
|
|
createdAt: Date? = nil,
|
|
|
|
updatedAt: Date? = nil,
|
|
|
|
activedAt: Date? = nil,
|
|
|
|
userID: String? = nil,
|
2024-06-21 12:09:49 +02:00
|
|
|
instanceConfiguration: InstanceConfiguration? = nil
|
2023-05-25 16:26:17 +02:00
|
|
|
) -> Self {
|
|
|
|
MastodonAuthentication(
|
|
|
|
identifier: identifier ?? self.identifier,
|
|
|
|
domain: domain ?? self.domain,
|
|
|
|
username: username ?? self.username,
|
|
|
|
appAccessToken: appAccessToken ?? self.appAccessToken,
|
|
|
|
userAccessToken: userAccessToken ?? self.userAccessToken,
|
|
|
|
clientID: clientID ?? self.clientID,
|
|
|
|
clientSecret: clientSecret ?? self.clientSecret,
|
|
|
|
createdAt: createdAt ?? self.createdAt,
|
|
|
|
updatedAt: updatedAt ?? self.updatedAt,
|
|
|
|
activedAt: activedAt ?? self.activedAt,
|
|
|
|
userID: userID ?? self.userID,
|
2024-06-21 12:09:49 +02:00
|
|
|
instanceConfiguration: instanceConfiguration ?? self.instanceConfiguration
|
2023-05-25 16:26:17 +02:00
|
|
|
)
|
|
|
|
}
|
2023-12-07 13:35:33 +01:00
|
|
|
|
2023-12-13 15:07:16 +01:00
|
|
|
public func account() -> Mastodon.Entity.Account? {
|
2023-12-28 22:39:24 +01:00
|
|
|
|
|
|
|
let account = FileManager
|
|
|
|
.default
|
|
|
|
.accounts(for: self.userIdentifier())
|
|
|
|
.first(where: { $0.id == userID })
|
2023-12-13 15:07:16 +01:00
|
|
|
|
|
|
|
return account
|
2023-12-07 13:35:33 +01:00
|
|
|
}
|
|
|
|
|
2023-12-28 22:39:24 +01:00
|
|
|
public func userIdentifier() -> MastodonUserIdentifier {
|
|
|
|
MastodonUserIdentifier(domain: domain, userID: userID)
|
|
|
|
}
|
|
|
|
|
2024-06-25 11:36:04 +02:00
|
|
|
@MainActor
|
2024-06-21 12:09:49 +02:00
|
|
|
func updating(instanceV1 instance: Mastodon.Entity.Instance) -> Self {
|
2024-06-25 10:41:03 +02:00
|
|
|
return copy(instanceConfiguration: .v1(instance))
|
2024-06-21 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
2024-06-25 11:36:04 +02:00
|
|
|
@MainActor
|
2024-06-21 12:09:49 +02:00
|
|
|
func updating(instanceV2 instance: Mastodon.Entity.V2.Instance) -> Self {
|
|
|
|
guard
|
2024-06-26 14:36:17 +02:00
|
|
|
let instanceConfiguration,
|
2024-06-21 12:09:49 +02:00
|
|
|
case let InstanceConfiguration.v2(_, translationLanguages) = instanceConfiguration
|
|
|
|
else {
|
|
|
|
return copy(instanceConfiguration: .v2(instance, [:]))
|
|
|
|
}
|
|
|
|
return copy(instanceConfiguration: .v2(instance, translationLanguages))
|
|
|
|
}
|
|
|
|
|
2024-06-25 11:36:04 +02:00
|
|
|
@MainActor
|
2024-06-21 12:09:49 +02:00
|
|
|
func updating(translationLanguages: TranslationLanguages) -> Self {
|
|
|
|
switch self.instanceConfiguration {
|
2024-06-25 10:41:03 +02:00
|
|
|
case .v1(let instance):
|
|
|
|
return copy(instanceConfiguration: .v1(instance))
|
2024-06-21 12:09:49 +02:00
|
|
|
case .v2(let instance, _):
|
|
|
|
return copy(instanceConfiguration: .v2(instance, translationLanguages))
|
|
|
|
case .none:
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 16:26:17 +02:00
|
|
|
func updating(activatedAt: Date) -> Self {
|
|
|
|
copy(activedAt: activatedAt)
|
|
|
|
}
|
2024-04-30 09:22:27 +02:00
|
|
|
|
|
|
|
var authorization: Mastodon.API.OAuth.Authorization {
|
|
|
|
.init(accessToken: userAccessToken)
|
|
|
|
}
|
2023-05-25 16:26:17 +02:00
|
|
|
}
|