module NostrManager class DiscoverUserProfile < NostrManagerService MAX_EVENTS = 3 def initialize(pubkey:) @pubkey = pubkey @relays = Setting.nostr_discovery_relays end def call received_events = 0 profile_events = [] filter = Nostr::Filter.new( authors: [@pubkey], kinds: [0], limit: 1, ) @relays.each do |url| event = NostrManager::FetchLatestEvent.call(filter: filter, relay_url: url) if event.present? profile_events << event if profile_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 latest_event = profile_events.min_by { |e| e["created_at"] } puts latest_event.inspect end end end