ui: Fix issue where relays with trailing slashes cannot be removed (#1531)

Summary
-------

This fixes the issue at Github #1531 where relays with trailing slashes cannot be removed.

The root cause (Identified by @fishcakeday) was that for a relay to be removed, a certain dictionary entry containing the relay url needed to be removed prior to sending the updated relay list. However those dictionary keys used `String` objects, which cannot tell that two URLs are the same with or without a trailing slash.

To fix the issue, I have used a dictionary with the type `[RelayURL: RelayInfo]`, and made the necessary protocol conformance implementations for RelayURL. This way, URLs are handled with higher accuracy (e.g. Trailing slashes do not matter, URLs that resolve to the same location will match no matter what).

This allows us to leverage the existing parsing and handling logic that comes with the `URL` type, instead of manually handling URL strings.

Generally speaking it is preferrable to work with higher level `URL` or `RelayURL` objects than handle URLs via `String`. There is an opportunity to refactor more code, but I intentionally kept the changes to `RelayURL` limited to the functionality in this issue, because otherwise the changeset becomes very big and risky.

Issue reproduction
------------------

**Device:** iPhone 14 Pro simulator
**iOS:** 17.0
**Damus:** Local build from `476f52562` with the following local change:
``` diff

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Daniel D’Aquino
2023-09-30 02:04:04 +00:00
committed by William Casarin
parent 66d731ad0a
commit 768ab3e9e4
9 changed files with 115 additions and 46 deletions

View File

@@ -13,37 +13,6 @@ enum NostrConnectionEvent {
case nostr_event(NostrResponse)
}
public struct RelayURL: Hashable {
private(set) var url: URL
var id: String {
return url.absoluteString
}
init?(_ str: String) {
guard let last = str.last else { return nil }
var urlstr = str
if last == "/" {
urlstr = String(str.dropLast(1))
}
guard let url = URL(string: urlstr) else {
return nil
}
guard let scheme = url.scheme else {
return nil
}
guard scheme == "ws" || scheme == "wss" else {
return nil
}
self.url = url
}
}
final class RelayConnection: ObservableObject {
@Published private(set) var isConnected = false
@Published private(set) var isConnecting = false

View File

@@ -0,0 +1,87 @@
//
// RelayURL.swift
// damus
//
// Created by Daniel DAquino on 2023-09-29.
//
import Foundation
public struct RelayURL: Hashable, Equatable, Codable, CodingKeyRepresentable {
private(set) var url: URL
var id: String {
return url.absoluteString
}
init?(_ str: String) {
guard let last = str.last else { return nil }
var urlstr = str
if last == "/" {
urlstr = String(str.dropLast(1))
}
guard let url = URL(string: urlstr) else {
return nil
}
guard let scheme = url.scheme else {
return nil
}
guard scheme == "ws" || scheme == "wss" else {
return nil
}
self.url = url
}
// MARK: - Codable
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let urlString = try container.decode(String.self)
guard let instance = RelayURL(urlString) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid URL string.")
}
self = instance
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(url.absoluteString)
}
// MARK: - CodingKeyRepresentable
// CodingKeyRepresentable conformance is necessary to ensure that
// a dictionary with type "[RelayURL: T] where T: Codable" can be encoded into a keyed container
// e.g. `{<URL>: <VALUE>, <URL>: <VALUE>}` instead of `[<URL>, <VALUE>, <URL>, <VALUE>]`, which is Swift's default for non-string-keyed dictionaries
public var codingKey: CodingKey {
return StringKey(stringValue: self.url.absoluteString)
}
public init?<T>(codingKey: T) where T : CodingKey {
self.init(codingKey.stringValue)
}
// MARK: - Equatable
public static func == (lhs: RelayURL, rhs: RelayURL) -> Bool {
return lhs.url == rhs.url
}
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(self.url)
}
}
private struct StringKey: CodingKey {
var stringValue: String
init(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int? { return nil }
init?(intValue: Int) { return nil }
}