Remove duplicate function fetchUser().
This commit is contained in:
parent
843eeed616
commit
a00cd60076
|
@ -28,6 +28,7 @@ final class RemoteProfileViewModel: ProfileViewModel {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
.retry(3)
|
.retry(3)
|
||||||
|
.receive(on: DispatchQueue.main)
|
||||||
.sink { completion in
|
.sink { completion in
|
||||||
switch completion {
|
switch completion {
|
||||||
case .failure(let error):
|
case .failure(let error):
|
||||||
|
@ -46,10 +47,8 @@ final class RemoteProfileViewModel: ProfileViewModel {
|
||||||
assertionFailure()
|
assertionFailure()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.user = mastodonUser
|
self.user = mastodonUser
|
||||||
}
|
}
|
||||||
}
|
|
||||||
.store(in: &disposeBag)
|
.store(in: &disposeBag)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,4 +90,41 @@ final class RemoteProfileViewModel: ProfileViewModel {
|
||||||
} // end Task
|
} // end Task
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -252,21 +252,12 @@ extension SceneDelegate {
|
||||||
let components = url.pathComponents
|
let components = url.pathComponents
|
||||||
if components.count == 2 && components[0] == "/" {
|
if components.count == 2 && components[0] == "/" {
|
||||||
let addr = components[1]
|
let addr = components[1]
|
||||||
let tokens = addr.components(separatedBy: "@")
|
|
||||||
if tokens.count != 2 { return }
|
|
||||||
let username = tokens[0]
|
|
||||||
let host = tokens[1]
|
|
||||||
if let authContext = coordinator?.authContext {
|
if let authContext = coordinator?.authContext {
|
||||||
Task { @MainActor in
|
let profileViewModel = RemoteProfileViewModel(
|
||||||
guard let user = try await AppContext.shared.apiService.fetchUser(
|
context: AppContext.shared,
|
||||||
username: username,
|
|
||||||
domain: host,
|
|
||||||
authenticationBox: authContext.mastodonAuthenticationBox
|
|
||||||
) else { return }
|
|
||||||
|
|
||||||
let profileViewModel = RemoteProfileViewModel(context: AppContext.shared,
|
|
||||||
authContext: authContext,
|
authContext: authContext,
|
||||||
userID: user.id)
|
acct: components[1]
|
||||||
|
)
|
||||||
self.coordinator?.present(
|
self.coordinator?.present(
|
||||||
scene: .profile(viewModel: profileViewModel),
|
scene: .profile(viewModel: profileViewModel),
|
||||||
from: nil,
|
from: nil,
|
||||||
|
@ -274,27 +265,18 @@ extension SceneDelegate {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
case "status":
|
case "status":
|
||||||
let components = url.pathComponents
|
let components = url.pathComponents
|
||||||
if components.count == 2 && components[0] == "/" {
|
if components.count == 2 && components[0] == "/" {
|
||||||
let statusId = components[1]
|
let statusId = components[1]
|
||||||
// View post from user
|
// View post from user
|
||||||
print("view status \(statusId)")
|
|
||||||
if let authContext = coordinator?.authContext {
|
if let authContext = coordinator?.authContext {
|
||||||
Task {
|
let threadViewModel = RemoteThreadViewModel(context: AppContext.shared,
|
||||||
guard let thread = try await AppContext.shared.apiService.fetchThread(
|
|
||||||
statusID: statusId,
|
|
||||||
authenticationBox: authContext.mastodonAuthenticationBox
|
|
||||||
) else { return }
|
|
||||||
|
|
||||||
let threadViewModel = CachedThreadViewModel(context: AppContext.shared,
|
|
||||||
authContext: authContext,
|
authContext: authContext,
|
||||||
status: thread)
|
statusID: statusId)
|
||||||
coordinator?.present(scene: .thread(viewModel: threadViewModel), from: nil, transition: .show)
|
coordinator?.present(scene: .thread(viewModel: threadViewModel), from: nil, transition: .show)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,3 +240,34 @@ extension APIService {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension APIService {
|
||||||
|
public func accountSearch(
|
||||||
|
domain: String,
|
||||||
|
query: Mastodon.API.Account.AccountLookupQuery,
|
||||||
|
authorization: Mastodon.API.OAuth.Authorization
|
||||||
|
) async throws -> Mastodon.Response.Content<Mastodon.Entity.Account> {
|
||||||
|
let response = try await Mastodon.API.Account.lookupAccount(
|
||||||
|
session: session,
|
||||||
|
domain: domain,
|
||||||
|
query: query,
|
||||||
|
authorization: authorization
|
||||||
|
).singleOutput()
|
||||||
|
|
||||||
|
// user
|
||||||
|
let managedObjectContext = self.backgroundManagedObjectContext
|
||||||
|
try await managedObjectContext.performChanges {
|
||||||
|
_ = Persistence.MastodonUser.createOrMerge(
|
||||||
|
in: managedObjectContext,
|
||||||
|
context: Persistence.MastodonUser.PersistContext(
|
||||||
|
domain: domain,
|
||||||
|
entity: response.value,
|
||||||
|
cache: nil,
|
||||||
|
networkDate: response.networkDate
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -50,80 +50,4 @@ extension APIService {
|
||||||
|
|
||||||
return response
|
return response
|
||||||
} // end func
|
} // end func
|
||||||
|
|
||||||
public func fetchThread(
|
|
||||||
statusID: Mastodon.Entity.Status.ID,
|
|
||||||
authenticationBox: MastodonAuthenticationBox
|
|
||||||
) async throws -> Status? {
|
|
||||||
let domain = authenticationBox.domain
|
|
||||||
let authorization = authenticationBox.userAuthorization
|
|
||||||
let managedObjectContext = self.backgroundManagedObjectContext
|
|
||||||
|
|
||||||
let responseOne = try await Mastodon.API.Statuses.status(
|
|
||||||
session: session,
|
|
||||||
domain: domain,
|
|
||||||
statusID: statusID,
|
|
||||||
authorization: authorization
|
|
||||||
).singleOutput()
|
|
||||||
|
|
||||||
try await managedObjectContext.performChanges {
|
|
||||||
let me = authenticationBox.authenticationRecord.object(in: managedObjectContext)?.user
|
|
||||||
_ = Persistence.Status.createOrMerge(
|
|
||||||
in: managedObjectContext,
|
|
||||||
context: Persistence.Status.PersistContext(
|
|
||||||
domain: domain,
|
|
||||||
entity: responseOne.value,
|
|
||||||
me: me,
|
|
||||||
statusCache: nil,
|
|
||||||
userCache: nil,
|
|
||||||
networkDate: responseOne.networkDate
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// let responseTwo = try await Mastodon.API.Statuses.statusContext(
|
|
||||||
// session: session,
|
|
||||||
// domain: domain,
|
|
||||||
// statusID: statusID,
|
|
||||||
// authorization: authorization
|
|
||||||
// ).singleOutput()
|
|
||||||
//
|
|
||||||
// try await managedObjectContext.performChanges {
|
|
||||||
// let me = authenticationBox.authenticationRecord.object(in: managedObjectContext)?.user
|
|
||||||
// let value = responseTwo.value.ancestors + responseTwo.value.descendants
|
|
||||||
//
|
|
||||||
// for entity in value {
|
|
||||||
// _ = Persistence.Status.createOrMerge(
|
|
||||||
// in: managedObjectContext,
|
|
||||||
// context: Persistence.Status.PersistContext(
|
|
||||||
// domain: domain,
|
|
||||||
// entity: entity,
|
|
||||||
// me: me,
|
|
||||||
// statusCache: nil,
|
|
||||||
// userCache: nil,
|
|
||||||
// networkDate: responseTwo.networkDate
|
|
||||||
// )
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
var result: Status?
|
|
||||||
try await managedObjectContext.perform {
|
|
||||||
let me = authenticationBox.authenticationRecord.object(in: managedObjectContext)?.user
|
|
||||||
|
|
||||||
if let status = Persistence.Status.fetch(in: managedObjectContext,
|
|
||||||
context: Persistence.Status.PersistContext(
|
|
||||||
domain: domain,
|
|
||||||
entity: responseOne.value,
|
|
||||||
me: me,
|
|
||||||
statusCache: nil,
|
|
||||||
userCache: nil,
|
|
||||||
networkDate: responseOne.networkDate
|
|
||||||
)) {
|
|
||||||
result = status
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue