connect to relays stored in contacts
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user