Start work on MastodonController
This commit is contained in:
parent
163f0ad687
commit
85f7e0be10
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue