Initial PeerTube Support

This commit is contained in:
Arkadiusz Fal
2022-12-09 01:15:19 +01:00
parent 72ea17b257
commit faf2469e04
39 changed files with 816 additions and 92 deletions

View File

@@ -8,7 +8,7 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
var app: VideosApp?
let instanceID: String?
var name: String?
let url: String
let urlString: String
var username: String
var password: String?
let anonymous: Bool
@@ -20,7 +20,7 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
app: VideosApp? = nil,
instanceID: String? = nil,
name: String? = nil,
url: String? = nil,
urlString: String? = nil,
username: String? = nil,
password: String? = nil,
anonymous: Bool = false,
@@ -29,10 +29,10 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
) {
self.anonymous = anonymous
self.id = id ?? (anonymous ? "anonymous-\(instanceID ?? url ?? UUID().uuidString)" : UUID().uuidString)
self.id = id ?? (anonymous ? "anonymous-\(instanceID ?? urlString ?? UUID().uuidString)" : UUID().uuidString)
self.instanceID = instanceID
self.name = name
self.url = url ?? ""
self.urlString = urlString ?? ""
self.username = username ?? ""
self.password = password ?? ""
self.country = country
@@ -40,6 +40,10 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
self.app = app ?? instance.app
}
var url: URL! {
URL(string: urlString)
}
var token: String? {
KeychainModel.shared.getAccountKey(self, "token")
}
@@ -49,7 +53,7 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
}
var instance: Instance! {
Defaults[.instances].first { $0.id == instanceID } ?? Instance(app: app ?? .invidious, name: url, apiURL: url)
Defaults[.instances].first { $0.id == instanceID } ?? Instance(app: app ?? .invidious, name: urlString, apiURLString: urlString)
}
var isPublic: Bool {

View File

@@ -56,12 +56,23 @@ final class AccountValidator: Service {
case .piped:
return resource("/streams/dQw4w9WgXcQ")
case .peerTube:
// TODO: fixme
return resource("")
case .local:
return resource("")
}
}
func validateInstance() {
reset()
app.wrappedValue = .peerTube
setValidationResult(true)
return
guard let app = appsToValidateInstance.popLast() else { return }
tryValidatingUsing(app)
}

View File

@@ -14,7 +14,7 @@ struct AccountsBridge: Defaults.Bridge {
"id": value.id,
"instanceID": value.instanceID ?? "",
"name": value.name ?? "",
"apiURL": value.url,
"apiURL": value.urlString,
"username": value.username,
"password": value.password ?? ""
]
@@ -34,6 +34,6 @@ struct AccountsBridge: Defaults.Bridge {
let name = object["name"] ?? ""
let password = object["password"]
return Account(id: id, instanceID: instanceID, name: name, url: url, username: username, password: password)
return Account(id: id, instanceID: instanceID, name: name, urlString: url, username: username, password: password)
}
}

View File

@@ -9,6 +9,7 @@ final class AccountsModel: ObservableObject {
@Published private var invidious = InvidiousAPI()
@Published private var piped = PipedAPI()
@Published private var peerTube = PeerTubeAPI()
@Published var publicAccount: Account?
@@ -31,15 +32,19 @@ final class AccountsModel: ObservableObject {
}
var app: VideosApp {
current?.instance?.app ?? .invidious
current?.instance?.app ?? .local
}
var api: VideosAPI {
var api: VideosAPI! {
switch app {
case .piped:
return piped
case .invidious:
return invidious
case .peerTube:
return peerTube
default:
return nil
}
}
@@ -83,10 +88,14 @@ final class AccountsModel: ObservableObject {
}
switch account.instance.app {
case .local:
return
case .invidious:
invidious.setAccount(account)
case .piped:
piped.setAccount(account)
case .peerTube:
peerTube.setAccount(account)
}
Defaults[.lastAccountIsPublic] = account.isPublic
@@ -102,7 +111,7 @@ final class AccountsModel: ObservableObject {
}
static func add(instance: Instance, name: String, username: String, password: String) -> Account {
let account = Account(instanceID: instance.id, name: name, url: instance.apiURL)
let account = Account(instanceID: instance.id, name: name, urlString: instance.apiURLString)
Defaults[.accounts].append(account)
setCredentials(account, username: username, password: password)

View File

@@ -7,25 +7,33 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
let app: VideosApp
let id: String
let name: String
let apiURL: String
let apiURLString: String
var frontendURL: String?
var proxiesVideos: Bool
init(app: VideosApp, id: String? = nil, name: String, apiURL: String, frontendURL: String? = nil, proxiesVideos: Bool = false) {
init(app: VideosApp, id: String? = nil, name: String? = nil, apiURLString: String, frontendURL: String? = nil, proxiesVideos: Bool = false) {
self.app = app
self.id = id ?? UUID().uuidString
self.name = name
self.apiURL = apiURL
self.name = name ?? app.rawValue
self.apiURLString = apiURLString
self.frontendURL = frontendURL
self.proxiesVideos = proxiesVideos
}
var anonymous: VideosAPI {
var apiURL: URL! {
URL(string: apiURLString)
}
var anonymous: VideosAPI! {
switch app {
case .invidious:
return InvidiousAPI(account: anonymousAccount)
case .piped:
return PipedAPI(account: anonymousAccount)
case .peerTube:
return PeerTubeAPI(account: anonymousAccount)
case .local:
return nil
}
}
@@ -34,23 +42,23 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
}
var longDescription: String {
name.isEmpty ? "\(app.name) - \(apiURL)" : "\(app.name) - \(name) (\(apiURL))"
name.isEmpty ? "\(app.name) - \(apiURLString)" : "\(app.name) - \(name) (\(apiURL))"
}
var shortDescription: String {
name.isEmpty ? apiURL : name
name.isEmpty ? apiURLString : name
}
var anonymousAccount: Account {
Account(instanceID: id, name: "Anonymous".localized(), url: apiURL, anonymous: true)
Account(instanceID: id, name: "Anonymous".localized(), urlString: apiURLString, anonymous: true)
}
var urlComponents: URLComponents {
URLComponents(string: apiURL)!
URLComponents(url: apiURL, resolvingAgainstBaseURL: false)!
}
var frontendHost: String? {
guard let url = app == .invidious ? apiURL : frontendURL else {
guard let url = app == .invidious ? apiURLString : frontendURL else {
return nil
}

View File

@@ -14,7 +14,7 @@ struct InstancesBridge: Defaults.Bridge {
"app": value.app.rawValue,
"id": value.id,
"name": value.name,
"apiURL": value.apiURL,
"apiURL": value.apiURLString,
"frontendURL": value.frontendURL ?? "",
"proxiesVideos": value.proxiesVideos ? "true" : "false"
]
@@ -34,6 +34,6 @@ struct InstancesBridge: Defaults.Bridge {
let frontendURL: String? = object["frontendURL"]!.isEmpty ? nil : object["frontendURL"]
let proxiesVideos = object["proxiesVideos"] == "true"
return Instance(app: app, id: id, name: name, apiURL: apiURL, frontendURL: frontendURL, proxiesVideos: proxiesVideos)
return Instance(app: app, id: id, name: name, apiURLString: apiURL, frontendURL: frontendURL, proxiesVideos: proxiesVideos)
}
}

View File

@@ -38,7 +38,7 @@ final class InstancesModel: ObservableObject {
func add(app: VideosApp, name: String, url: String) -> Instance {
let instance = Instance(
app: app, id: UUID().uuidString, name: name, apiURL: standardizedURL(url)
app: app, id: UUID().uuidString, name: name, apiURLString: standardizedURL(url)
)
Defaults[.instances].append(instance)