akkounts/app/services/nostr_manager/discover_user_profile.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

37 lines
896 B
Ruby

module NostrManager
class DiscoverUserProfile < NostrManagerService
MAX_EVENTS = 2
def initialize(pubkey:, relays: nil)
@pubkey = pubkey
@relays = relays.present? ? 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
profile_events.min_by { |e| e["created_at"] }
end
end
end