damus

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

commit 5f1545b86abfbcc618f57290454bcb7acf1d2f2d
parent fe444228e6036391154f9fb9456e0a3d1a1497b9
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 10 Feb 2023 08:27:28 -0800

Paid relay detection

Diffstat:
Mdamus/ContentView.swift | 7++++---
Mdamus/Models/HomeModel.swift | 10++++++++--
Mdamus/Nostr/Relay.swift | 13+++++++++++++
Mdamus/Util/Relays/RelayFilters.swift | 22++++++++++++++++++----
Mdamus/Views/Relays/RelayDetailView.swift | 2+-
5 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -589,12 +589,13 @@ struct ContentView: View { func connect() { let pool = RelayPool() let metadatas = RelayMetadatas() + let relay_filters = RelayFilters(our_pubkey: pubkey) + let new_relay_filters = load_relay_filters(pubkey) == nil for relay in BOOTSTRAP_RELAYS { if let url = URL(string: relay) { - add_new_relay(url: url, info: .rw, metadatas: metadatas, pool: pool) + add_new_relay(relay_filters: relay_filters, metadatas: metadatas, pool: pool, url: url, info: .rw, new_relay_filters: new_relay_filters) } - add_relay(pool, relay) } pool.register_handler(sub_id: sub_id, handler: home.handle_event) @@ -610,7 +611,7 @@ struct ContentView: View { zaps: Zaps(our_pubkey: pubkey), lnurls: LNUrls(), settings: UserSettingsStore(), - relay_filters: RelayFilters(our_pubkey: pubkey), + relay_filters: relay_filters, relay_metadata: metadatas ) home.damus_state = self.damus_state! diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -691,11 +691,12 @@ func load_our_relays(state: DamusState, m_old_ev: NostrEvent?, ev: NostrEvent) { let diff = old.symmetricDifference(new) + let new_relay_filters = load_relay_filters(state.pubkey) == nil for d in diff { changed = true if new.contains(d) { if let url = URL(string: d) { - add_new_relay(url: url, info: decoded[d] ?? .rw, metadatas: state.relay_metadata, pool: state.pool) + add_new_relay(relay_filters: state.relay_filters, metadatas: state.relay_metadata, pool: state.pool, url: url, info: decoded[d] ?? .rw, new_relay_filters: new_relay_filters) } } else { state.pool.remove_relay(d) @@ -707,7 +708,7 @@ func load_our_relays(state: DamusState, m_old_ev: NostrEvent?, ev: NostrEvent) { } } -func add_new_relay(url: URL, info: RelayInfo, metadatas: RelayMetadatas, pool: RelayPool) { +func add_new_relay(relay_filters: RelayFilters, metadatas: RelayMetadatas, pool: RelayPool, url: URL, info: RelayInfo, new_relay_filters: Bool) { try? pool.add_relay(url, info: info) let relay_id = url.absoluteString @@ -722,6 +723,11 @@ func add_new_relay(url: URL, info: RelayInfo, metadatas: RelayMetadatas, pool: R DispatchQueue.main.async { metadatas.insert(relay_id: relay_id, metadata: meta) + + // if this is the first time adding filters, we should filter non-paid relays + if new_relay_filters && !meta.is_paid { + relay_filters.insert(timeline: .search, relay_id: relay_id) + } } } } diff --git a/damus/Nostr/Relay.swift b/damus/Nostr/Relay.swift @@ -24,6 +24,14 @@ enum RelayFlags: Int { case broken = 1 } +struct Limitations: Codable { + let payment_required: Bool? + + static var empty: Limitations { + Limitations(payment_required: nil) + } +} + struct RelayMetadata: Codable { let name: String? let description: String? @@ -32,6 +40,11 @@ struct RelayMetadata: Codable { let supported_nips: [Int]? let software: String? let version: String? + let limitation: Limitations? + + var is_paid: Bool { + return limitation?.payment_required ?? false + } } class Relay: Identifiable { diff --git a/damus/Util/Relays/RelayFilters.swift b/damus/Util/Relays/RelayFilters.swift @@ -10,6 +10,11 @@ import Foundation struct RelayFilter: Hashable { let timeline: Timeline let relay_id: String + + init(timeline: Timeline, relay_id: String, on: Bool = false) { + self.timeline = timeline + self.relay_id = relay_id + } } class RelayFilters { @@ -44,7 +49,7 @@ class RelayFilters { init(our_pubkey: String) { self.our_pubkey = our_pubkey - disabled = load_relay_filters(our_pubkey) + disabled = load_relay_filters(our_pubkey) ?? Set() } } @@ -54,10 +59,19 @@ func save_relay_filters(_ pubkey: String, filters: Set<RelayFilter>) { UserDefaults.standard.set(arr, forKey: key) } -func load_relay_filters(_ pubkey: String) -> Set<RelayFilter> { - let key = pk_setting_key(pubkey, key: "relay_filters") +func relay_filter_setting_key(_ pubkey: String) -> String { + return pk_setting_key(pubkey, key: "relay_filters") +} + +func clear_relay_filters(_ pubkey: String) { + let key = relay_filter_setting_key(pubkey) + UserDefaults.standard.removeObject(forKey: key) +} + +func load_relay_filters(_ pubkey: String) -> Set<RelayFilter>? { + let key = relay_filter_setting_key(pubkey) guard let filters = UserDefaults.standard.stringArray(forKey: key) else { - return Set() + return nil } return filters.reduce(into: Set()) { s, str in diff --git a/damus/Views/Relays/RelayDetailView.swift b/damus/Views/Relays/RelayDetailView.swift @@ -91,7 +91,7 @@ struct RelayDetailView: View { struct RelayDetailView_Previews: PreviewProvider { static var previews: some View { - let metadata = RelayMetadata(name: "name", description: "desc", pubkey: "pubkey", contact: "contact", supported_nips: [1,2,3], software: "software", version: "version") + let metadata = RelayMetadata(name: "name", description: "desc", pubkey: "pubkey", contact: "contact", supported_nips: [1,2,3], software: "software", version: "version", limitation: Limitations.empty) RelayDetailView(state: test_damus_state(), relay: "relay", nip11: metadata, conn_color: .green) } }