damus

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

commit 23bb7c7f18679e81177a6a9100d2c569189d8834
parent bbb0ee205f18279eda783f8b165ce226761feaf9
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  8 Jun 2022 13:09:46 -0700

connect to relays stored in contacts

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

Diffstat:
Mdamus/ContentView.swift | 4++--
Mdamus/Models/Contacts.swift | 5+++++
Mdamus/Models/FollowersModel.swift | 6+++++-
Mdamus/Models/HomeModel.swift | 21++++++++++++++++++---
Mdamus/Nostr/NostrEvent.swift | 4++++
5 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -331,9 +331,9 @@ struct ContentView: View { let pool = RelayPool() add_relay(pool, "wss://relay.damus.io") - add_relay(pool, "wss://nostr-pub.wellorder.net") + //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, "ws://monad.jb55.com:8080") //add_relay(pool, "wss://nostr-relay.freeberty.net") //add_relay(pool, "wss://nostr-relay.untethr.me") diff --git a/damus/Models/Contacts.swift b/damus/Models/Contacts.swift @@ -133,6 +133,11 @@ func follow_user_event(our_contacts: NostrEvent?, our_pubkey: String, follow: Re return ev } + +func decode_json_relays(_ content: String) -> [String: RelayInfo]? { + return decode_json(content) +} + /* func ensure_relay_info(relays: [RelayDescriptor], content: String) -> [String: RelayInfo] { guard let relay_info = decode_json_relays(content) else { diff --git a/damus/Models/FollowersModel.swift b/damus/Models/FollowersModel.swift @@ -43,7 +43,11 @@ class FollowersModel: ObservableObject { if has_contact.contains(ev.pubkey) { return } - process_contact_event(contacts: damus_state.contacts, pubkey: damus_state.pubkey, ev: ev) + process_contact_event( + pool: damus_state.pool, + contacts: damus_state.contacts, + pubkey: damus_state.pubkey, ev: ev + ) contacts.append(ev.pubkey) has_contact.insert(ev.pubkey) } diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -69,7 +69,7 @@ class HomeModel: ObservableObject { } func handle_contact_event(sub_id: String, relay_id: String, ev: NostrEvent) { - process_contact_event(contacts: damus_state.contacts, pubkey: damus_state.pubkey, ev: ev) + process_contact_event(pool: damus_state.pool, contacts: damus_state.contacts, pubkey: damus_state.pubkey, ev: ev) if sub_id == init_subid { pool.send(.unsubscribe(init_subid), to: [relay_id]) @@ -309,7 +309,7 @@ func add_contact_if_friend(contacts: Contacts, ev: NostrEvent) { } func load_our_contacts(contacts: Contacts, our_pubkey: String, ev: NostrEvent) { - if ev.pubkey != our_pubkey { + guard ev.pubkey == our_pubkey else { return } @@ -393,7 +393,22 @@ func process_metadata_event(profiles: Profiles, ev: NostrEvent) { notify(.profile_updated, ProfileUpdate(pubkey: ev.pubkey, profile: profile)) } -func process_contact_event(contacts: Contacts, pubkey: String, ev: NostrEvent) { +func process_contact_event(pool: RelayPool, contacts: Contacts, pubkey: String, ev: NostrEvent) { load_our_contacts(contacts: contacts, our_pubkey: pubkey, ev: ev) + load_our_relays(pool: pool, ev: ev) add_contact_if_friend(contacts: contacts, ev: ev) } + +func load_our_relays(pool: RelayPool, ev: NostrEvent) { + guard let decoded = decode_json_relays(ev.content) else { + return + } + + for key in decoded.keys { + if let url = URL(string: key) { + if let _ = try? pool.add_relay(url, info: decoded[key]!) { + pool.connect(to: [key]) + } + } + } +} diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift @@ -236,6 +236,10 @@ func encode_json<T: Encodable>(_ val: T) -> String? { return (try? encoder.encode(val)).map { String(decoding: $0, as: UTF8.self) } } +func decode_json<T: Decodable>(_ val: String) -> T? { + return try? JSONDecoder().decode(T.self, from: Data(val.utf8)) +} + func decode_data<T: Decodable>(_ data: Data) -> T? { let decoder = JSONDecoder() do {