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
|
|
|
|
2018-08-16 22:55:40 +00:00
|
|
|
class MastodonController {
|
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-01-06 00:54:28 +00:00
|
|
|
private(set) lazy var cache = MastodonCache(mastodonController: self)
|
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
let instanceURL: URL
|
|
|
|
private(set) var accountInfo: LocalData.UserAccountInfo?
|
|
|
|
|
|
|
|
let client: Client!
|
2018-08-16 11:46:19 +00:00
|
|
|
|
2020-01-05 20:25:07 +00:00
|
|
|
var account: Account!
|
|
|
|
var instance: Instance!
|
2020-01-08 02:29:15 +00:00
|
|
|
|
|
|
|
init(instanceURL: URL) {
|
|
|
|
self.instanceURL = instanceURL
|
|
|
|
self.accountInfo = nil
|
|
|
|
self.client = Client(baseURL: instanceURL)
|
2018-08-16 22:55:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 20:25:07 +00:00
|
|
|
func run<Result>(_ request: Request<Result>, completion: @escaping Client.Callback<Result>) {
|
2020-01-05 19:00:39 +00:00
|
|
|
client.run(request, completion: completion)
|
|
|
|
}
|
|
|
|
|
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-01-05 20:25:07 +00:00
|
|
|
func getOwnAccount(completion: ((Account) -> Void)? = nil) {
|
2018-10-02 23:23:50 +00:00
|
|
|
if account != nil {
|
|
|
|
completion?(account)
|
|
|
|
} else {
|
2020-01-05 19:00:39 +00:00
|
|
|
let request = Client.getSelfAccount()
|
|
|
|
run(request) { response in
|
2018-10-02 23:23:50 +00:00
|
|
|
guard case let .success(account, _) = response else { fatalError() }
|
|
|
|
self.account = account
|
2020-01-06 00:54:28 +00:00
|
|
|
self.cache.add(account: account)
|
2018-10-02 23:23:50 +00:00
|
|
|
completion?(account)
|
|
|
|
}
|
2018-08-31 02:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 20:25:07 +00:00
|
|
|
func getOwnInstance() {
|
2020-01-05 19:00:39 +00:00
|
|
|
let request = Client.getInstance()
|
|
|
|
run(request) { (response) in
|
2018-09-30 02:20:17 +00:00
|
|
|
guard case let .success(instance, _) = response else { fatalError() }
|
|
|
|
self.instance = instance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:46:19 +00:00
|
|
|
}
|