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-08-16 22:55:40 +00:00
|
|
|
static let shared = MastodonController()
|
|
|
|
|
2018-08-16 11:46:19 +00:00
|
|
|
var client: Client!
|
|
|
|
|
2018-08-31 02:30:19 +00:00
|
|
|
var account: Account!
|
|
|
|
|
2018-08-16 11:46:19 +00:00
|
|
|
private init() {
|
|
|
|
}
|
|
|
|
|
2018-08-20 21:23:35 +00:00
|
|
|
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)
|
|
|
|
|
2018-09-13 02:29:58 +00:00
|
|
|
client.clientID = LocalData.shared.clientID
|
|
|
|
client.clientSecret = LocalData.shared.clientSecret
|
|
|
|
client.accessToken = LocalData.shared.accessToken
|
2018-08-16 22:55:40 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 21:23:35 +00:00
|
|
|
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-08-19 20:14:04 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:30:19 +00:00
|
|
|
func getOwnAccount() {
|
2018-09-18 00:58:05 +00:00
|
|
|
let request = client.getSelfAccount()
|
|
|
|
client.run(request) { response in
|
2018-09-11 14:52:21 +00:00
|
|
|
guard case let .success(account, _) = response else { fatalError() }
|
2018-08-31 02:30:19 +00:00
|
|
|
self.account = account
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:46:19 +00:00
|
|
|
}
|