Tusker/Tusker/Controllers/MastodonController.swift

77 lines
2.4 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
2018-10-02 23:31:00 +00:00
static var client: Client!
2018-08-16 22:55:40 +00:00
2018-10-02 23:31:00 +00:00
static var account: Account!
static var instance: Instance!
2018-08-16 11:46:19 +00:00
2018-10-02 23:31:00 +00:00
private init() {}
2018-08-31 02:30:19 +00:00
2018-10-02 23:31:00 +00:00
static func createClient() {
2018-08-19 20:14:04 +00:00
guard let url = LocalData.shared.instanceURL else { fatalError("Can't connect without instance URL") }
2018-08-16 11:46:19 +00:00
2018-08-19 20:14:04 +00:00
client = Client(baseURL: url)
client.clientID = LocalData.shared.clientID
client.clientSecret = LocalData.shared.clientSecret
client.accessToken = LocalData.shared.accessToken
2018-08-16 22:55:40 +00:00
}
2018-10-02 23:31:00 +00:00
static 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
}
2018-10-02 23:31:00 +00:00
static 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
}
}
2018-10-02 23:31:00 +00:00
static func getOwnAccount(completion: ((Account) -> Void)? = nil) {
2018-10-02 23:23:50 +00:00
if account != nil {
completion?(account)
} else {
let request = client.getSelfAccount()
client.run(request) { response in
guard case let .success(account, _) = response else { fatalError() }
self.account = account
2018-10-20 16:03:18 +00:00
MastodonCache.add(account: account)
2018-10-02 23:23:50 +00:00
completion?(account)
}
2018-08-31 02:30:19 +00:00
}
}
2018-10-02 23:31:00 +00:00
static func getOwnInstance() {
2018-09-30 02:20:17 +00:00
let request = client.getInstance()
client.run(request) { (response) in
guard case let .success(instance, _) = response else { fatalError() }
self.instance = instance
}
}
2018-08-16 11:46:19 +00:00
}