damus

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

commit 4cd48d31436ef5676026ee617094fbe325b4a011
parent 4da93b76309d3e0a847a9a60df54228e3593b95e
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 18 Jun 2022 08:20:20 -0700

wip some initial relay bootstrap logic

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

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 4++++
Mdamus/ContentView.swift | 20++++++++++++--------
Mdamus/Models/HomeModel.swift | 26+++++++++++++++++++++++++-
Adamus/Models/LocalUserConfig.swift | 14++++++++++++++
Mdamus/Nostr/Relay.swift | 2+-
Mdamus/Nostr/RelayPool.swift | 18+++++++++++++++++-
6 files changed, 73 insertions(+), 11 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -56,6 +56,7 @@ 4C5F9118283D88E40052CD1C /* FollowingModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C5F9117283D88E40052CD1C /* FollowingModel.swift */; }; 4C633350283D40E500B1C9C3 /* HomeModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C63334F283D40E500B1C9C3 /* HomeModel.swift */; }; 4C633352283D419F00B1C9C3 /* SignalModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C633351283D419F00B1C9C3 /* SignalModel.swift */; }; + 4C649844285A952100EAE2B3 /* LocalUserConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C649843285A952100EAE2B3 /* LocalUserConfig.swift */; }; 4C75EFA427FA577B0006080F /* PostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFA327FA577B0006080F /* PostView.swift */; }; 4C75EFA627FF87A20006080F /* Nostr.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFA527FF87A20006080F /* Nostr.swift */; }; 4C75EFAD28049CFB0006080F /* PostButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFAC28049CFB0006080F /* PostButton.swift */; }; @@ -166,6 +167,7 @@ 4C5F9117283D88E40052CD1C /* FollowingModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowingModel.swift; sourceTree = "<group>"; }; 4C63334F283D40E500B1C9C3 /* HomeModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeModel.swift; sourceTree = "<group>"; }; 4C633351283D419F00B1C9C3 /* SignalModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignalModel.swift; sourceTree = "<group>"; }; + 4C649843285A952100EAE2B3 /* LocalUserConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalUserConfig.swift; sourceTree = "<group>"; }; 4C75EFA327FA577B0006080F /* PostView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostView.swift; sourceTree = "<group>"; }; 4C75EFA527FF87A20006080F /* Nostr.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Nostr.swift; sourceTree = "<group>"; }; 4C75EFA72804823E0006080F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; }; @@ -265,6 +267,7 @@ 4C5F9117283D88E40052CD1C /* FollowingModel.swift */, 4C987B56283FD07F0042CE38 /* FollowersModel.swift */, 4C5C7E67284ED36500A22DF5 /* SearchHomeModel.swift */, + 4C649843285A952100EAE2B3 /* LocalUserConfig.swift */, ); path = Models; sourceTree = "<group>"; @@ -591,6 +594,7 @@ 4C75EFA627FF87A20006080F /* Nostr.swift in Sources */, 4CE4F9DE2852768D00C00DD9 /* ConfigView.swift in Sources */, 4C285C8E28399BFE008A31F1 /* SaveKeysView.swift in Sources */, + 4C649844285A952100EAE2B3 /* LocalUserConfig.swift in Sources */, 4C75EFB328049D640006080F /* NostrEvent.swift in Sources */, 4CA2EFA0280E37AC0044ACD8 /* TimelineView.swift in Sources */, 4C363A8428233689006E126D /* Parser.swift in Sources */, diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -8,6 +8,13 @@ import SwiftUI import Starscream +let BOOTSTRAP_RELAYS = [ + "wss://relay.damus.io", + "wss://nostr-pub.wellorder.net", + "wss://nostr-relay.freeberty.net", + "wss://nostr-relay.untethr.me", +] + struct TimestampedProfile { let profile: Profile let timestamp: Int64 @@ -327,14 +334,11 @@ struct ContentView: View { func connect() { let pool = RelayPool() - - add_relay(pool, "wss://relay.damus.io") - //add_relay(pool, "wss://nostr-pub.wellorder.net") - //add_relay(pool, "wss://nostr.bitcoiner.social") - //add_relay(pool, "ws://monad.jb55.com:8080") - //add_relay(pool, "wss://nostr-relay.freeberty.net") - //add_relay(pool, "wss://nostr-relay.untethr.me") - + + for relay in BOOTSTRAP_RELAYS { + add_relay(pool, relay) + } + pool.register_handler(sub_id: sub_id, handler: home.handle_event) self.damus_state = DamusState(pool: pool, keypair: keypair, diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -142,7 +142,7 @@ class HomeModel: ObservableObject { self.loading = true send_initial_filters(relay_id: relay_id) } else { - self.loading = true + //remove_bootstrap_nodes(damus_state) send_home_filters(relay_id: relay_id) } case .error(let merr): @@ -425,3 +425,27 @@ func load_our_relays(our_pubkey: String, pool: RelayPool, ev: NostrEvent) { } } } + + +func remove_bootstrap_nodes(_ damus_state: DamusState) { + guard let contacts = damus_state.contacts.event else { + return + } + + guard let relays = decode_json_relays(contacts.content) else { + return + } + + let descriptors = relays.reduce(into: []) { arr, kv in + guard let url = URL(string: kv.key) else { + return + } + arr.append(RelayDescriptor(url: url, info: kv.value)) + } + + for relay in BOOTSTRAP_RELAYS { + if !(descriptors.contains { ($0 as! RelayDescriptor).url.absoluteString == relay }) { + damus_state.pool.remove_relay(relay) + } + } +} diff --git a/damus/Models/LocalUserConfig.swift b/damus/Models/LocalUserConfig.swift @@ -0,0 +1,14 @@ +// +// LocalUserConfig.swift +// damus +// +// Created by William Casarin on 2022-06-15. +// + +import Foundation + + +struct LocalUserConfig: Codable { + let relays: [RelayDescriptor] +} + diff --git a/damus/Nostr/Relay.swift b/damus/Nostr/Relay.swift @@ -14,7 +14,7 @@ struct RelayInfo: Codable { static let rw = RelayInfo(read: true, write: true) } -struct RelayDescriptor { +struct RelayDescriptor: Codable { let url: URL let info: RelayInfo } diff --git a/damus/Nostr/RelayPool.swift b/damus/Nostr/RelayPool.swift @@ -45,9 +45,25 @@ class RelayPool { } func register_handler(sub_id: String, handler: @escaping (String, NostrConnectionEvent) -> ()) { + self.handlers.append(RelayHandler(sub_id: sub_id, callback: handler)) } - + + func remove_relay(_ relay_id: String) { + var i: Int = 0 + + self.disconnect(to: [relay_id]) + + for relay in relays { + if relay.id == relay_id { + relays.remove(at: i) + break + } + + i += 1 + } + } + func add_relay(_ url: URL, info: RelayInfo) throws { let relay_id = get_relay_id(url) if get_relay(relay_id) != nil {