akkounts/app/services/nostr_manager/discover_user_relays.rb
Râu Cao 04a9061663
All checks were successful
continuous-integration/drone/push Build is passing
WIP Render nostr profile and relay status with Ruby
2024-10-18 15:29:43 +02:00

38 lines
874 B
Ruby

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