damus

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

commit 0c63f2ee26af3d083648c6b725046684096c3dd2
parent 500f8bc2ec39531afa8bf45f1199e26187e03624
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 28 Jan 2024 15:30:43 -0800

purple: add staging option to DamusPurpleURL

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/ContentView.swift | 2+-
Mdamus/Models/Purple/DamusPurpleURL.swift | 48+++++++++++++++++++++++++++++++-----------------
Mdamus/Views/Purple/DamusPurpleURLSheetView.swift | 4++--
MdamusTests/UrlTests.swift | 18++++++++++++++++++
4 files changed, 52 insertions(+), 20 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -1094,7 +1094,7 @@ enum OpenResult { } func on_open_url(state: DamusState, url: URL, result: @escaping (OpenResult?) -> Void) { - if let purple_url = DamusPurpleURL.from_url(url: url) { + if let purple_url = DamusPurpleURL(url: url) { result(.purple(purple_url)) return } diff --git a/damus/Models/Purple/DamusPurpleURL.swift b/damus/Models/Purple/DamusPurpleURL.swift @@ -7,39 +7,53 @@ import Foundation -enum DamusPurpleURL { - case verify_npub(checkout_id: String) - case welcome(checkout_id: String) - case landing - static func from_url(url: URL) -> DamusPurpleURL? { +struct DamusPurpleURL: Equatable { + let is_staging: Bool + let variant: Self.Variant + + enum Variant: Equatable { + case verify_npub(checkout_id: String) + case welcome(checkout_id: String) + case landing + } + + init(is_staging: Bool, variant: Self.Variant) { + self.is_staging = is_staging + self.variant = variant + } + + init?(url: URL) { guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return nil } guard components.scheme == "damus" else { return nil } + let is_staging = components.find("staging") != nil switch components.path { case "purple:verify": guard let checkout_id = components.find("id") else { return nil } - return .verify_npub(checkout_id: checkout_id) + self = .init(is_staging: is_staging, variant: .verify_npub(checkout_id: checkout_id)) case "purple:welcome": guard let checkout_id = components.find("id") else { return nil } - return .welcome(checkout_id: checkout_id) + self = .init(is_staging: is_staging, variant: .welcome(checkout_id: checkout_id)) case "purple:landing": - return .landing + self = .init(is_staging: is_staging, variant: .landing) default: return nil } } - + func url_string() -> String { - switch self { - case .verify_npub(let id): - return "damus:purple:verify?id=\(id)" - case .welcome(let id): - return "damus:purple:welcome?id=\(id)" - case .landing: - return "damus:purple:landing" + let staging = is_staging ? "&staging=true" : "" + switch self.variant { + case .verify_npub(let id): + return "damus:purple:verify?id=\(id)\(staging)" + case .welcome(let id): + return "damus:purple:welcome?id=\(id)\(staging)" + case .landing: + let staging = is_staging ? "?staging=true" : "" + return "damus:purple:landing\(staging)" } } - + } extension URLComponents { diff --git a/damus/Views/Purple/DamusPurpleURLSheetView.swift b/damus/Views/Purple/DamusPurpleURLSheetView.swift @@ -15,7 +15,7 @@ struct DamusPurpleURLSheetView: View { let purple_url: DamusPurpleURL var body: some View { - switch self.purple_url { + switch self.purple_url.variant { case .verify_npub(let checkout_id): DamusPurpleVerifyNpubView(damus_state: damus_state, checkout_id: checkout_id) case .welcome(_): @@ -28,7 +28,7 @@ struct DamusPurpleURLSheetView: View { struct DamusPurpleURLSheetView_Previews: PreviewProvider { static var previews: some View { - DamusPurpleURLSheetView(damus_state: test_damus_state, purple_url: .verify_npub(checkout_id: "123")) + DamusPurpleURLSheetView(damus_state: test_damus_state, purple_url: .init(is_staging: false, variant: .verify_npub(checkout_id: "123"))) } } diff --git a/damusTests/UrlTests.swift b/damusTests/UrlTests.swift @@ -18,6 +18,24 @@ final class UrlTests: XCTestCase { // Put teardown code here. This method is called after the invocation of each test method in the class. } + func testPurpleUrls() { + let landing_staging = DamusPurpleURL(is_staging: true, variant: .landing) + let welcome_staging = DamusPurpleURL(is_staging: true, variant: .welcome(checkout_id: "abc")) + let verify_staging = DamusPurpleURL(is_staging: true, variant: .verify_npub(checkout_id: "abc")) + + let landing = DamusPurpleURL(is_staging: false, variant: .landing) + let welcome = DamusPurpleURL(is_staging: false, variant: .welcome(checkout_id: "abc")) + let verify = DamusPurpleURL(is_staging: false, variant: .verify_npub(checkout_id: "abc")) + + XCTAssertEqual(landing_staging, .init(url: URL(string: landing_staging.url_string())!)!) + XCTAssertEqual(welcome_staging, .init(url: URL(string: welcome_staging.url_string())!)!) + XCTAssertEqual(verify_staging, .init(url: URL(string: verify_staging.url_string())!)!) + + XCTAssertEqual(landing, .init(url: URL(string: landing.url_string())!)!) + XCTAssertEqual(welcome, .init(url: URL(string: welcome.url_string())!)!) + XCTAssertEqual(verify, .init(url: URL(string: verify.url_string())!)!) + } + func testParseUrlTrailingParenthesis() { let testURL = URL(string: "https://en.m.wikipedia.org/wiki/Delicious_(website)") XCTAssertNotNil(testURL)