yattee/Model/AccountValidator.swift

144 lines
3.8 KiB
Swift
Raw Normal View History

2021-09-25 08:18:22 +00:00
import Foundation
import Siesta
import SwiftUI
final class AccountValidator: Service {
2021-10-16 22:48:58 +00:00
let app: Binding<Instance.App>
2021-09-25 08:18:22 +00:00
let url: String
let account: Instance.Account?
var formObjectID: Binding<String>
var isValid: Binding<Bool>
var isValidated: Binding<Bool>
var isValidating: Binding<Bool>
2021-09-25 08:18:22 +00:00
var error: Binding<String?>?
init(
2021-10-16 22:48:58 +00:00
app: Binding<Instance.App>,
2021-09-25 08:18:22 +00:00
url: String,
account: Instance.Account? = nil,
id: Binding<String>,
isValid: Binding<Bool>,
isValidated: Binding<Bool>,
isValidating: Binding<Bool>,
2021-09-25 08:18:22 +00:00
error: Binding<String?>? = nil
) {
2021-10-16 22:48:58 +00:00
self.app = app
2021-09-25 08:18:22 +00:00
self.url = url
self.account = account
formObjectID = id
self.isValid = isValid
self.isValidated = isValidated
self.isValidating = isValidating
2021-09-25 08:18:22 +00:00
self.error = error
super.init(baseURL: url)
configure()
}
func configure() {
2021-10-16 22:48:58 +00:00
configure {
$0.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
}
2021-09-25 08:18:22 +00:00
configure("/api/v1/auth/feed", requestMethods: [.get]) {
guard self.account != nil else {
return
}
$0.headers["Cookie"] = self.cookieHeader
}
}
func validateInstance() {
reset()
2021-10-16 22:48:58 +00:00
// TODO: validation for Piped instances
guard app.wrappedValue == .invidious else {
isValid.wrappedValue = true
error?.wrappedValue = nil
isValidated.wrappedValue = true
isValidating.wrappedValue = false
return
}
2021-09-25 08:18:22 +00:00
stats
.load()
2021-10-16 22:48:58 +00:00
.onSuccess { response in
2021-09-25 08:18:22 +00:00
guard self.url == self.formObjectID.wrappedValue else {
return
}
2021-10-16 22:48:58 +00:00
if response
.json
.dictionaryValue["software"]?
.dictionaryValue["name"]?
.stringValue == "invidious"
{
self.isValid.wrappedValue = true
self.error?.wrappedValue = nil
} else {
self.isValid.wrappedValue = false
self.error?.wrappedValue = "Not an Invidious Instance"
}
2021-09-25 08:18:22 +00:00
}
.onFailure { error in
guard self.url == self.formObjectID.wrappedValue else {
return
}
self.isValid.wrappedValue = false
2021-09-25 08:18:22 +00:00
self.error?.wrappedValue = error.userMessage
}
.onCompletion { _ in
self.isValidated.wrappedValue = true
self.isValidating.wrappedValue = false
2021-09-25 08:18:22 +00:00
}
}
2021-10-16 22:48:58 +00:00
func validateInvidiousAccount() {
2021-09-25 08:18:22 +00:00
reset()
feed
.load()
.onSuccess { _ in
guard self.account!.sid == self.formObjectID.wrappedValue else {
return
}
self.isValid.wrappedValue = true
2021-09-25 08:18:22 +00:00
}
.onFailure { _ in
guard self.account!.sid == self.formObjectID.wrappedValue else {
return
}
self.isValid.wrappedValue = false
}
.onCompletion { _ in
self.isValidated.wrappedValue = true
self.isValidating.wrappedValue = false
2021-09-25 08:18:22 +00:00
}
}
func reset() {
isValid.wrappedValue = false
isValidated.wrappedValue = false
isValidating.wrappedValue = false
2021-09-25 08:18:22 +00:00
error?.wrappedValue = nil
}
var cookieHeader: String {
"SID=\(account!.sid)"
}
var stats: Resource {
resource("/api/v1/stats")
}
var feed: Resource {
resource("/api/v1/auth/feed")
}
}