IceCubes/IceCubesAppIntents/InlinePostIntent.swift

97 lines
2.9 KiB
Swift
Raw Normal View History

2024-05-04 13:12:43 +02:00
import AppAccount
2024-05-04 13:19:19 +02:00
import AppIntents
2024-05-04 13:12:43 +02:00
import Env
2024-05-04 13:19:19 +02:00
import Foundation
2024-05-04 13:12:43 +02:00
import Models
2024-05-04 13:19:19 +02:00
import Network
2024-05-04 13:12:43 +02:00
enum PostVisibility: String, AppEnum {
case direct, priv, unlisted, pub
2024-05-04 13:19:19 +02:00
public static var caseDisplayRepresentations: [PostVisibility: DisplayRepresentation] {
2024-05-04 13:12:43 +02:00
[.direct: "Private",
2024-05-04 13:19:19 +02:00
.priv: "Followers Only",
.unlisted: "Quiet Public",
.pub: "Public"]
2024-05-04 13:12:43 +02:00
}
2024-05-04 13:19:19 +02:00
static var typeDisplayName: LocalizedStringResource { "Visibility" }
2024-05-04 13:12:43 +02:00
public static let typeDisplayRepresentation: TypeDisplayRepresentation = "Visibility"
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
var toAppVisibility: Models.Visibility {
switch self {
case .direct:
2024-05-04 13:19:19 +02:00
.direct
2024-05-04 13:12:43 +02:00
case .priv:
2024-05-04 13:19:19 +02:00
.priv
2024-05-04 13:12:43 +02:00
case .unlisted:
2024-05-04 13:19:19 +02:00
.unlisted
2024-05-04 13:12:43 +02:00
case .pub:
2024-05-04 13:19:19 +02:00
.pub
2024-05-04 13:12:43 +02:00
}
}
}
struct AppAccountWrapper: Identifiable, AppEntity {
var id: String { account.id }
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
let account: AppAccount
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
static var defaultQuery = DefaultAppAccountQuery()
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
static var typeDisplayRepresentation: TypeDisplayRepresentation = "AppAccount"
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(account.accountName ?? account.server)")
}
}
struct DefaultAppAccountQuery: EntityQuery {
func entities(for identifiers: [AppAccountWrapper.ID]) async throws -> [AppAccountWrapper] {
return await AppAccountsManager.shared.availableAccounts.filter { account in
identifiers.contains { id in
id == account.id
}
2024-05-04 13:19:19 +02:00
}.map { AppAccountWrapper(account: $0) }
2024-05-04 13:12:43 +02:00
}
func suggestedEntities() async throws -> [AppAccountWrapper] {
2024-05-04 13:19:19 +02:00
await AppAccountsManager.shared.availableAccounts.map { .init(account: $0) }
2024-05-04 13:12:43 +02:00
}
func defaultResult() async -> AppAccountWrapper? {
await .init(account: AppAccountsManager.shared.currentAccount)
}
}
struct InlinePostIntent: AppIntent {
static let title: LocalizedStringResource = "Send text status to Mastodon"
static var description: IntentDescription {
2024-05-04 13:19:19 +02:00
"Send a text status to Mastodon using Ice Cubes"
2024-05-04 13:12:43 +02:00
}
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
static let openAppWhenRun: Bool = false
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
@Parameter(title: "Account", requestValueDialog: IntentDialog("Account"))
var account: AppAccountWrapper
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
@Parameter(title: "Post visibility", requestValueDialog: IntentDialog("Visibility of your post"))
var visibility: PostVisibility
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
@Parameter(title: "Post content", requestValueDialog: IntentDialog("Content of the post to be sent to Mastodon"))
var content: String
2024-05-04 13:19:19 +02:00
2024-05-04 13:12:43 +02:00
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog & ShowsSnippetView {
let client = Client(server: account.account.server, version: .v1, oauthToken: account.account.oauthToken)
let status = StatusData(status: content, visibility: visibility.toAppVisibility)
do {
let status: Status = try await client.post(endpoint: Statuses.postStatus(json: status))
return .result(dialog: "\(status.content.asRawText) was posted on Mastodon")
} catch {
return .result(dialog: "An error occured while posting to Mastodon, please try again.")
}
}
}