From 87d900b627126a70a6bae5acac371936e848a8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 19 Jun 2024 20:06:07 +0200 Subject: [PATCH] Add own relay to NIP-05 relay list if configured --- app/controllers/well_known_controller.rb | 14 +++++++++++--- spec/requests/well_known_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/controllers/well_known_controller.rb b/app/controllers/well_known_controller.rb index 02eb02c..5cf4398 100644 --- a/app/controllers/well_known_controller.rb +++ b/app/controllers/well_known_controller.rb @@ -5,11 +5,19 @@ class WellKnownController < ApplicationController @user = User.where(cn: params[:name], ou: domain).first http_status :not_found and return if @user.nil? || @user.nostr_pubkey.blank? + res = { + names: { "#{@user.cn}": @user.nostr_pubkey } + } + + if Setting.nostr_relay_url + res[:relays] = { + @user.nostr_pubkey => [ Setting.nostr_relay_url ] + } + end + respond_to do |format| format.json do - render json: { - names: { "#{@user.cn}": @user.nostr_pubkey } - }.to_json + render json: res.to_json end end end diff --git a/spec/requests/well_known_spec.rb b/spec/requests/well_known_spec.rb index 9862b79..eba66e8 100644 --- a/spec/requests/well_known_spec.rb +++ b/spec/requests/well_known_spec.rb @@ -45,6 +45,27 @@ RSpec.describe "Well-known URLs", type: :request do expect(res["names"].keys.size).to eq(1) expect(res["names"]["bobdylan"]).to eq(user.nostr_pubkey) end + + context "without relay configured" do + it "does not include a recommended relay" do + get "/.well-known/nostr.json?name=bobdylan" + res = JSON.parse(response.body) + expect(res["relays"]).to be_nil + end + end + + context "with relay configured" do + before do + Setting.nostr_relay_url = "wss://nostr.kosmos.org" + end + + it "includes a recommended relay" do + get "/.well-known/nostr.json?name=bobdylan" + res = JSON.parse(response.body) + expect(res["relays"][user.nostr_pubkey].length).to eq(1) + expect(res["relays"][user.nostr_pubkey].first).to eq("wss://nostr.kosmos.org") + end + end end end end