Don't hardcode the account id

This commit is contained in:
Shadowfacts 2024-09-03 10:01:18 -04:00
parent ed28b42183
commit a812afee4e
4 changed files with 60 additions and 12 deletions

View File

@ -589,6 +589,7 @@
"$(inherited)", "$(inherited)",
"@executable_path/../Frameworks", "@executable_path/../Frameworks",
); );
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.MastoSearch; PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.MastoSearch;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
@ -616,6 +617,7 @@
"$(inherited)", "$(inherited)",
"@executable_path/../Frameworks", "@executable_path/../Frameworks",
); );
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.MastoSearch; PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.MastoSearch;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="22113.3" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS"> <document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="23091" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies> <dependencies>
<deployment identifier="macosx"/> <deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22113.3"/> <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="23091"/>
<capability name="Search Toolbar Item" minToolsVersion="12.0" minSystemVersion="11.0"/> <capability name="Search Toolbar Item" minToolsVersion="12.0" minSystemVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies> </dependencies>

View File

@ -6,8 +6,8 @@ import PackageDescription
let package = Package( let package = Package(
name: "MastoSearchCore", name: "MastoSearchCore",
platforms: [ platforms: [
.macOS(.v12), .macOS(.v13),
.iOS(.v15), .iOS(.v16),
], ],
products: [ products: [
// Products define the executables and libraries a package produces, and make them visible to other packages. // Products define the executables and libraries a package produces, and make them visible to other packages.

View File

@ -6,8 +6,9 @@
// //
import Foundation import Foundation
import os
public struct APIController { public final class APIController {
public static let shared = APIController() public static let shared = APIController()
@ -38,6 +39,8 @@ public struct APIController {
return decoder return decoder
}() }()
private var verifyCredsResponse: OSAllocatedUnfairLock<VerifyCredentialsResponse?> = .init(initialState: nil)
private init() {} private init() {}
@ -61,7 +64,7 @@ public struct APIController {
return return
} }
do { do {
let statuses = try decoder.decode(R.self, from: data) let statuses = try self.decoder.decode(R.self, from: data)
completion(.success(statuses)) completion(.success(statuses))
} catch { } catch {
completion(.failure(.decoding(error))) completion(.failure(.decoding(error)))
@ -108,16 +111,52 @@ public struct APIController {
} }
public func getStatuses(range: RequestRange, completion: @escaping (Result<[Status], Error>) -> Void) { public func getStatuses(range: RequestRange, completion: @escaping (Result<[Status], Error>) -> Void) {
verifyCredentials {
switch $0 {
case .failure(let error):
completion(.failure(error))
case .success(let response):
guard let account = LocalData.account else {
completion(.failure(.noAccount))
return
}
var components = URLComponents(url: account.instanceURL, resolvingAgainstBaseURL: false)!
components.path = "/api/v1/accounts/\(response.id)/statuses"
components.queryItems = range.queryParameters + [
URLQueryItem(name: "exclude_replies", value: "false"),
URLQueryItem(name: "limit", value: "50"),
]
self.run(request: URLRequest(url: components.url!), completion: completion)
}
}
}
private func verifyCredentials(completion: @escaping (Result<VerifyCredentialsResponse, Error>) -> Void) {
let cached = verifyCredsResponse.withLock {
if let cached = $0 {
completion(.success(cached))
return true
} else {
return false
}
}
guard !cached else {
return
}
guard let account = LocalData.account else { guard let account = LocalData.account else {
completion(.failure(.noAccount))
return return
} }
var components = URLComponents(url: account.instanceURL, resolvingAgainstBaseURL: false)! var components = URLComponents(url: account.instanceURL, resolvingAgainstBaseURL: false)!
components.path = "/api/v1/accounts/1/statuses" components.path = "/api/v1/accounts/verify_credentials"
components.queryItems = range.queryParameters + [ run(request: URLRequest(url: components.url!)) { (result: Result<VerifyCredentialsResponse, Error>) in
URLQueryItem(name: "exclude_replies", value: "false"), if case .success(let resp) = result {
URLQueryItem(name: "limit", value: "50"), self.verifyCredsResponse.withLock {
] $0 = resp
run(request: URLRequest(url: components.url!), completion: completion) }
}
completion(result)
}
} }
} }
@ -176,6 +215,10 @@ extension APIController {
case id, url, spoiler_text, content, created_at, reblog case id, url, spoiler_text, content, created_at, reblog
} }
} }
private struct VerifyCredentialsResponse: Decodable {
let id: String
}
} }
extension APIController { extension APIController {
@ -184,6 +227,7 @@ extension APIController {
case error(Swift.Error) case error(Swift.Error)
case noData case noData
case decoding(Swift.Error) case decoding(Swift.Error)
case noAccount
public var localizedDescription: String { public var localizedDescription: String {
switch self { switch self {
@ -195,6 +239,8 @@ extension APIController {
return "No data" return "No data"
case .decoding(let error): case .decoding(let error):
return "Decoding: \(error.localizedDescription)" return "Decoding: \(error.localizedDescription)"
case .noAccount:
return "No account"
} }
} }
} }