Tusker/Tusker/Controllers/MastodonController.swift

88 lines
2.8 KiB
Swift
Raw Normal View History

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 {
2018-08-16 11:46:19 +00:00
@available(*, deprecated, message: "Use dependency injection to obtain an instance")
static let shared = MastodonController()
2018-08-16 22:55:40 +00:00
private(set) lazy var cache = MastodonCache(mastodonController: self)
private var client: Client!
2018-08-16 11:46:19 +00:00
var account: Account!
var instance: Instance!
var accessToken: String? {
client?.accessToken
}
func createClient(instanceURL: URL = LocalData.shared.instanceURL!) {
client = Client(baseURL: instanceURL)
2018-08-16 11:46:19 +00:00
if instanceURL == LocalData.shared.instanceURL {
client.clientID = LocalData.shared.clientID
client.clientSecret = LocalData.shared.clientSecret
client.accessToken = LocalData.shared.accessToken
}
2018-08-16 22:55:40 +00:00
}
func run<Result>(_ request: Request<Result>, completion: @escaping Client.Callback<Result>) {
client.run(request, completion: completion)
}
func registerApp(completion: @escaping () -> Void) {
2018-08-19 20:14:04 +00:00
guard LocalData.shared.clientID == nil,
LocalData.shared.clientSecret == nil else {
2018-08-17 00:11:56 +00:00
completion()
2018-08-19 20:14:04 +00:00
return
}
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() }
LocalData.shared.clientID = app.clientID
LocalData.shared.clientSecret = app.clientSecret
2018-08-19 20:14:04 +00:00
completion()
2018-08-16 22:55:40 +00:00
}
2018-08-17 00:11:56 +00:00
}
func authorize(authorizationCode: String, completion: @escaping () -> 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() }
2018-08-19 20:14:04 +00:00
LocalData.shared.accessToken = settings.accessToken
completion()
2018-08-16 11:46:19 +00:00
}
}
func getOwnAccount(completion: ((Account) -> Void)? = nil) {
2018-10-02 23:23:50 +00:00
if account != nil {
completion?(account)
} else {
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
self.cache.add(account: account)
2018-10-02 23:23:50 +00:00
completion?(account)
}
2018-08-31 02:30:19 +00:00
}
}
func getOwnInstance() {
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
}