2021-10-09 13:01:08 +02:00
|
|
|
//
|
|
|
|
// InstanceService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-10-9.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public final class InstanceService {
|
2021-10-09 13:01:08 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2023-09-21 12:52:05 +02:00
|
|
|
|
2021-10-09 13:01:08 +02:00
|
|
|
// input
|
|
|
|
let backgroundManagedObjectContext: NSManagedObjectContext
|
|
|
|
weak var apiService: APIService?
|
|
|
|
weak var authenticationService: AuthenticationService?
|
|
|
|
|
|
|
|
// output
|
2022-11-14 13:12:16 +01:00
|
|
|
|
2021-10-09 13:01:08 +02:00
|
|
|
init(
|
|
|
|
apiService: APIService,
|
|
|
|
authenticationService: AuthenticationService
|
|
|
|
) {
|
|
|
|
self.backgroundManagedObjectContext = apiService.backgroundManagedObjectContext
|
|
|
|
self.apiService = apiService
|
|
|
|
self.authenticationService = authenticationService
|
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationService.$mastodonAuthenticationBoxes
|
2021-10-09 13:01:08 +02:00
|
|
|
.receive(on: DispatchQueue.main)
|
2022-10-09 14:07:57 +02:00
|
|
|
.compactMap { $0.first?.domain }
|
2021-10-09 13:01:08 +02:00
|
|
|
.removeDuplicates() // prevent infinity loop
|
2024-06-25 11:36:04 +02:00
|
|
|
.asyncMap { [weak self] in await self?.updateInstance(domain: $0) }
|
2024-06-26 14:37:27 +02:00
|
|
|
.sink {}
|
2021-10-09 13:01:08 +02:00
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension InstanceService {
|
2024-06-25 11:36:04 +02:00
|
|
|
|
|
|
|
@MainActor
|
|
|
|
func updateInstance(domain: String) async {
|
2024-05-08 18:12:52 +02:00
|
|
|
guard let apiService else { return }
|
2024-06-25 11:36:04 +02:00
|
|
|
|
|
|
|
let response = try? await apiService.instance(domain: domain, authenticationBox: authenticationService?.mastodonAuthenticationBoxes.first)
|
|
|
|
.singleOutput()
|
|
|
|
|
|
|
|
if response?.value.version?.majorServerVersion(greaterThanOrEquals: 4) == true {
|
|
|
|
guard let instanceV2 = try? await apiService.instanceV2(domain: domain, authenticationBox: authenticationService?.mastodonAuthenticationBoxes.first).singleOutput() else {
|
|
|
|
return
|
2021-10-09 13:01:08 +02:00
|
|
|
}
|
2024-06-25 11:36:04 +02:00
|
|
|
|
|
|
|
self.updateInstanceV2(domain: domain, response: instanceV2)
|
|
|
|
if let translationResponse = try? await apiService.translationLanguages(domain: domain, authenticationBox: authenticationService?.mastodonAuthenticationBoxes.first).singleOutput() {
|
|
|
|
updateTranslationLanguages(domain: domain, response: translationResponse)
|
2021-10-09 13:01:08 +02:00
|
|
|
}
|
2024-06-25 11:36:04 +02:00
|
|
|
} else if let response {
|
|
|
|
self.updateInstance(domain: domain, response: response)
|
|
|
|
}
|
2024-06-21 12:09:49 +02:00
|
|
|
}
|
2024-06-25 11:36:04 +02:00
|
|
|
|
|
|
|
@MainActor
|
2024-06-21 12:09:49 +02:00
|
|
|
private func updateTranslationLanguages(domain: String, response: Mastodon.Response.Content<TranslationLanguages>) {
|
|
|
|
AuthenticationServiceProvider.shared
|
|
|
|
.updating(translationLanguages: response.value, for: domain)
|
|
|
|
}
|
|
|
|
|
2024-06-25 11:36:04 +02:00
|
|
|
@MainActor
|
|
|
|
private func updateInstance(domain: String, response: Mastodon.Response.Content<Mastodon.Entity.Instance>) {
|
|
|
|
AuthenticationServiceProvider.shared
|
|
|
|
.updating(instanceV1: response.value, for: domain)
|
2022-12-12 16:41:13 +01:00
|
|
|
}
|
|
|
|
|
2024-06-25 11:36:04 +02:00
|
|
|
@MainActor
|
|
|
|
private func updateInstanceV2(domain: String, response: Mastodon.Response.Content<Mastodon.Entity.V2.Instance>) {
|
2024-06-21 12:09:49 +02:00
|
|
|
AuthenticationServiceProvider.shared
|
2024-06-25 11:36:04 +02:00
|
|
|
.updating(instanceV2: response.value, for: domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension String {
|
|
|
|
func majorServerVersion(greaterThanOrEquals comparedVersion: Int) -> Bool {
|
|
|
|
guard
|
|
|
|
let majorVersionString = split(separator: ".").first,
|
|
|
|
let majorVersionInt = Int(majorVersionString)
|
|
|
|
else { return false }
|
|
|
|
|
|
|
|
return majorVersionInt >= comparedVersion
|
2022-12-12 16:41:13 +01:00
|
|
|
}
|
2021-10-09 13:01:08 +02:00
|
|
|
}
|