qstatus/qStatus/Services/PostingManager.swift
Paweł Orzech c27437b33c
Initial implementation of qStatus macOS menubar app
Native macOS app for posting to Mastodon, WordPress (self-hosted), and Micro.blog.
Features: global hotkey (Ctrl+Option+Cmd+T), multiple accounts with selection,
image attachments (up to 4, drag-and-drop), floating panel UI, Keychain storage.
2026-02-27 23:40:51 +01:00

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 = 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 = 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)
}
}
}