IceCubes/IceCubesAppIntents/InlinePostIntent.swift

62 lines
1.9 KiB
Swift
Raw Permalink 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 InlinePostIntent: AppIntent {
2024-05-05 13:12:19 +02:00
static let title: LocalizedStringResource = "Send post to Mastodon"
static let description: IntentDescription = "Send a text post to Mastodon with Ice Cubes"
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"))
2024-05-05 13:12:19 +02:00
var account: AppAccountEntity
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.")
}
}
}