Start work on MastodonController

This commit is contained in:
Shadowfacts 2018-08-16 18:55:40 -04:00
parent 163f0ad687
commit 85f7e0be10
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 38 additions and 7 deletions

View File

@ -16,6 +16,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch. // Override point for customization after application launch.
MastodonController.shared.connect()
return true return true
} }

View File

@ -9,16 +9,21 @@
import Foundation import Foundation
import MastodonKit import MastodonKit
struct MastodonController { class MastodonController {
static private(set) var shared = MastodonController() static let shared = MastodonController()
var userDefaults = UserDefaults()
var client: Client! var client: Client!
var clientID: String!
var clientSecret: String!
private init() { private init() {
} }
mutating func connect() { func connect() {
// TODO: OAuth // TODO: OAuth
let url = ProcessInfo.processInfo.environment["mastodon_url"]! let url = ProcessInfo.processInfo.environment["mastodon_url"]!
let username = ProcessInfo.processInfo.environment["mastodon_username"]! let username = ProcessInfo.processInfo.environment["mastodon_username"]!
@ -26,11 +31,34 @@ struct MastodonController {
client = Client(baseURL: url) client = Client(baseURL: url)
let loginReq = Login.silent(clientID: "net.shadowfacts.Tusker", clientSecret: "some super secret thing", scopes: [.read, .write, .follow], username: username, password: password) register() {
let loginReq = Login.silent(clientID: self.clientID, clientSecret: self.clientSecret, scopes: [.read, .write, .follow], username: username, password: password)
self.client.run(loginReq) { result in
guard case let .success(loginSettings, _) = result else { fatalError() }
print("access token: \(loginSettings.accessToken)")
}
}
}
private func register(completion: @escaping () -> Void) {
if let clientId = userDefaults.string(forKey: "clientID"),
let clientSecret = userDefaults.string(forKey: "clientSecret") {
self.clientID = clientId
self.clientSecret = clientSecret
completion()
return
}
client.run(loginReq) { result in let registerRequest = Clients.register(clientName: "Tusker", scopes: [.read, .write, .follow])
guard case let .success(loginSettings, _) = result else { fatalError() }
print("access token: \(loginSettings.accessToken)") client.run(registerRequest) { result in
guard case let .success(application, _) = result else { fatalError() }
self.clientID = application.clientID
self.clientSecret = application.clientSecret
self.userDefaults.set(self.clientID, forKey: "clientID")
self.userDefaults.set(self.clientSecret, forKey: "clientSecret")
completion()
} }
} }