-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
a2b7dfe
commit 1607ee7
Showing
9 changed files
with
115 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// RelayURL.swift | ||
// damus | ||
// | ||
// Created by Daniel D’Aquino 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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters