damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 305ee03b0e92b31de533b5b862602f7ab150a364
parent a88f5db10b322056a8f86e71497c2e9379631c6f
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 21 Sep 2023 09:08:07 -0400

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?

Diffstat:
Mdamus/Views/Relays/RecommendedRelayView.swift | 4++--
Mdamus/Views/Relays/RelayPicView.swift | 26+++++++++++++++++++++-----
2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/damus/Views/Relays/RecommendedRelayView.swift b/damus/Views/Relays/RecommendedRelayView.swift @@ -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 { diff --git a/damus/Views/Relays/RelayPicView.swift b/damus/Views/Relays/RelayPicView.swift @@ -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)