2021-04-02 13:33:29 +02:00
|
|
|
//
|
|
|
|
// RemoteProfileViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-2.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import Foundation
|
2021-10-11 13:19:27 +02:00
|
|
|
import Combine
|
2021-04-02 13:33:29 +02:00
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2021-04-02 13:33:29 +02:00
|
|
|
|
|
|
|
final class RemoteProfileViewModel: ProfileViewModel {
|
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
init(context: AppContext, authContext: AuthContext, userID: Mastodon.Entity.Account.ID) {
|
|
|
|
super.init(context: context, authContext: authContext, optionalMastodonUser: nil)
|
2021-04-02 13:33:29 +02:00
|
|
|
|
2023-01-02 00:55:54 +01:00
|
|
|
let domain = authContext.mastodonAuthenticationBox.domain
|
|
|
|
let authorization = authContext.mastodonAuthenticationBox.userAuthorization
|
|
|
|
Just(userID)
|
|
|
|
.asyncMap { userID in
|
|
|
|
try await context.apiService.accountInfo(
|
|
|
|
domain: domain,
|
|
|
|
userID: userID,
|
|
|
|
authorization: authorization
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.retry(3)
|
2023-01-06 00:57:58 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
2023-01-02 00:55:54 +01:00
|
|
|
.sink { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
// TODO: handle error
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote user %s fetch failed: %s", ((#file as NSString).lastPathComponent), #line, #function, userID, error.localizedDescription)
|
|
|
|
case .finished:
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote user %s fetched", ((#file as NSString).lastPathComponent), #line, #function, userID)
|
|
|
|
}
|
|
|
|
} receiveValue: { [weak self] response in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let managedObjectContext = context.managedObjectContext
|
|
|
|
let request = MastodonUser.sortedFetchRequest
|
|
|
|
request.fetchLimit = 1
|
|
|
|
request.predicate = MastodonUser.predicate(domain: domain, id: response.value.id)
|
|
|
|
guard let mastodonUser = managedObjectContext.safeFetch(request).first else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
2023-01-06 00:57:58 +01:00
|
|
|
self.user = mastodonUser
|
2023-01-02 00:55:54 +01:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-02 13:33:29 +02:00
|
|
|
}
|
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
init(context: AppContext, authContext: AuthContext, notificationID: Mastodon.Entity.Notification.ID) {
|
|
|
|
super.init(context: context, authContext: authContext, optionalMastodonUser: nil)
|
2021-10-11 13:19:27 +02:00
|
|
|
|
2022-02-11 08:28:39 +01:00
|
|
|
Task { @MainActor in
|
|
|
|
let response = try await context.apiService.notification(
|
|
|
|
notificationID: notificationID,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
2022-02-11 08:28:39 +01:00
|
|
|
)
|
|
|
|
let userID = response.value.account.id
|
|
|
|
|
|
|
|
let _user: MastodonUser? = try await context.managedObjectContext.perform {
|
|
|
|
let request = MastodonUser.sortedFetchRequest
|
2022-10-09 14:07:57 +02:00
|
|
|
request.predicate = MastodonUser.predicate(domain: authContext.mastodonAuthenticationBox.domain, id: userID)
|
2022-02-11 08:28:39 +01:00
|
|
|
request.fetchLimit = 1
|
|
|
|
return context.managedObjectContext.safeFetch(request).first
|
|
|
|
}
|
|
|
|
|
|
|
|
if let user = _user {
|
|
|
|
self.user = user
|
|
|
|
} else {
|
|
|
|
_ = try await context.apiService.accountInfo(
|
2022-10-09 14:07:57 +02:00
|
|
|
domain: authContext.mastodonAuthenticationBox.domain,
|
2022-02-11 08:28:39 +01:00
|
|
|
userID: userID,
|
2022-10-09 14:07:57 +02:00
|
|
|
authorization: authContext.mastodonAuthenticationBox.userAuthorization
|
2022-02-11 08:28:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
let _user: MastodonUser? = try await context.managedObjectContext.perform {
|
|
|
|
let request = MastodonUser.sortedFetchRequest
|
2022-10-09 14:07:57 +02:00
|
|
|
request.predicate = MastodonUser.predicate(domain: authContext.mastodonAuthenticationBox.domain, id: userID)
|
2022-02-11 08:28:39 +01:00
|
|
|
request.fetchLimit = 1
|
|
|
|
return context.managedObjectContext.safeFetch(request).first
|
|
|
|
}
|
|
|
|
|
|
|
|
self.user = _user
|
|
|
|
}
|
|
|
|
} // end Task
|
2021-10-11 13:19:27 +02:00
|
|
|
}
|
|
|
|
|
2023-01-06 00:57:58 +01:00
|
|
|
init(context: AppContext, authContext: AuthContext, acct: String) {
|
|
|
|
super.init(context: context, authContext: authContext, optionalMastodonUser: nil)
|
|
|
|
|
|
|
|
let domain = authContext.mastodonAuthenticationBox.domain
|
|
|
|
let authorization = authContext.mastodonAuthenticationBox.userAuthorization
|
|
|
|
Just(acct)
|
|
|
|
.asyncMap { acct in
|
|
|
|
try await context.apiService.accountSearch(
|
|
|
|
domain: domain,
|
|
|
|
query: .init(acct: acct),
|
|
|
|
authorization: authorization
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.retry(3)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
// TODO: handle error
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote user %s fetch failed: %s", ((#file as NSString).lastPathComponent), #line, #function, acct, error.localizedDescription)
|
|
|
|
case .finished:
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote user %s fetched", ((#file as NSString).lastPathComponent), #line, #function, acct)
|
|
|
|
}
|
|
|
|
} receiveValue: { [weak self] response in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let managedObjectContext = context.managedObjectContext
|
|
|
|
let request = MastodonUser.sortedFetchRequest
|
|
|
|
request.fetchLimit = 1
|
|
|
|
request.predicate = MastodonUser.predicate(domain: domain, id: response.value.id)
|
|
|
|
guard let mastodonUser = managedObjectContext.safeFetch(request).first else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.user = mastodonUser
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
2021-04-02 13:33:29 +02:00
|
|
|
}
|