From 26aeb515be0f66d3b53cc753f8243331e3664388 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 16 Aug 2018 18:55:40 -0400 Subject: [PATCH] Start work on MastodonController --- Tusker/AppDelegate.swift | 3 ++ Tusker/Controllers/MastodonController.swift | 42 +++++++++++++++++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Tusker/AppDelegate.swift b/Tusker/AppDelegate.swift index d6460736..d930cd64 100644 --- a/Tusker/AppDelegate.swift +++ b/Tusker/AppDelegate.swift @@ -16,6 +16,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. + + MastodonController.shared.connect() + return true } diff --git a/Tusker/Controllers/MastodonController.swift b/Tusker/Controllers/MastodonController.swift index 71cba902..12055823 100644 --- a/Tusker/Controllers/MastodonController.swift +++ b/Tusker/Controllers/MastodonController.swift @@ -9,16 +9,21 @@ import Foundation import MastodonKit -struct MastodonController { +class MastodonController { - static private(set) var shared = MastodonController() + static let shared = MastodonController() + + var userDefaults = UserDefaults() var client: Client! + var clientID: String! + var clientSecret: String! + private init() { } - mutating func connect() { + func connect() { // TODO: OAuth let url = ProcessInfo.processInfo.environment["mastodon_url"]! let username = ProcessInfo.processInfo.environment["mastodon_username"]! @@ -26,11 +31,34 @@ struct MastodonController { 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 - guard case let .success(loginSettings, _) = result else { fatalError() } - print("access token: \(loginSettings.accessToken)") + let registerRequest = Clients.register(clientName: "Tusker", scopes: [.read, .write, .follow]) + + 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() } }