61 lines
1.7 KiB
Ruby
61 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Settings
|
|
class NostrRelayStatusComponent < ViewComponent::Base
|
|
def initialize(nip65_event:, relay_url: Setting.nostr_relay_url)
|
|
@nip65_event = nip65_event
|
|
@relay_url = relay_url
|
|
@relay_host = relay_host(relay_url)
|
|
@statuses = []
|
|
|
|
if nip65_event.present?
|
|
@statuses.push({
|
|
text: "You have a relay list configured",
|
|
icon_name: "check-circle",
|
|
icon_color: "emerald-500"
|
|
})
|
|
|
|
if @relay_url.present?
|
|
if normalized_relay_urls(nip65_event).include?(NostrManagerService.normalize_url(@relay_url))
|
|
@statuses.push({
|
|
text: "The <strong>#{@relay_host}</strong> relay is part of your relay list",
|
|
icon_name: "check-circle",
|
|
icon_color: "emerald-500"
|
|
})
|
|
else
|
|
@statuses.push({
|
|
text: "<strong>#{@relay_host}</strong> is missing from your relay list",
|
|
icon_name: "alert-octagon",
|
|
icon_color: "amber-500"
|
|
})
|
|
end
|
|
end
|
|
else
|
|
@statuses.push({
|
|
text: "We could not find a relay list for your public key",
|
|
icon_name: "alert-octagon",
|
|
icon_color: "amber-500"
|
|
})
|
|
end
|
|
|
|
@button_text = (nip65_event.present? ? "Edit relays" : "Set up relays")
|
|
end
|
|
|
|
private
|
|
|
|
def relay_host(url)
|
|
URI.parse(url).host
|
|
rescue URI::InvalidURIError
|
|
nil
|
|
end
|
|
|
|
def relay_urls(nip65_event)
|
|
nip65_event["tags"].select { |t| t.is_a?(Array) && t[0] == "r" }.map { |t| t[1] }
|
|
end
|
|
|
|
def normalized_relay_urls(nip65_event)
|
|
relay_urls(nip65_event).map { |url| NostrManagerService.normalize_url(url) }
|
|
end
|
|
end
|
|
end
|