2022-06-09 12:09:28 +02:00
|
|
|
//
|
|
|
|
// SendPostIntentHandler.swift
|
|
|
|
// MastodonIntent
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-7-26.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Intents
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
2022-09-30 13:28:09 +02:00
|
|
|
import MastodonCore
|
2022-06-09 12:09:28 +02:00
|
|
|
|
|
|
|
final class SendPostIntentHandler: NSObject {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2023-10-11 15:38:31 +02:00
|
|
|
let coreDataStack = CoreDataStack()
|
2022-06-09 12:09:28 +02:00
|
|
|
lazy var managedObjectContext = coreDataStack.persistentContainer.viewContext
|
2022-09-30 13:28:09 +02:00
|
|
|
lazy var api: APIService = {
|
|
|
|
let backgroundManagedObjectContext = coreDataStack.newTaskContext()
|
|
|
|
return APIService(
|
|
|
|
backgroundManagedObjectContext: backgroundManagedObjectContext
|
|
|
|
)
|
|
|
|
}()
|
2022-06-09 12:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - SendPostIntentHandling
|
|
|
|
extension SendPostIntentHandler: SendPostIntentHandling {
|
|
|
|
|
|
|
|
func handle(intent: SendPostIntent) async -> SendPostIntentResponse {
|
|
|
|
guard let content = intent.content else {
|
|
|
|
return SendPostIntentResponse(code: .failure, userActivity: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
let visibility: Mastodon.Entity.Status.Visibility = {
|
|
|
|
switch intent.visibility {
|
|
|
|
case .unknown: return .public
|
|
|
|
case .public: return .public
|
|
|
|
case .followersOnly: return .private
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
do {
|
|
|
|
// fetch authentications from
|
|
|
|
// user pick accounts
|
|
|
|
// or fallback to active account
|
|
|
|
let mastodonAuthentications: [MastodonAuthentication]
|
|
|
|
let accounts = intent.accounts ?? []
|
|
|
|
if accounts.isEmpty {
|
2023-05-25 16:26:17 +02:00
|
|
|
// fixme: refactor this and implemented method on AuthenticationServiceProvider
|
|
|
|
let _authentication = AuthenticationServiceProvider.shared.authentications.sorted(by: { $0.activedAt > $1.activedAt }).first
|
2022-06-09 12:09:28 +02:00
|
|
|
|
|
|
|
guard let authentication = _authentication else {
|
|
|
|
let failureReason = APIService.APIError.implicit(.authenticationMissing).errorDescription ?? "Fail to Send Post"
|
|
|
|
return SendPostIntentResponse.failure(failureReason: failureReason)
|
|
|
|
}
|
|
|
|
mastodonAuthentications = [authentication]
|
|
|
|
} else {
|
|
|
|
mastodonAuthentications = try accounts.mastodonAuthentication(in: managedObjectContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
let authenticationBoxes = mastodonAuthentications.map { authentication in
|
|
|
|
MastodonAuthenticationBox(
|
2023-05-25 16:26:17 +02:00
|
|
|
authentication: authentication,
|
2022-06-09 12:09:28 +02:00
|
|
|
domain: authentication.domain,
|
|
|
|
userID: authentication.userID,
|
|
|
|
appAuthorization: .init(accessToken: authentication.appAccessToken),
|
2023-05-09 11:15:24 +02:00
|
|
|
userAuthorization: .init(accessToken: authentication.userAccessToken),
|
2023-05-25 16:26:17 +02:00
|
|
|
inMemoryCache: .sharedCache(for: authentication.userAccessToken)
|
2022-06-09 12:09:28 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var posts: [Post] = []
|
|
|
|
for authenticationBox in authenticationBoxes {
|
|
|
|
let idempotencyKey = UUID().uuidString
|
|
|
|
let response = try await api.publishStatus(
|
|
|
|
domain: authenticationBox.domain,
|
|
|
|
idempotencyKey: idempotencyKey,
|
|
|
|
query: .init(
|
|
|
|
status: content,
|
|
|
|
mediaIDs: nil,
|
|
|
|
pollOptions: nil,
|
|
|
|
pollExpiresIn: nil,
|
|
|
|
inReplyToID: nil,
|
|
|
|
sensitive: nil,
|
|
|
|
spoilerText: nil,
|
2023-01-24 01:50:10 +01:00
|
|
|
visibility: visibility,
|
|
|
|
language: nil
|
2022-06-09 12:09:28 +02:00
|
|
|
),
|
|
|
|
authenticationBox: authenticationBox
|
|
|
|
)
|
|
|
|
let post = Post(
|
|
|
|
identifier: response.value.id,
|
|
|
|
display: response.value.account.acct,
|
|
|
|
subtitle: content,
|
|
|
|
image: response.value.account.avatarImageURL().flatMap { INImage(url: $0) }
|
|
|
|
)
|
2023-02-07 19:47:30 +01:00
|
|
|
if let urlString = response.value.url, let url = URL(string: urlString) {
|
|
|
|
post.url = url
|
2023-02-07 19:37:36 +01:00
|
|
|
}
|
2022-06-09 12:09:28 +02:00
|
|
|
posts.append(post)
|
|
|
|
} // end for in
|
|
|
|
|
|
|
|
let intentResponse = SendPostIntentResponse(code: .success, userActivity: nil)
|
|
|
|
intentResponse.posts = posts
|
|
|
|
|
|
|
|
return intentResponse
|
|
|
|
} catch {
|
|
|
|
let intentResponse = SendPostIntentResponse(code: .failure, userActivity: nil)
|
|
|
|
if let error = error as? LocalizedError {
|
|
|
|
intentResponse.failureReason = [
|
|
|
|
error.errorDescription,
|
|
|
|
error.failureReason,
|
|
|
|
error.recoverySuggestion
|
|
|
|
]
|
|
|
|
.compactMap { $0 }
|
|
|
|
.joined(separator: ", ")
|
|
|
|
} else {
|
|
|
|
intentResponse.failureReason = error.localizedDescription
|
|
|
|
}
|
|
|
|
return intentResponse
|
|
|
|
}
|
|
|
|
} // end func
|
|
|
|
|
|
|
|
// content
|
|
|
|
func resolveContent(for intent: SendPostIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
|
|
|
|
guard let content = intent.content, !content.isEmpty else {
|
|
|
|
completion(.needsValue())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
completion(.success(with: content))
|
|
|
|
}
|
|
|
|
|
|
|
|
// accounts
|
|
|
|
func resolveAccounts(for intent: SendPostIntent) async -> [AccountResolutionResult] {
|
|
|
|
guard let accounts = intent.accounts, !accounts.isEmpty else {
|
|
|
|
return [AccountResolutionResult.needsValue()]
|
|
|
|
}
|
|
|
|
|
|
|
|
let results = accounts.map { account in
|
|
|
|
AccountResolutionResult.success(with: account)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func provideAccountsOptionsCollection(for intent: SendPostIntent) async throws -> INObjectCollection<Account> {
|
|
|
|
let accounts = try await Account.fetch(in: managedObjectContext)
|
|
|
|
return .init(items: accounts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// visibility
|
|
|
|
func resolveVisibility(for intent: SendPostIntent, with completion: @escaping (PostVisibilityResolutionResult) -> Void) {
|
|
|
|
completion(.success(with: intent.visibility))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|