2023-04-16 17:23:13 +00:00
|
|
|
//
|
|
|
|
// PostService.swift
|
2023-04-18 00:03:31 +00:00
|
|
|
// ComposeUI
|
2023-04-16 17:23:13 +00:00
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 4/27/22.
|
|
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Pachyderm
|
|
|
|
import UniformTypeIdentifiers
|
|
|
|
|
2023-04-23 02:03:52 +00:00
|
|
|
@MainActor
|
2023-04-16 17:23:13 +00:00
|
|
|
class PostService: ObservableObject {
|
|
|
|
private let mastodonController: ComposeMastodonContext
|
|
|
|
private let config: ComposeUIConfig
|
|
|
|
private let draft: Draft
|
|
|
|
|
|
|
|
@Published var currentStep = 1
|
2023-05-11 13:59:57 +00:00
|
|
|
@Published private(set) var totalSteps = 2
|
2023-04-16 17:23:13 +00:00
|
|
|
|
|
|
|
init(mastodonController: ComposeMastodonContext, config: ComposeUIConfig, draft: Draft) {
|
|
|
|
self.mastodonController = mastodonController
|
|
|
|
self.config = config
|
|
|
|
self.draft = draft
|
|
|
|
}
|
|
|
|
|
|
|
|
func post() async throws {
|
|
|
|
guard draft.hasContent else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// save before posting, so if a crash occurs during network request, the status won't be lost
|
2023-04-23 01:16:30 +00:00
|
|
|
DraftsPersistentContainer.shared.save()
|
2023-04-16 17:23:13 +00:00
|
|
|
|
|
|
|
let uploadedAttachments = try await uploadAttachments()
|
|
|
|
|
|
|
|
let contentWarning = draft.contentWarningEnabled ? draft.contentWarning : nil
|
|
|
|
let sensitive = contentWarning != nil
|
|
|
|
|
2023-05-11 13:59:57 +00:00
|
|
|
let request: Request<Status>
|
|
|
|
|
|
|
|
if let editedStatusID = draft.editedStatusID {
|
|
|
|
if mastodonController.instanceFeatures.needsEditAttachmentsInSeparateRequest {
|
|
|
|
await updateEditedAttachments()
|
|
|
|
}
|
|
|
|
|
|
|
|
request = Client.editStatus(
|
|
|
|
id: editedStatusID,
|
|
|
|
text: textForPosting(),
|
|
|
|
contentType: config.contentType,
|
|
|
|
spoilerText: contentWarning,
|
|
|
|
sensitive: sensitive,
|
|
|
|
language: mastodonController.instanceFeatures.createStatusWithLanguage ? draft.language : nil,
|
|
|
|
mediaIDs: uploadedAttachments,
|
|
|
|
mediaAttributes: draft.draftAttachments.compactMap {
|
|
|
|
if let id = $0.editedAttachmentID {
|
|
|
|
return EditStatusMediaAttributes(id: id, description: $0.attachmentDescription, focus: nil)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
},
|
|
|
|
poll: draft.poll.map {
|
|
|
|
EditPollParameters(options: $0.pollOptions.map(\.text), expiresIn: Int($0.duration), multiple: $0.multiple)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
request = Client.createStatus(
|
|
|
|
text: textForPosting(),
|
|
|
|
contentType: config.contentType,
|
|
|
|
inReplyTo: draft.inReplyToID,
|
|
|
|
mediaIDs: uploadedAttachments,
|
|
|
|
sensitive: sensitive,
|
|
|
|
spoilerText: contentWarning,
|
|
|
|
visibility: draft.visibility,
|
|
|
|
language: mastodonController.instanceFeatures.createStatusWithLanguage ? draft.language : nil,
|
|
|
|
pollOptions: draft.poll?.pollOptions.map(\.text),
|
|
|
|
pollExpiresIn: draft.poll == nil ? nil : Int(draft.poll!.duration),
|
|
|
|
pollMultiple: draft.poll?.multiple,
|
|
|
|
localOnly: mastodonController.instanceFeatures.localOnlyPosts ? draft.localOnly : nil
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-16 17:23:13 +00:00
|
|
|
do {
|
2023-05-11 13:59:57 +00:00
|
|
|
let (status, _) = try await mastodonController.run(request)
|
2023-04-16 17:23:13 +00:00
|
|
|
currentStep += 1
|
2023-05-11 13:59:57 +00:00
|
|
|
mastodonController.storeCreatedStatus(status)
|
2023-04-16 17:23:13 +00:00
|
|
|
} catch let error as Client.Error {
|
|
|
|
throw Error.posting(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 13:59:57 +00:00
|
|
|
private func uploadAttachments() async throws -> [String] {
|
|
|
|
// 2 steps (request data, then upload) for each attachment
|
|
|
|
self.totalSteps += 2 * draft.attachments.count
|
|
|
|
|
|
|
|
var attachments: [String] = []
|
2023-04-16 17:23:13 +00:00
|
|
|
attachments.reserveCapacity(draft.attachments.count)
|
2023-04-23 01:16:30 +00:00
|
|
|
for (index, attachment) in draft.draftAttachments.enumerated() {
|
2023-05-11 13:59:57 +00:00
|
|
|
// if this attachment already exists and is being edited, we don't do anything
|
|
|
|
// edits to the description are handled as part of the edit status request
|
|
|
|
if let editedAttachmentID = attachment.editedAttachmentID {
|
|
|
|
attachments.append(editedAttachmentID)
|
|
|
|
currentStep += 2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-16 17:23:13 +00:00
|
|
|
let data: Data
|
|
|
|
let utType: UTType
|
|
|
|
do {
|
|
|
|
(data, utType) = try await getData(for: attachment)
|
|
|
|
currentStep += 1
|
|
|
|
} catch let error as AttachmentData.Error {
|
|
|
|
throw Error.attachmentData(index: index, cause: error)
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
let uploaded = try await uploadAttachment(data: data, utType: utType, description: attachment.attachmentDescription)
|
2023-05-11 13:59:57 +00:00
|
|
|
attachments.append(uploaded.id)
|
2023-04-16 17:23:13 +00:00
|
|
|
currentStep += 1
|
|
|
|
} catch let error as Client.Error {
|
|
|
|
throw Error.attachmentUpload(index: index, cause: error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attachments
|
|
|
|
}
|
|
|
|
|
|
|
|
private func getData(for attachment: DraftAttachment) async throws -> (Data, UTType) {
|
|
|
|
return try await withCheckedThrowingContinuation { continuation in
|
2023-04-23 01:16:30 +00:00
|
|
|
attachment.getData(features: mastodonController.instanceFeatures) { result in
|
2023-04-16 17:23:13 +00:00
|
|
|
switch result {
|
|
|
|
case let .success(res):
|
|
|
|
continuation.resume(returning: res)
|
|
|
|
case let .failure(error):
|
|
|
|
continuation.resume(throwing: error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func uploadAttachment(data: Data, utType: UTType, description: String?) async throws -> Attachment {
|
|
|
|
let formAttachment = FormAttachment(mimeType: utType.preferredMIMEType!, data: data, fileName: "file.\(utType.preferredFilenameExtension!)")
|
|
|
|
let req = Client.upload(attachment: formAttachment, description: description)
|
|
|
|
return try await mastodonController.run(req).0
|
|
|
|
}
|
|
|
|
|
|
|
|
private func textForPosting() -> String {
|
|
|
|
var text = draft.text
|
|
|
|
// when using dictation, iOS sometimes leaves a U+FFFC OBJECT REPLACEMENT CHARACTER behind in the text,
|
|
|
|
// which we want to strip out before actually posting the status
|
|
|
|
text = text.replacingOccurrences(of: "\u{fffc}", with: "")
|
|
|
|
|
|
|
|
if draft.localOnly && mastodonController.instanceFeatures.needsLocalOnlyEmojiHack {
|
|
|
|
text += " 👁"
|
|
|
|
}
|
|
|
|
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
2023-05-11 13:59:57 +00:00
|
|
|
// only needed for akkoma, not used on regular mastodon
|
|
|
|
private func updateEditedAttachments() async {
|
|
|
|
for attachment in draft.draftAttachments {
|
|
|
|
guard let id = attachment.editedAttachmentID else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let req = Client.updateAttachment(id: id, description: attachment.attachmentDescription, focus: nil)
|
|
|
|
_ = try? await mastodonController.run(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-16 17:23:13 +00:00
|
|
|
enum Error: Swift.Error, LocalizedError {
|
|
|
|
case attachmentData(index: Int, cause: AttachmentData.Error)
|
|
|
|
case attachmentUpload(index: Int, cause: Client.Error)
|
|
|
|
case posting(Client.Error)
|
|
|
|
|
|
|
|
var localizedDescription: String {
|
|
|
|
switch self {
|
|
|
|
case let .attachmentData(index: index, cause: cause):
|
|
|
|
return "Attachment \(index + 1): \(cause.localizedDescription)"
|
|
|
|
case let .attachmentUpload(index: index, cause: cause):
|
|
|
|
return "Attachment \(index + 1): \(cause.localizedDescription)"
|
|
|
|
case let .posting(error):
|
|
|
|
return error.localizedDescription
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|