module NostrManager class DiscoverUserRelays < NostrManagerService MAX_EVENTS = 2 def initialize(pubkey:) @pubkey = pubkey @relays = Setting.nostr_discovery_relays end def call received_events = 0 nip65_events = [] user_relays = [] filter = Nostr::Filter.new( authors: [@pubkey], kinds: [10002], limit: 1, ) @relays.each do |url| event = NostrManager::FetchLatestEvent.call(filter: filter, relay_url: url) if event.present? nip65_events << event if nip65_events.none? { |e| e["id"] == event["id"] } received_events += 1 end if received_events >= MAX_EVENTS puts "Found #{MAX_EVENTS} events, ending the search" break end end nip65_events.min_by { |e| e["created_at"] } end end end