Resolve Mastodon remote status links

Closes #384
This commit is contained in:
Shadowfacts 2023-05-15 22:01:44 -04:00
parent dbc89509d7
commit 126e8c8858
2 changed files with 24 additions and 3 deletions

View File

@ -11,6 +11,13 @@ import Pachyderm
import WebURL
import WebURLFoundationExtras
private let mastodonRemoteStatusRegex = try! NSRegularExpression(pattern: "^/@.+@.+/\\d{18}")
private func isLikelyMastodonRemoteStatus(url: URL) -> Bool {
let path = url.path
let range = NSRange(location: 0, length: path.utf16.count)
return mastodonRemoteStatusRegex.numberOfMatches(in: path, range: range) == 1
}
class ConversationViewController: UIViewController {
weak var mastodonController: MastodonController!
@ -210,11 +217,24 @@ class ConversationViewController: UIViewController {
indicator.startAnimating()
state = .loading(indicator)
let url = WebURL(url)!.serialized(excludingFragment: true)
let request = Client.search(query: url, types: [.statuses], resolve: true)
let effectiveURL: String
class RedirectBlocker: NSObject, URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
completionHandler(nil)
}
}
if isLikelyMastodonRemoteStatus(url: url),
let (_, response) = try? await URLSession.shared.data(from: url, delegate: RedirectBlocker()),
let location = (response as? HTTPURLResponse)?.value(forHTTPHeaderField: "location") {
effectiveURL = location
} 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.first(where: { $0.url?.serialized() == url }) else {
guard let status = results.statuses.first(where: { $0.url?.serialized() == effectiveURL }) else {
throw UnableToResolveError()
}
_ = mastodonController.persistentContainer.addOrUpdateOnViewContext(status: status)

View File

@ -205,6 +205,7 @@ enum PopoverSource {
private let statusPathRegex = try! NSRegularExpression(
pattern:
"(^/@[a-z0-9_]+/\\d{18})" // mastodon
+ "|(^/@.+@.+/\\d{18})" // mastodon remote
+ "|(^/notice/[a-z0-9]{18})" // pleroma
+ "|(^/notes/[a-z0-9]{10})" // misskey
+ "|(^/p/[a-z0-9_]+/\\d{18})" // pixelfed