Browse Source

connect to relays stored in contacts

Signed-off-by: William Casarin <jb55@jb55.com>
profiles-everywhere
William Casarin 3 years ago
parent
commit
23bb7c7f18
  1. 4
      damus/ContentView.swift
  2. 5
      damus/Models/Contacts.swift
  3. 6
      damus/Models/FollowersModel.swift
  4. 21
      damus/Models/HomeModel.swift
  5. 4
      damus/Nostr/NostrEvent.swift

4
damus/ContentView.swift

@ -331,9 +331,9 @@ struct ContentView: View {
let pool = RelayPool() let pool = RelayPool()
add_relay(pool, "wss://relay.damus.io") 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, "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.freeberty.net")
//add_relay(pool, "wss://nostr-relay.untethr.me") //add_relay(pool, "wss://nostr-relay.untethr.me")

5
damus/Models/Contacts.swift

@ -133,6 +133,11 @@ func follow_user_event(our_contacts: NostrEvent?, our_pubkey: String, follow: Re
return ev return ev
} }
func decode_json_relays(_ content: String) -> [String: RelayInfo]? {
return decode_json(content)
}
/* /*
func ensure_relay_info(relays: [RelayDescriptor], content: String) -> [String: RelayInfo] { func ensure_relay_info(relays: [RelayDescriptor], content: String) -> [String: RelayInfo] {
guard let relay_info = decode_json_relays(content) else { guard let relay_info = decode_json_relays(content) else {

6
damus/Models/FollowersModel.swift

@ -43,7 +43,11 @@ class FollowersModel: ObservableObject {
if has_contact.contains(ev.pubkey) { if has_contact.contains(ev.pubkey) {
return 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) contacts.append(ev.pubkey)
has_contact.insert(ev.pubkey) has_contact.insert(ev.pubkey)
} }

21
damus/Models/HomeModel.swift

@ -69,7 +69,7 @@ class HomeModel: ObservableObject {
} }
func handle_contact_event(sub_id: String, relay_id: String, ev: NostrEvent) { 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 { if sub_id == init_subid {
pool.send(.unsubscribe(init_subid), to: [relay_id]) 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) { func load_our_contacts(contacts: Contacts, our_pubkey: String, ev: NostrEvent) {
if ev.pubkey != our_pubkey { guard ev.pubkey == our_pubkey else {
return return
} }
@ -393,7 +393,22 @@ func process_metadata_event(profiles: Profiles, ev: NostrEvent) {
notify(.profile_updated, ProfileUpdate(pubkey: ev.pubkey, profile: profile)) 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_contacts(contacts: contacts, our_pubkey: pubkey, ev: ev)
load_our_relays(pool: pool, ev: ev)
add_contact_if_friend(contacts: contacts, 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])
}
}
}
}

4
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) } 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? { func decode_data<T: Decodable>(_ data: Data) -> T? {
let decoder = JSONDecoder() let decoder = JSONDecoder()
do { do {

Loading…
Cancel
Save