Merge pull request #862 from mastodon/fix-urlscheme-profile-resolve

fix(deeplinking): Fix profile resolving didn't use WebFinger so resolving non-local profiles might fail
This commit is contained in:
Marcus Kida 2023-01-11 15:31:22 +01:00 committed by GitHub
commit 83f4eb1d8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 68 deletions

View File

@ -90,41 +90,41 @@ final class RemoteProfileViewModel: ProfileViewModel {
} // end Task } // end Task
} }
init(context: AppContext, authContext: AuthContext, acct: String) { init(context: AppContext, authContext: AuthContext, acct: String){
super.init(context: context, authContext: authContext, optionalMastodonUser: nil) super.init(context: context, authContext: authContext, optionalMastodonUser: nil)
let domain = authContext.mastodonAuthenticationBox.domain let domain = authContext.mastodonAuthenticationBox.domain
let authorization = authContext.mastodonAuthenticationBox.userAuthorization let authenticationBox = authContext.mastodonAuthenticationBox
Just(acct)
.asyncMap { acct in Just(acct)
try await context.apiService.accountSearch( .asyncMap { acct -> Mastodon.Response.Content<Mastodon.Entity.Account?> in
domain: domain, try await context.apiService.search(
query: .init(acct: acct), query: .init(q: acct, type: .accounts, resolve: true),
authorization: authorization authenticationBox: authenticationBox
) ).map { $0.accounts.first }
} }
.retry(3) .retry(3)
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { completion in .sink { completion in
switch completion { switch completion {
case .failure(let error): case .failure(let error):
// TODO: handle 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) 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: case .finished:
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote user %s fetched", ((#file as NSString).lastPathComponent), #line, #function, acct) 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 } receiveValue: { [weak self] response in
guard let self = self else { return } guard let self = self, let value = response.value else { return }
let managedObjectContext = context.managedObjectContext let managedObjectContext = context.managedObjectContext
let request = MastodonUser.sortedFetchRequest let request = MastodonUser.sortedFetchRequest
request.fetchLimit = 1 request.fetchLimit = 1
request.predicate = MastodonUser.predicate(domain: domain, id: response.value.id) request.predicate = MastodonUser.predicate(domain: domain, id: value.id)
guard let mastodonUser = managedObjectContext.safeFetch(request).first else { guard let mastodonUser = managedObjectContext.safeFetch(request).first else {
assertionFailure() assertionFailure()
return return
} }
self.user = mastodonUser self.user = mastodonUser
} }
.store(in: &disposeBag) .store(in: &disposeBag)
} }
} }

View File

@ -240,34 +240,3 @@ 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
}
}