Fix not being able to resolve remote Mastodon posts

Closes #515
This commit is contained in:
Shadowfacts 2024-07-22 21:40:19 -07:00
parent 8b0c2f80b6
commit 90537f9d12
1 changed files with 16 additions and 5 deletions

View File

@ -221,20 +221,31 @@ class ConversationViewController: UIViewController {
completionHandler(nil)
}
}
if isLikelyMastodonRemoteStatus(url: url),
let (_, response) = try? await URLSession.appDefault.data(from: url, delegate: RedirectBlocker()),
if isLikelyMastodonRemoteStatus(url: url) {
var request = URLRequest(url: url)
// Mastodon uses an intermediate redirect page for browsers which requires user input that we don't want.
request.addValue("application/activity+json", forHTTPHeaderField: "accept")
if let (_, response) = try? await URLSession.appDefault.data(for: request, delegate: RedirectBlocker()),
let location = (response as? HTTPURLResponse)?.value(forHTTPHeaderField: "location") {
effectiveURL = location
} else {
effectiveURL = WebURL(url)!.serialized(excludingFragment: true)
}
} else {
effectiveURL = WebURL(url)!.serialized(excludingFragment: true)
}
let request = Client.search(query: effectiveURL, types: [.statuses], resolve: true)
do {
let (results, _) = try await mastodonController.run(request)
guard let status = results.statuses.compactMap(\.value).first(where: { $0.url?.serialized() == effectiveURL }) else {
let statuses = results.statuses.compactMap(\.value)
// Don't try to exactly match effective URL because the URL form Mastodon
// uses for the ActivityPub redirect doesn't match what's returned by the API.
// Instead we just assume that, if only one status was returned, it worked.
guard statuses.count == 1 else {
throw UnableToResolveError()
}
let status = statuses[0]
_ = mastodonController.persistentContainer.addOrUpdateOnViewContext(status: status)
mode = .localID(status.id)
return status.id