From 85f7e0be10547f7c0012b7552c59352c41b9103b 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 d6460736af..d930cd6443 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 71cba902cf..12055823d6 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() } }