purple: improve environment handling
This commit improves the handling of different Purple environments: - 3 environments were added (test, staging, production) to fulfill all testing needs - Damus website constants were also added (This will become important for the next few commits) - Toggle settings were replaced with a picker, where the user can select one of the 3 environments (test, staging, production) - Damus purple page and website address links can now be obtained directly from the DamusPurple struct, which improves flexibility and reduces complexity for code that consumes these constants. Signed-off-by: Daniel D’Aquino <daniel@daquino.me> Reviewed-by: William Casarin <jb55@jb55.com> Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
William Casarin
parent
992b0f2eba
commit
dd240899cf
@@ -23,8 +23,8 @@ class DamusPurple: StoreObserverDelegate {
|
||||
return await self.profile_purple_badge_info(pubkey: pubkey)?.active
|
||||
}
|
||||
|
||||
var environment: ServerEnvironment {
|
||||
self.settings.purple_api_staging ? .staging : .production
|
||||
var environment: DamusPurpleEnvironment {
|
||||
return self.settings.purple_enviroment
|
||||
}
|
||||
|
||||
func profile_purple_badge_info(pubkey: Pubkey) async -> UserBadgeInfo? {
|
||||
@@ -57,7 +57,7 @@ class DamusPurple: StoreObserverDelegate {
|
||||
}
|
||||
|
||||
func get_account_data(pubkey: Pubkey) async -> Data? {
|
||||
let url = environment.get_base_url().appendingPathComponent("accounts/\(pubkey.hex())")
|
||||
let url = environment.api_base_url().appendingPathComponent("accounts/\(pubkey.hex())")
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
|
||||
@@ -72,7 +72,7 @@ class DamusPurple: StoreObserverDelegate {
|
||||
}
|
||||
|
||||
func create_account(pubkey: Pubkey) async throws {
|
||||
let url = environment.get_base_url().appendingPathComponent("accounts")
|
||||
let url = environment.api_base_url().appendingPathComponent("accounts")
|
||||
|
||||
Log.info("Creating account with Damus Purple server", for: .damus_purple)
|
||||
|
||||
@@ -110,7 +110,7 @@ class DamusPurple: StoreObserverDelegate {
|
||||
|
||||
do {
|
||||
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
|
||||
let url = environment.get_base_url().appendingPathComponent("accounts/\(keypair.pubkey.hex())/app-store-receipt")
|
||||
let url = environment.api_base_url().appendingPathComponent("accounts/\(keypair.pubkey.hex())/app-store-receipt")
|
||||
|
||||
Log.info("Sending in-app purchase receipt to Damus Purple server", for: .damus_purple)
|
||||
|
||||
@@ -139,7 +139,7 @@ class DamusPurple: StoreObserverDelegate {
|
||||
}
|
||||
|
||||
func translate(text: String, source source_language: String, target target_language: String) async throws -> String {
|
||||
var url = environment.get_base_url()
|
||||
var url = environment.api_base_url()
|
||||
url.append(path: "/translate")
|
||||
url.append(queryItems: [
|
||||
.init(name: "source", value: source_language),
|
||||
@@ -169,7 +169,7 @@ class DamusPurple: StoreObserverDelegate {
|
||||
}
|
||||
|
||||
func verify_npub_for_checkout(checkout_id: String) async throws {
|
||||
var url = environment.get_base_url()
|
||||
var url = environment.api_base_url()
|
||||
url.append(path: "/ln-checkout/\(checkout_id)/verify")
|
||||
|
||||
let (data, response) = try await make_nip98_authenticated_request(
|
||||
@@ -219,20 +219,6 @@ extension DamusPurple {
|
||||
// MARK: Helper structures
|
||||
|
||||
extension DamusPurple {
|
||||
enum ServerEnvironment {
|
||||
case staging
|
||||
case production
|
||||
|
||||
func get_base_url() -> URL {
|
||||
switch self {
|
||||
case .staging:
|
||||
Constants.PURPLE_API_TEST_BASE_URL
|
||||
case .production:
|
||||
Constants.PURPLE_API_PRODUCTION_BASE_URL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum PurpleError: Error {
|
||||
case translation_error(status_code: Int, response: Data)
|
||||
case translation_no_response
|
||||
|
||||
69
damus/Models/Purple/DamusPurpleEnvironment.swift
Normal file
69
damus/Models/Purple/DamusPurpleEnvironment.swift
Normal file
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// DamusPurpleEnvironment.swift
|
||||
// damus
|
||||
//
|
||||
// Created by Daniel D’Aquino on 2024-01-29.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum DamusPurpleEnvironment: String, CaseIterable, Codable, Identifiable, StringCodable, Equatable {
|
||||
case local_test
|
||||
case staging
|
||||
case production
|
||||
|
||||
func text_description() -> String {
|
||||
switch self {
|
||||
case .local_test:
|
||||
return NSLocalizedString("Test (localhost)", comment: "Label indicating a localhost test environment for Damus Purple functionality (Developer feature)")
|
||||
case .staging:
|
||||
return NSLocalizedString("Staging", comment: "Label indicating a staging test environment for Damus Purple functionality (Developer feature)")
|
||||
case .production:
|
||||
return NSLocalizedString("Production", comment: "Label indicating the production environment for Damus Purple")
|
||||
}
|
||||
}
|
||||
|
||||
func api_base_url() -> URL {
|
||||
switch self {
|
||||
case .local_test:
|
||||
Constants.PURPLE_API_LOCAL_TEST_BASE_URL
|
||||
case .staging:
|
||||
Constants.PURPLE_API_STAGING_BASE_URL
|
||||
case .production:
|
||||
Constants.PURPLE_API_PRODUCTION_BASE_URL
|
||||
}
|
||||
}
|
||||
|
||||
func purple_landing_page_url() -> URL {
|
||||
switch self {
|
||||
case .local_test:
|
||||
Constants.PURPLE_LANDING_PAGE_LOCAL_TEST_URL
|
||||
case .staging:
|
||||
Constants.PURPLE_LANDING_PAGE_STAGING_URL
|
||||
case .production:
|
||||
Constants.PURPLE_LANDING_PAGE_PRODUCTION_URL
|
||||
}
|
||||
}
|
||||
|
||||
func damus_website_url() -> URL {
|
||||
switch self {
|
||||
case .local_test:
|
||||
Constants.DAMUS_WEBSITE_LOCAL_TEST_URL
|
||||
case .staging:
|
||||
Constants.DAMUS_WEBSITE_STAGING_URL
|
||||
case .production:
|
||||
Constants.DAMUS_WEBSITE_PRODUCTION_URL
|
||||
}
|
||||
}
|
||||
|
||||
init?(from string: String) {
|
||||
guard let initialized = Self.init(rawValue: string) else { return nil }
|
||||
self = initialized
|
||||
}
|
||||
|
||||
func to_string() -> String {
|
||||
return self.rawValue
|
||||
}
|
||||
|
||||
var id: String { self.rawValue }
|
||||
}
|
||||
@@ -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_staging", default_value: false)
|
||||
var purple_api_staging: Bool
|
||||
@StringSetting(key: "purple_environment", default_value: .production)
|
||||
var purple_enviroment: DamusPurpleEnvironment
|
||||
|
||||
@Setting(key: "emoji_reactions", default_value: default_emoji_reactions)
|
||||
var emoji_reactions: [String]
|
||||
|
||||
Reference in New Issue
Block a user