Tusker/Tusker/Controllers/MastodonController.swift

65 lines
1.9 KiB
Swift

//
// MastodonController.swift
// Tusker
//
// Created by Shadowfacts on 8/15/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
import Pachyderm
class MastodonController {
static let shared = MastodonController()
var client: Client!
var account: Account!
private init() {
}
func createClient() {
guard let url = LocalData.shared.instanceURL else { fatalError("Can't connect without instance URL") }
client = Client(baseURL: url)
client.clientID = LocalData.shared.clientID
client.clientSecret = LocalData.shared.clientSecret
client.accessToken = LocalData.shared.accessToken
}
func registerApp(completion: @escaping () -> Void) {
guard LocalData.shared.clientID == nil,
LocalData.shared.clientSecret == nil else {
completion()
return
}
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
completion()
}
}
func authorize(authorizationCode: String, completion: @escaping () -> Void) {
client.getAccessToken(authorizationCode: authorizationCode, redirectURI: "tusker://oauth") { response in
guard case let .success(settings, _) = response else { fatalError() }
LocalData.shared.accessToken = settings.accessToken
completion()
}
}
func getOwnAccount() {
let request = client.getSelfAccount()
client.run(request) { response in
guard case let .success(account, _) = response else { fatalError() }
self.account = account
}
}
}