damus

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

commit ff6b19578e5a8225c2856be146c042cc3bdec4e7
parent 0c63f2ee26af3d083648c6b725046684096c3dd2
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 28 Jan 2024 17:32:44 -0800

purple: switch local testing to staging testing

Diffstat:
Mdamus/Models/DamusState.swift | 2+-
Mdamus/Models/Purple/DamusPurple.swift | 14+++++++++-----
Mdamus/Models/UserSettingsStore.swift | 4++--
Mdamus/Nostr/NIP98AuthenticatedRequest.swift | 2+-
Mdamus/Nostr/NostrEvent.swift | 8+++++++-
Mdamus/Util/Constants.swift | 6+++---
Mdamus/Views/Purple/DamusPurpleView.swift | 2+-
Mdamus/Views/Settings/DeveloperSettingsView.swift | 2+-
8 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/damus/Models/DamusState.swift b/damus/Models/DamusState.swift @@ -63,7 +63,7 @@ class DamusState: HeadlessDamusState { self.video = video self.ndb = ndb self.purple = purple ?? DamusPurple( - environment: settings.purple_api_local_test_mode ? .local_test : .production, + settings: settings, keypair: keypair ) } diff --git a/damus/Models/Purple/DamusPurple.swift b/damus/Models/Purple/DamusPurple.swift @@ -8,12 +8,12 @@ import Foundation class DamusPurple: StoreObserverDelegate { - let environment: ServerEnvironment + let settings: UserSettingsStore let keypair: Keypair var starred_profiles_cache: [Pubkey: UserBadgeInfo] - init(environment: ServerEnvironment, keypair: Keypair) { - self.environment = environment + init(settings: UserSettingsStore, keypair: Keypair) { + self.settings = settings self.keypair = keypair self.starred_profiles_cache = [:] } @@ -23,6 +23,10 @@ class DamusPurple: StoreObserverDelegate { return await self.profile_purple_badge_info(pubkey: pubkey)?.active } + var environment: ServerEnvironment { + self.settings.purple_api_staging ? .staging : .production + } + func profile_purple_badge_info(pubkey: Pubkey) async -> UserBadgeInfo? { if let cached_result = self.starred_profiles_cache[pubkey] { return cached_result @@ -216,12 +220,12 @@ extension DamusPurple { extension DamusPurple { enum ServerEnvironment { - case local_test + case staging case production func get_base_url() -> URL { switch self { - case .local_test: + case .staging: Constants.PURPLE_API_TEST_BASE_URL case .production: Constants.PURPLE_API_PRODUCTION_BASE_URL diff --git a/damus/Models/UserSettingsStore.swift b/damus/Models/UserSettingsStore.swift @@ -205,8 +205,8 @@ class UserSettingsStore: ObservableObject { @Setting(key: "enable_experimental_purple_api", default_value: false) var enable_experimental_purple_api: Bool - @Setting(key: "purple_api_local_test_mode", default_value: false) - var purple_api_local_test_mode: Bool + @Setting(key: "purple_api_staging", default_value: false) + var purple_api_staging: Bool @Setting(key: "emoji_reactions", default_value: default_emoji_reactions) var emoji_reactions: [String] diff --git a/damus/Nostr/NIP98AuthenticatedRequest.swift b/damus/Nostr/NIP98AuthenticatedRequest.swift @@ -43,7 +43,7 @@ func make_nip98_authenticated_request(method: HTTPMethod, url: URL, payload: Dat createdAt: UInt32(Date().timeIntervalSince1970) ) - let auth_note_json_data: Data = try JSONEncoder().encode(auth_note) + let auth_note_json_data: Data = try encode_json_data(auth_note) let auth_note_base64: String = base64_encode(auth_note_json_data.bytes) request.setValue("Nostr " + auth_note_base64, forHTTPHeaderField: "Authorization") diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift @@ -325,7 +325,13 @@ func decode_nostr_event(txt: String) -> NostrResponse? { func encode_json<T: Encodable>(_ val: T) -> String? { let encoder = JSONEncoder() encoder.outputFormatting = .withoutEscapingSlashes - return (try? encoder.encode(val)).map { String(decoding: $0, as: UTF8.self) } + return (try? encode_json_data(val)).map { String(decoding: $0, as: UTF8.self) } +} + +func encode_json_data<T: Encodable>(_ val: T) throws -> Data { + let encoder = JSONEncoder() + encoder.outputFormatting = .withoutEscapingSlashes + return try encoder.encode(val) } func decode_nostr_event_json(json: String) -> NostrEvent? { diff --git a/damus/Util/Constants.swift b/damus/Util/Constants.swift @@ -14,8 +14,8 @@ class Constants { static let DEVICE_TOKEN_RECEIVER_TEST_URL: URL = URL(string: "http://localhost:8000/user-info")! static let MAIN_APP_BUNDLE_IDENTIFIER: String = "com.jb55.damus2" static let NOTIFICATION_EXTENSION_BUNDLE_IDENTIFIER: String = "com.jb55.damus2.DamusNotificationService" - static let PURPLE_API_PRODUCTION_BASE_URL: URL = URL(string: "https://purple.damus.io")! - static let PURPLE_API_TEST_BASE_URL: URL = URL(string: "http://127.0.0.1:8989")! - static let PURPLE_LANDING_PAGE_TEST_URL: URL = URL(string: "http://localhost:3000/purple")! + static let PURPLE_API_PRODUCTION_BASE_URL: URL = URL(string: "https://api.damus.io")! + static let PURPLE_API_TEST_BASE_URL: URL = URL(string: "https://api-staging.damus.io")! + static let PURPLE_LANDING_PAGE_TEST_URL: URL = URL(string: "https://staging.damus.io/purple")! static let PURPLE_LANDING_PAGE_PRODUCTION_URL: URL = URL(string: "https://damus.io/purple")! } diff --git a/damus/Views/Purple/DamusPurpleView.swift b/damus/Views/Purple/DamusPurpleView.swift @@ -412,7 +412,7 @@ struct DamusPurpleView: View { Spacer() Link( NSLocalizedString("Learn more", comment: "Label for a link to the Damus Purple landing page"), - destination: damus_state.settings.purple_api_local_test_mode ? Constants.PURPLE_LANDING_PAGE_TEST_URL : Constants.PURPLE_LANDING_PAGE_PRODUCTION_URL + destination: damus_state.settings.purple_api_staging ? Constants.PURPLE_LANDING_PAGE_TEST_URL : Constants.PURPLE_LANDING_PAGE_PRODUCTION_URL ) .foregroundColor(DamusColors.pink) .padding() diff --git a/damus/Views/Settings/DeveloperSettingsView.swift b/damus/Views/Settings/DeveloperSettingsView.swift @@ -28,7 +28,7 @@ struct DeveloperSettingsView: View { Toggle("Enable experimental Purple API support", isOn: $settings.enable_experimental_purple_api) .toggleStyle(.switch) - Toggle("Purple API localhost test mode", isOn: $settings.purple_api_local_test_mode) + Toggle("Purple API staging mode", isOn: $settings.purple_api_staging) .toggleStyle(.switch) } }