summaryrefslogtreecommitdiff
path: root/Sora/Data
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-06-16 09:37:27 -0700
committerFuwn <[email protected]>2025-06-16 09:37:27 -0700
commitd292e582910c990cae8481c966366b34dc7041b3 (patch)
treefe11aa49917d793e363094644d6c1662886ece88 /Sora/Data
parentfeat: Development commit (diff)
downloadsora-testing-d292e582910c990cae8481c966366b34dc7041b3.tar.xz
sora-testing-d292e582910c990cae8481c966366b34dc7041b3.zip
feat: Development commit
Diffstat (limited to 'Sora/Data')
-rw-r--r--Sora/Data/Booru/BooruManager.swift17
-rw-r--r--Sora/Data/Booru/Provider/BooruProviderCredentials.swift40
-rw-r--r--Sora/Data/Settings/SettingsManager.swift42
3 files changed, 96 insertions, 3 deletions
diff --git a/Sora/Data/Booru/BooruManager.swift b/Sora/Data/Booru/BooruManager.swift
index 12d96f0..ce61644 100644
--- a/Sora/Data/Booru/BooruManager.swift
+++ b/Sora/Data/Booru/BooruManager.swift
@@ -28,6 +28,7 @@ class BooruManager: ObservableObject {
.appendingPathComponent("\(BooruProvider.safebooru.asFileNameComponent)_tags.json")
private let pageCache = NSCache<NSString, BooruPageCacheEntry>() // swiftlint:disable:this legacy_objc_type
private let cacheDuration: TimeInterval = 300
+ private let credentials: BooruProviderCredentials?
// MARK: - Computed Properties
var tags: [String] {
@@ -42,10 +43,11 @@ class BooruManager: ObservableObject {
var canGoForwardInHistory: Bool { historyIndex < searchHistory.count - 1 }
// MARK: - Initialisation
- init(_ provider: BooruProvider) {
+ init(_ provider: BooruProvider, credentials: BooruProviderCredentials? = nil) {
self.provider = provider
self.flavor = BooruProviderFlavor(provider: provider)
self.domain = provider.domain
+ self.credentials = credentials
pageCache.countLimit = 50
pageCache.totalCostLimit = 50 * 1_024 * 1_024
@@ -226,9 +228,18 @@ class BooruManager: ObservableObject {
return URL(string: "https://\(domain)/post.xml?page=\(page)&limit=\(limit)&tags=\(tagString)")
case .gelbooru:
+ var urlString =
+ "https://\(domain)/index.php?page=dapi&s=post&q=index&pid=\(page)&limit=\(limit)&tags=\(tagString)"
+
+ if let validCredentials = credentials,
+ !validCredentials.apiKey.isEmpty,
+ validCredentials.userID != 0
+ {
+ urlString += "&api_key=\(validCredentials.apiKey)&user_id=\(validCredentials.userID)"
+ }
+
return URL(
- string:
- "https://\(domain)/index.php?page=dapi&s=post&q=index&pid=\(page)&limit=\(limit)&tags=\(tagString)"
+ string: urlString
)
}
}
diff --git a/Sora/Data/Booru/Provider/BooruProviderCredentials.swift b/Sora/Data/Booru/Provider/BooruProviderCredentials.swift
new file mode 100644
index 0000000..898201a
--- /dev/null
+++ b/Sora/Data/Booru/Provider/BooruProviderCredentials.swift
@@ -0,0 +1,40 @@
+import Foundation
+
+struct BooruProviderCredentials: Codable, Identifiable, Equatable {
+ let id: UUID
+ let provider: BooruProvider
+ var apiKey: String
+ var userID: Int
+
+ init(provider: BooruProvider, apiKey: String, userID: Int, id: UUID = UUID()) {
+ self.id = id
+ self.provider = provider
+ self.apiKey = apiKey
+ self.userID = userID
+ }
+
+ static func from(
+ // swiftlint:disable:next large_tuple
+ _ rawCredentials: [(provider: BooruProvider, apiKey: String, userID: Int)],
+ existingCredentials: [Self]
+ ) -> [Self] {
+ rawCredentials.map { credentials in
+ if let existingKey = existingCredentials.first(
+ where: { $0.provider == credentials.provider }
+ ) {
+ return Self(
+ provider: credentials.provider,
+ apiKey: credentials.apiKey,
+ userID: credentials.userID,
+ id: existingKey.id
+ )
+ }
+
+ return Self(
+ provider: credentials.provider,
+ apiKey: credentials.apiKey,
+ userID: credentials.userID
+ )
+ }
+ }
+}
diff --git a/Sora/Data/Settings/SettingsManager.swift b/Sora/Data/Settings/SettingsManager.swift
index 98ea00c..c824cff 100644
--- a/Sora/Data/Settings/SettingsManager.swift
+++ b/Sora/Data/Settings/SettingsManager.swift
@@ -60,6 +60,9 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l
@AppStorage("folders")
private var foldersData = Data()
+ @AppStorage("providerCredentials")
+ private var providerCredentialsData = Data()
+
// MARK: - Computed Properties
var bookmarks: [SettingsBookmark] {
get {
@@ -167,6 +170,45 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l
}
}
+ // MARK: Provider Credentials
+ var providerCredentials: [BooruProviderCredentials] {
+ get {
+ syncableData(
+ key: "providerAPIKeys",
+ localData: providerCredentialsData,
+ sort: { $0 },
+ identifier: { $0.id }
+ )
+ }
+
+ set {
+ let existingCredentials: [BooruProviderCredentials] =
+ Self.decode([BooruProviderCredentials].self, from: providerCredentialsData) ?? []
+ let rawCredentials = newValue.map { credentials in
+ (provider: credentials.provider, apiKey: credentials.apiKey, userID: credentials.userID)
+ }
+ let mergedCredentials = BooruProviderCredentials.from(
+ rawCredentials, existingCredentials: existingCredentials
+ )
+
+ syncableData(
+ key: "providerAPIKeys",
+ localData: $providerCredentialsData,
+ newValue: mergedCredentials,
+ sort: { $0 },
+ identifier: { $0.id }
+ )
+ }
+ }
+
+ var providerAPIKeys: [BooruProvider: String] {
+ Dictionary(uniqueKeysWithValues: providerCredentials.map { ($0.provider, $0.apiKey) })
+ }
+
+ var providerUserIDs: [BooruProvider: Int] {
+ Dictionary(uniqueKeysWithValues: providerCredentials.map { ($0.provider, $0.userID) })
+ }
+
// MARK: - Initialisation
init() {
syncObservation = NotificationCenter.default.addObserver(