2023-04-19 01:55:14 +00:00
|
|
|
//
|
|
|
|
// ShareMastodonContext.swift
|
|
|
|
// ShareExtension
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 4/17/23.
|
|
|
|
// Copyright © 2023 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Pachyderm
|
|
|
|
import ComposeUI
|
|
|
|
import UserAccounts
|
|
|
|
import InstanceFeatures
|
|
|
|
import Combine
|
|
|
|
|
2024-01-28 19:03:14 +00:00
|
|
|
final class ShareMastodonContext: ComposeMastodonContext, ObservableObject, Sendable {
|
2023-04-19 01:55:14 +00:00
|
|
|
let accountInfo: UserAccountInfo?
|
|
|
|
let client: Client
|
|
|
|
let instanceFeatures: InstanceFeatures
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private var customEmojis: [Emoji]?
|
|
|
|
|
2024-01-28 19:03:14 +00:00
|
|
|
@MainActor
|
|
|
|
@Published
|
|
|
|
private(set) var ownAccount: Account?
|
2023-04-19 01:55:14 +00:00
|
|
|
|
|
|
|
init(accountInfo: UserAccountInfo) {
|
|
|
|
self.accountInfo = accountInfo
|
|
|
|
self.client = Client(baseURL: accountInfo.instanceURL, accessToken: accountInfo.accessToken)
|
|
|
|
self.instanceFeatures = InstanceFeatures()
|
|
|
|
|
|
|
|
Task { @MainActor in
|
2023-12-04 22:55:03 +00:00
|
|
|
async let instance = try? await run(Client.getInstanceV1()).0
|
2024-01-27 19:58:36 +00:00
|
|
|
async let nodeInfo = try? await client.nodeInfo()
|
2023-04-19 01:55:14 +00:00
|
|
|
guard let instance = await instance else { return }
|
2023-12-04 22:55:03 +00:00
|
|
|
self.instanceFeatures.update(instance: InstanceInfo(v1: instance), nodeInfo: await nodeInfo)
|
2023-04-19 01:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Task { @MainActor in
|
|
|
|
if let account = try? await run(Client.getSelfAccount()).0 {
|
|
|
|
self.ownAccount = account
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:16:30 +00:00
|
|
|
// MARK: ComposeMastodonContext
|
2023-04-19 01:55:14 +00:00
|
|
|
|
|
|
|
func run<Result: Decodable & Sendable>(_ request: Request<Result>) async throws -> (Result, Pagination?) {
|
|
|
|
return try await withCheckedThrowingContinuation({ continuation in
|
|
|
|
client.run(request) { response in
|
|
|
|
switch response {
|
|
|
|
case .success(let result, let pagination):
|
|
|
|
continuation.resume(returning: (result, pagination))
|
|
|
|
case .failure(let error):
|
|
|
|
continuation.resume(throwing: error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
2024-01-27 19:58:36 +00:00
|
|
|
func getCustomEmojis() async -> [Emoji] {
|
2023-04-19 01:55:14 +00:00
|
|
|
if let customEmojis {
|
2024-01-27 19:58:36 +00:00
|
|
|
return customEmojis
|
2023-04-19 01:55:14 +00:00
|
|
|
} else {
|
2024-01-27 19:58:36 +00:00
|
|
|
let emojis = (try? await self.run(Client.getCustomEmoji()).0) ?? []
|
|
|
|
self.customEmojis = emojis
|
|
|
|
return emojis
|
2023-04-19 01:55:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchCachedAccounts(query: String) -> [AccountProtocol] {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
func cachedRelationship(for accountID: String) -> RelationshipProtocol? {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchCachedHashtags(query: String) -> [Hashtag] {
|
|
|
|
return []
|
|
|
|
}
|
2023-05-11 13:59:57 +00:00
|
|
|
|
|
|
|
func storeCreatedStatus(_ status: Status) {
|
|
|
|
}
|
2024-08-11 17:14:10 +00:00
|
|
|
|
|
|
|
func fetchStatus(id: String) -> (any StatusProtocol)? {
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-19 01:55:14 +00:00
|
|
|
}
|