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

View File

@ -1,8 +1,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>
<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="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>

View File

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