1
0
mirror of git://jb55.com/damus synced 2024-09-16 02:03:45 +00:00

relays: fix tld extraction performance issues

This uses a simpler variant that doesn't require a library. It is also
much faster and doesn't cause a delay when you open the relay view.

Not sure why I needed to touch other parts of the code to make the build
work. Probably xcode beta thing?
This commit is contained in:
William Casarin 2023-09-21 09:08:07 -04:00
parent a88f5db10b
commit 305ee03b0e
2 changed files with 23 additions and 7 deletions

View File

@ -42,7 +42,7 @@ struct RecommendedRelayView: View {
VStack(alignment: .leading) {
HStack {
Text(meta?.name ?? relay.hostname ?? relay)
Text(meta?.name ?? relay)
.font(.headline)
.padding(.bottom, 2)
@ -82,7 +82,7 @@ struct RecommendedRelayView: View {
}
HStack {
Text(meta?.name ?? relay.hostname ?? relay)
Text(meta?.name ?? relay)
.lineLimit(1)
}
.contextMenu {

View File

@ -7,13 +7,12 @@
import SwiftUI
import Kingfisher
import TLDExtract
struct FailedRelayImage: View {
let url: URL?
var body: some View {
let abbrv = String(url?.hostname?.first?.uppercased() ?? "R")
let abbrv = String(url?.host()?.first?.uppercased() ?? "R")
Text("\(abbrv)")
.font(.system(size: 40, weight: .bold))
}
@ -90,11 +89,28 @@ struct RelayPicView: View {
}
}
func extract_tld(_ host: String) -> String {
let parts = host.split(separator: ".")
if parts.count >= 3 {
let last_3 = parts.suffix(3)
if parts[1] == "co" && parts[2] == "uk" {
return String(last_3.joined(separator: "."))
} else {
return String(parts.suffix(2).joined(separator: "."))
}
} else if parts.count == 2 {
return host
}
return host
}
func get_relay_url(relay: String, icon: String?) -> URL? {
let extractor = TLDExtract()
var favicon = relay + "/favicon.ico"
if let parseRelay: TLDResult = extractor.parse(relay) {
favicon = "https://" + (parseRelay.rootDomain ?? relay) + "/favicon.ico"
let tld = extract_tld(relay)
if tld != relay {
favicon = "https://" + tld + "/favicon.ico"
}
let pic = icon ?? favicon
return URL(string: pic)