- Extract OAuth UserDefaults keys into shared OAuthKeys enum - Extract triplicated mimeTypeFor() into shared mimeTypeForImage() - Cache normalized base URL at client init instead of per-request
65 lines
2.4 KiB
Swift
65 lines
2.4 KiB
Swift
import Foundation
|
|
|
|
@MainActor
|
|
struct PostingManager {
|
|
|
|
static func post(
|
|
text: String,
|
|
images: [ImageAttachment],
|
|
accounts: [Account],
|
|
accountStore: AccountStore
|
|
) async -> [(Account, Result<URL, Error>)] {
|
|
var results: [(Account, Result<URL, Error>)] = []
|
|
|
|
for account in accounts {
|
|
guard let token = accountStore.token(for: account) else {
|
|
results.append((account, .failure(PostingError(message: "No token for \(account.displayName)"))))
|
|
continue
|
|
}
|
|
|
|
do {
|
|
let url = try await postToAccount(account: account, token: token, text: text, images: images)
|
|
results.append((account, .success(url)))
|
|
} catch {
|
|
results.append((account, .failure(error)))
|
|
}
|
|
}
|
|
|
|
return results
|
|
}
|
|
|
|
private static func postToAccount(
|
|
account: Account, token: String, text: String, images: [ImageAttachment]
|
|
) async throws -> URL {
|
|
let imageData = images.map { (data: $0.data, filename: $0.filename) }
|
|
|
|
switch account.serviceType {
|
|
case .mastodon:
|
|
let client = try MastodonClient(account: account, token: token)
|
|
var mediaIDs: [String] = []
|
|
for img in imageData {
|
|
let id = try await client.uploadMedia(imageData: img.data, filename: img.filename, altText: nil)
|
|
mediaIDs.append(id)
|
|
}
|
|
return try await client.createPost(text: text, mediaIDs: mediaIDs)
|
|
|
|
case .wordpress:
|
|
let client = try WordPressClient(account: account, token: token)
|
|
var mediaIDs: [String] = []
|
|
for img in imageData {
|
|
let id = try await client.uploadMedia(imageData: img.data, filename: img.filename, altText: nil)
|
|
mediaIDs.append(id)
|
|
}
|
|
return try await client.createPost(text: text, mediaIDs: mediaIDs)
|
|
|
|
case .microblog:
|
|
let client = MicroblogClient(account: account, token: token)
|
|
var mediaIDs: [String] = []
|
|
for img in imageData {
|
|
let id = try await client.uploadMedia(imageData: img.data, filename: img.filename, altText: nil)
|
|
mediaIDs.append(id)
|
|
}
|
|
return try await client.createPost(text: text, mediaIDs: mediaIDs)
|
|
}
|
|
}
|
|
}
|