From d2bb013db7d2a1d1161b524a4a5c35b0c7fa94e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=E2=80=99Aquino?= Date: Mon, 30 Oct 2023 22:58:56 +0000 Subject: [PATCH] relays: automatically load extra regional bootstrap relays MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Depending on user's locale. currently only supported for Japanese users. This change allows Japanese users to automatically connect with popular Japanese regional relays during account creation, thus allowing Japanese users to better connect with the Japanese Nostr community. More specifically, 3 Japanese regional relays will be automatically added to the user's relay list (on top of the usual relays) under the following conditions: 1. User's region (As configured in iOS settings) is Japan, AND 2. The user is creating a new Nostr account through Damus. In the case the Nostr account is not new, Damus will not add any regional relays (It will respect the user's relay list). Testing ------- PASS Device: iPhone 15 Pro (Simulator) iOS: 17.0.1 Damus: This commit Test steps: 1. With the US region set, install Damus 2. Go through onboarding and create a new account. 3. Once the onboarding is complete, look at the connected relays. Should only be connected with popular international relays. PASS 4. Uninstall Damus 5. On iOS settings, go to Location & Region settings and change the region to Japan. 6. Install Damus 7. Go through onboarding and create a new account. 8. Once the onboarding is complete, look at the connected relays. User should be connected with intl relays as well as Japanese relays. PASS 9. Quit Damus and restart 10. Ensure the Japanese relays are still on the list. PASS 11. Quit Damus 12. Change region back to US 13. Restart Damus and check the relay list. Relay list should not be affected. PASS 14. Reinstall Damus 15. Check relay list. Only intl relays should be shown. PASS 16. Change region to Japan (JP) 17. Restart Damus and check the relay list. Relay list should not be affected. PASS 18. Reinstall Damus 19. This time, login with a pre-existing account (One that is not connected to Japanese relays). 20. After onboarding, check relay list. The relay list should be unaffected (i.e. Japanese relays should not have been added since this account is pre-existing). PASS Note: The actual network connection with some of the Japanese relays failed, but that is likely due to the Geo-based IP restrictions imposed by some of those Japanese relays. The relay list has been copied verbatim from @mattn's suggestions. Reference ticket: https://github.com/damus-io/damus/issues/1447 Changelog-Changed: Automatically load extra regional Japanese relays during account creation if user's region is set to Japan. Signed-off-by: Daniel D’Aquino Signed-off-by: William Casarin --- damus/Util/Relays/RelayBootstrap.swift | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/damus/Util/Relays/RelayBootstrap.swift b/damus/Util/Relays/RelayBootstrap.swift index 344f3679f..712ef7d2c 100644 --- a/damus/Util/Relays/RelayBootstrap.swift +++ b/damus/Util/Relays/RelayBootstrap.swift @@ -14,6 +14,14 @@ let BOOTSTRAP_RELAYS = [ "wss://nos.lol", ] +let REGION_SPECIFIC_BOOTSTRAP_RELAYS: [Locale.Region: [String]] = [ + Locale.Region.japan: [ + "wss://relay-jp.nostr.wirednet.jp", + "wss://yabu.me", + "wss://r.kojira.io", + ] +] + func bootstrap_relays_setting_key(pubkey: Pubkey) -> String { return pk_setting_key(pubkey, key: "bootstrap_relays") } @@ -29,16 +37,25 @@ func load_bootstrap_relays(pubkey: Pubkey) -> [String] { guard let relays = UserDefaults.standard.stringArray(forKey: key) else { print("loading default bootstrap relays") - return BOOTSTRAP_RELAYS.map { $0 } + return get_default_bootstrap_relays().map { $0 } } if relays.count == 0 { print("loading default bootstrap relays") - return BOOTSTRAP_RELAYS.map { $0 } + return get_default_bootstrap_relays().map { $0 } } - let loaded_relays = Array(Set(relays + BOOTSTRAP_RELAYS)) + let loaded_relays = Array(Set(relays + get_default_bootstrap_relays())) print("Loading custom bootstrap relays: \(loaded_relays)") return loaded_relays } +func get_default_bootstrap_relays() -> [String] { + var default_bootstrap_relays = BOOTSTRAP_RELAYS + + if let user_region = Locale.current.region, let regional_bootstrap_relays = REGION_SPECIFIC_BOOTSTRAP_RELAYS[user_region] { + default_bootstrap_relays.append(contentsOf: regional_bootstrap_relays) + } + + return default_bootstrap_relays +}