forked from shadowfacts/Tusker
59 lines
1.9 KiB
Swift
59 lines
1.9 KiB
Swift
//
|
|
// MastodonController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 8/15/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import MastodonKit
|
|
|
|
class MastodonController {
|
|
|
|
static let shared = MastodonController()
|
|
|
|
var client: Client!
|
|
|
|
private init() {
|
|
}
|
|
|
|
func createClient() {
|
|
guard let url = LocalData.shared.instanceURL else { fatalError("Can't connect without instance URL") }
|
|
|
|
client = Client(baseURL: url)
|
|
|
|
if let accessToken = LocalData.shared.accessToken {
|
|
client.accessToken = accessToken
|
|
}
|
|
}
|
|
|
|
func registerApp(completion: @escaping () -> Void) {
|
|
guard LocalData.shared.clientID == nil,
|
|
LocalData.shared.clientSecret == nil else {
|
|
completion()
|
|
return
|
|
}
|
|
|
|
let registerRequest = Clients.register(clientName: "Tusker", redirectURI: "tusker://oauth", scopes: [.read, .write, .follow])
|
|
|
|
client.run(registerRequest) { result in
|
|
guard case let .success(application, _) = result else { fatalError() }
|
|
LocalData.shared.clientID = application.clientID
|
|
LocalData.shared.clientSecret = application.clientSecret
|
|
completion()
|
|
}
|
|
}
|
|
|
|
func authorize(authorizationCode: String, completion: @escaping () -> Void) {
|
|
let authorizeRequest = Login.authorize(code: authorizationCode, clientID: LocalData.shared.clientID!, clientSecret: LocalData.shared.clientSecret!, redirectURI: "tusker://oauth")
|
|
client.run(authorizeRequest) { result in
|
|
guard case let .success(settings, _) = result else { fatalError() }
|
|
LocalData.shared.accessToken = settings.accessToken
|
|
self.client.accessToken = settings.accessToken
|
|
completion()
|
|
}
|
|
}
|
|
|
|
}
|