1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Model/Accounts/AccountValidator.swift

135 lines
3.5 KiB
Swift
Raw Normal View History

2021-09-25 13:48:22 +05:30
import Foundation
import Siesta
import SwiftUI
final class AccountValidator: Service {
2021-10-21 03:51:50 +05:30
let app: Binding<VideosApp>
2021-09-25 13:48:22 +05:30
let url: String
2021-10-21 03:51:50 +05:30
let account: Account?
2021-09-25 13:48:22 +05:30
var formObjectID: Binding<String>
var isValid: Binding<Bool>
var isValidated: Binding<Bool>
var isValidating: Binding<Bool>
2021-09-25 13:48:22 +05:30
var error: Binding<String?>?
init(
2021-10-21 03:51:50 +05:30
app: Binding<VideosApp>,
2021-09-25 13:48:22 +05:30
url: String,
2021-10-21 03:51:50 +05:30
account: Account? = nil,
id: Binding<String>,
isValid: Binding<Bool>,
isValidated: Binding<Bool>,
isValidating: Binding<Bool>,
2021-09-25 13:48:22 +05:30
error: Binding<String?>? = nil
) {
2021-10-17 04:18:58 +05:30
self.app = app
2021-09-25 13:48:22 +05:30
self.url = url
self.account = account
formObjectID = id
self.isValid = isValid
self.isValidated = isValidated
self.isValidating = isValidating
2021-09-25 13:48:22 +05:30
self.error = error
super.init(baseURL: url)
configure()
}
func configure() {
2021-10-17 04:18:58 +05:30
configure {
$0.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
}
2021-09-25 13:48:22 +05:30
configure("/api/v1/auth/feed", requestMethods: [.get]) {
guard self.account != nil else {
return
}
$0.headers["Cookie"] = self.cookieHeader
}
}
func validateInstance() {
reset()
2021-11-08 03:09:28 +05:30
neverGonnaGiveYouUp
2021-09-25 13:48:22 +05:30
.load()
2021-10-17 04:18:58 +05:30
.onSuccess { response in
2021-09-25 13:48:22 +05:30
guard self.url == self.formObjectID.wrappedValue else {
return
}
2021-11-08 03:09:28 +05:30
let json = response.json.dictionaryValue
let author = self.app.wrappedValue == .invidious ? json["author"] : json["uploader"]
if author == "Rick Astley" {
2021-10-17 04:18:58 +05:30
self.isValid.wrappedValue = true
self.error?.wrappedValue = nil
} else {
self.isValid.wrappedValue = false
}
2021-09-25 13:48:22 +05:30
}
.onFailure { error in
guard self.url == self.formObjectID.wrappedValue else {
return
}
self.isValid.wrappedValue = false
2021-09-25 13:48:22 +05:30
self.error?.wrappedValue = error.userMessage
}
.onCompletion { _ in
self.isValidated.wrappedValue = true
self.isValidating.wrappedValue = false
2021-09-25 13:48:22 +05:30
}
}
2021-10-17 04:18:58 +05:30
func validateInvidiousAccount() {
2021-09-25 13:48:22 +05:30
reset()
feed
.load()
.onSuccess { _ in
guard self.account!.sid == self.formObjectID.wrappedValue else {
return
}
self.isValid.wrappedValue = true
2021-09-25 13:48:22 +05:30
}
.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 13:48:22 +05:30
}
}
func reset() {
isValid.wrappedValue = false
isValidated.wrappedValue = false
isValidating.wrappedValue = false
2021-09-25 13:48:22 +05:30
error?.wrappedValue = nil
}
var cookieHeader: String {
"SID=\(account!.sid)"
}
var feed: Resource {
resource("/api/v1/auth/feed")
}
2021-11-08 03:09:28 +05:30
var videoResourceBasePath: String {
app.wrappedValue == .invidious ? "/api/v1/videos" : "/streams"
}
var neverGonnaGiveYouUp: Resource {
resource("\(videoResourceBasePath)/dQw4w9WgXcQ")
}
2021-09-25 13:48:22 +05:30
}