forked from shadowfacts/Tusker
parent
dbc89509d7
commit
126e8c8858
|
@ -11,6 +11,13 @@ import Pachyderm
|
||||||
import WebURL
|
import WebURL
|
||||||
import WebURLFoundationExtras
|
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 {
|
class ConversationViewController: UIViewController {
|
||||||
|
|
||||||
weak var mastodonController: MastodonController!
|
weak var mastodonController: MastodonController!
|
||||||
|
@ -210,11 +217,24 @@ class ConversationViewController: UIViewController {
|
||||||
indicator.startAnimating()
|
indicator.startAnimating()
|
||||||
state = .loading(indicator)
|
state = .loading(indicator)
|
||||||
|
|
||||||
let url = WebURL(url)!.serialized(excludingFragment: true)
|
let effectiveURL: String
|
||||||
let request = Client.search(query: url, types: [.statuses], resolve: true)
|
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 {
|
do {
|
||||||
let (results, _) = try await mastodonController.run(request)
|
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()
|
throw UnableToResolveError()
|
||||||
}
|
}
|
||||||
_ = mastodonController.persistentContainer.addOrUpdateOnViewContext(status: status)
|
_ = mastodonController.persistentContainer.addOrUpdateOnViewContext(status: status)
|
||||||
|
|
|
@ -205,6 +205,7 @@ enum PopoverSource {
|
||||||
private let statusPathRegex = try! NSRegularExpression(
|
private let statusPathRegex = try! NSRegularExpression(
|
||||||
pattern:
|
pattern:
|
||||||
"(^/@[a-z0-9_]+/\\d{18})" // mastodon
|
"(^/@[a-z0-9_]+/\\d{18})" // mastodon
|
||||||
|
+ "|(^/@.+@.+/\\d{18})" // mastodon remote
|
||||||
+ "|(^/notice/[a-z0-9]{18})" // pleroma
|
+ "|(^/notice/[a-z0-9]{18})" // pleroma
|
||||||
+ "|(^/notes/[a-z0-9]{10})" // misskey
|
+ "|(^/notes/[a-z0-9]{10})" // misskey
|
||||||
+ "|(^/p/[a-z0-9_]+/\\d{18})" // pixelfed
|
+ "|(^/p/[a-z0-9_]+/\\d{18})" // pixelfed
|
||||||
|
|
Loading…
Reference in New Issue