2018-08-16 11:46:19 +00:00
|
|
|
//
|
|
|
|
// MastodonController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 8/15/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2018-09-11 14:52:21 +00:00
|
|
|
import Pachyderm
|
2018-08-16 11:46:19 +00:00
|
|
|
|
2020-09-21 22:04:08 +00:00
|
|
|
class MastodonController: ObservableObject {
|
2020-01-08 02:29:15 +00:00
|
|
|
|
|
|
|
static private(set) var all = [LocalData.UserAccountInfo: MastodonController]()
|
|
|
|
|
|
|
|
@available(*, message: "do something less dumb")
|
|
|
|
static var first: MastodonController { all.first!.value }
|
2018-08-16 11:46:19 +00:00
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
static func getForAccount(_ account: LocalData.UserAccountInfo) -> MastodonController {
|
|
|
|
if let controller = all[account] {
|
|
|
|
return controller
|
|
|
|
} else {
|
|
|
|
let controller = MastodonController(instanceURL: account.instanceURL)
|
|
|
|
controller.accountInfo = account
|
|
|
|
controller.client.clientID = account.clientID
|
|
|
|
controller.client.clientSecret = account.clientSecret
|
|
|
|
controller.client.accessToken = account.accessToken
|
|
|
|
all[account] = controller
|
|
|
|
return controller
|
|
|
|
}
|
|
|
|
}
|
2018-08-16 22:55:40 +00:00
|
|
|
|
2020-05-13 22:58:11 +00:00
|
|
|
static func resetAll() {
|
|
|
|
all = [:]
|
|
|
|
}
|
|
|
|
|
2020-05-11 21:57:50 +00:00
|
|
|
private let transient: Bool
|
|
|
|
private(set) lazy var persistentContainer = MastodonCachePersistentStore(for: accountInfo, transient: transient)
|
2020-04-12 15:14:10 +00:00
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
let instanceURL: URL
|
2020-04-12 15:14:10 +00:00
|
|
|
var accountInfo: LocalData.UserAccountInfo?
|
2020-01-08 02:29:15 +00:00
|
|
|
|
|
|
|
let client: Client!
|
2018-08-16 11:46:19 +00:00
|
|
|
|
2020-09-21 22:04:08 +00:00
|
|
|
@Published private(set) var account: Account!
|
|
|
|
@Published private(set) var instance: Instance!
|
2020-10-12 02:14:45 +00:00
|
|
|
private(set) var customEmojis: [Emoji]?
|
2020-09-13 19:51:06 +00:00
|
|
|
|
2021-04-04 19:11:29 +00:00
|
|
|
private var pendingOwnInstanceRequestCallbacks = [(Instance) -> Void]()
|
|
|
|
private var ownInstanceRequest: URLSessionTask?
|
|
|
|
|
2020-09-13 19:51:06 +00:00
|
|
|
var loggedIn: Bool {
|
|
|
|
accountInfo != nil
|
|
|
|
}
|
2020-01-08 02:29:15 +00:00
|
|
|
|
2020-05-11 21:57:50 +00:00
|
|
|
init(instanceURL: URL, transient: Bool = false) {
|
2020-01-08 02:29:15 +00:00
|
|
|
self.instanceURL = instanceURL
|
|
|
|
self.accountInfo = nil
|
|
|
|
self.client = Client(baseURL: instanceURL)
|
2020-05-11 21:57:50 +00:00
|
|
|
self.transient = transient
|
2018-08-16 22:55:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 02:14:45 +00:00
|
|
|
@discardableResult
|
|
|
|
func run<Result>(_ request: Request<Result>, completion: @escaping Client.Callback<Result>) -> URLSessionTask? {
|
|
|
|
return client.run(request, completion: completion)
|
2020-01-05 19:00:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
func registerApp(completion: @escaping (_ clientID: String, _ clientSecret: String) -> Void) {
|
|
|
|
guard client.clientID == nil,
|
|
|
|
client.clientSecret == nil else {
|
|
|
|
|
|
|
|
completion(client.clientID!, client.clientSecret!)
|
2018-08-19 20:14:04 +00:00
|
|
|
return
|
|
|
|
}
|
2020-01-08 02:29:15 +00:00
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
client.registerApp(name: "Tusker", redirectURI: "tusker://oauth", scopes: [.read, .write, .follow]) { response in
|
|
|
|
guard case let .success(app, _) = response else { fatalError() }
|
2020-01-08 02:29:15 +00:00
|
|
|
self.client.clientID = app.clientID
|
|
|
|
self.client.clientSecret = app.clientSecret
|
|
|
|
completion(app.clientID, app.clientSecret)
|
2018-08-16 22:55:40 +00:00
|
|
|
}
|
2018-08-17 00:11:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
func authorize(authorizationCode: String, completion: @escaping (_ accessToken: String) -> Void) {
|
2018-09-11 14:52:21 +00:00
|
|
|
client.getAccessToken(authorizationCode: authorizationCode, redirectURI: "tusker://oauth") { response in
|
|
|
|
guard case let .success(settings, _) = response else { fatalError() }
|
2020-01-08 02:29:15 +00:00
|
|
|
self.client.accessToken = settings.accessToken
|
|
|
|
completion(settings.accessToken)
|
2018-08-16 11:46:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 21:52:00 +00:00
|
|
|
func getOwnAccount(completion: ((Result<Account, Client.Error>) -> Void)? = nil) {
|
2018-10-02 23:23:50 +00:00
|
|
|
if account != nil {
|
2020-09-16 21:52:00 +00:00
|
|
|
completion?(.success(account))
|
2018-10-02 23:23:50 +00:00
|
|
|
} else {
|
2020-01-05 19:00:39 +00:00
|
|
|
let request = Client.getSelfAccount()
|
|
|
|
run(request) { response in
|
2020-09-16 21:52:00 +00:00
|
|
|
switch response {
|
|
|
|
case let .failure(error):
|
|
|
|
completion?(.failure(error))
|
|
|
|
|
|
|
|
case let .success(account, _):
|
2020-09-21 22:04:08 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.account = account
|
|
|
|
}
|
2020-09-16 21:52:00 +00:00
|
|
|
self.persistentContainer.backgroundContext.perform {
|
|
|
|
if let accountMO = self.persistentContainer.account(for: account.id, in: self.persistentContainer.backgroundContext) {
|
|
|
|
accountMO.updateFrom(apiAccount: account, container: self.persistentContainer)
|
|
|
|
} else {
|
|
|
|
// the first time the user's account is added to the store,
|
|
|
|
// increment its reference count so that it's never removed
|
|
|
|
self.persistentContainer.addOrUpdate(account: account, incrementReferenceCount: true)
|
|
|
|
}
|
|
|
|
completion?(.success(account))
|
2020-05-12 01:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-02 23:23:50 +00:00
|
|
|
}
|
2018-08-31 02:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 23:07:30 +00:00
|
|
|
func getOwnInstance(completion: ((Instance) -> Void)? = nil) {
|
2021-04-04 19:11:29 +00:00
|
|
|
getOwnInstanceInternal(retryAttempt: 0, completion: completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func getOwnInstanceInternal(retryAttempt: Int, completion: ((Instance) -> Void)?) {
|
|
|
|
// this is main thread only to prevent concurrent access to ownInstanceRequest and pendingOwnInstanceRequestCallbacks
|
|
|
|
assert(Thread.isMainThread)
|
|
|
|
|
2020-03-16 23:07:30 +00:00
|
|
|
if let instance = self.instance {
|
|
|
|
completion?(instance)
|
|
|
|
} else {
|
2021-04-04 19:11:29 +00:00
|
|
|
if let completion = completion {
|
|
|
|
pendingOwnInstanceRequestCallbacks.append(completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ownInstanceRequest == nil {
|
|
|
|
let request = Client.getInstance()
|
|
|
|
ownInstanceRequest = run(request) { (response) in
|
|
|
|
switch response {
|
|
|
|
case .failure(_):
|
|
|
|
let delay: DispatchTimeInterval
|
|
|
|
switch retryAttempt {
|
|
|
|
case 0:
|
|
|
|
delay = .seconds(1)
|
|
|
|
case 1:
|
|
|
|
delay = .seconds(5)
|
|
|
|
case 2:
|
|
|
|
delay = .seconds(30)
|
|
|
|
case 3:
|
|
|
|
delay = .seconds(60)
|
|
|
|
default:
|
|
|
|
// if we've failed four times, just give up :/
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
|
|
|
|
// completion is nil because in this invocation of getOwnInstanceInternal we've already added it to the pending callbacks array
|
|
|
|
self.getOwnInstanceInternal(retryAttempt: retryAttempt + 1, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
case let .success(instance, _):
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.ownInstanceRequest = nil
|
|
|
|
self.instance = instance
|
|
|
|
|
|
|
|
for completion in self.pendingOwnInstanceRequestCallbacks {
|
|
|
|
completion(instance)
|
|
|
|
}
|
|
|
|
self.pendingOwnInstanceRequestCallbacks = []
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 22:04:08 +00:00
|
|
|
}
|
2020-03-16 23:07:30 +00:00
|
|
|
}
|
2018-09-30 02:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 02:14:45 +00:00
|
|
|
func getCustomEmojis(completion: @escaping ([Emoji]) -> Void) {
|
|
|
|
if let emojis = self.customEmojis {
|
|
|
|
completion(emojis)
|
|
|
|
} else {
|
|
|
|
let request = Client.getCustomEmoji()
|
|
|
|
run(request) { (response) in
|
|
|
|
if case let .success(emojis, _) = response {
|
|
|
|
self.customEmojis = emojis
|
|
|
|
completion(emojis)
|
|
|
|
} else {
|
|
|
|
completion([])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:46:19 +00:00
|
|
|
}
|