2023-03-18 13:35:02 +07:00
|
|
|
class WellKnownController < ApplicationController
|
2024-06-19 20:57:22 +02:00
|
|
|
before_action :require_nostr_enabled, only: [ :nostr ]
|
2024-09-10 16:06:11 +02:00
|
|
|
before_action :allow_cross_origin_requests, only: [ :nostr ]
|
|
|
|
|
|
|
|
|
|
layout false
|
2024-06-19 20:57:22 +02:00
|
|
|
|
2023-03-18 13:35:02 +07:00
|
|
|
def nostr
|
|
|
|
|
http_status :unprocessable_entity and return if params[:name].blank?
|
|
|
|
|
domain = request.headers["X-Forwarded-Host"].presence || Setting.primary_domain
|
2024-08-14 13:36:15 +02:00
|
|
|
relay_url = Setting.nostr_relay_url.presence
|
2024-06-19 20:57:22 +02:00
|
|
|
|
|
|
|
|
if params[:name] == "_"
|
2024-09-11 16:28:12 +02:00
|
|
|
if domain == Setting.primary_domain
|
|
|
|
|
# pubkey for the primary domain without a username (e.g. kosmos.org)
|
2024-09-14 16:40:22 +02:00
|
|
|
res = { names: { "_": Setting.nostr_public_key_primary_domain.presence || Setting.nostr_public_key } }
|
2024-09-11 16:28:12 +02:00
|
|
|
else
|
|
|
|
|
# pubkey for the akkounts domain without a username (e.g. accounts.kosmos.org)
|
|
|
|
|
res = { names: { "_": Setting.nostr_public_key } }
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-20 14:50:02 +02:00
|
|
|
res[:relays] = { "_" => [ relay_url ] } if relay_url
|
2024-06-19 20:57:22 +02:00
|
|
|
else
|
|
|
|
|
@user = User.where(cn: params[:name], ou: domain).first
|
|
|
|
|
http_status :not_found and return if @user.nil? || @user.nostr_pubkey.blank?
|
2023-03-18 13:35:02 +07:00
|
|
|
|
2024-06-19 20:57:22 +02:00
|
|
|
res = { names: { @user.cn => @user.nostr_pubkey } }
|
2024-06-20 14:50:02 +02:00
|
|
|
res[:relays] = { @user.nostr_pubkey => [ relay_url ] } if relay_url
|
2024-06-19 20:06:07 +02:00
|
|
|
end
|
|
|
|
|
|
2023-03-18 13:35:02 +07:00
|
|
|
respond_to do |format|
|
|
|
|
|
format.json do
|
2024-06-19 20:06:07 +02:00
|
|
|
render json: res.to_json
|
2023-03-18 13:35:02 +07:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-06-19 20:57:22 +02:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def require_nostr_enabled
|
|
|
|
|
http_status :not_found unless Setting.nostr_enabled?
|
|
|
|
|
end
|
2024-09-10 16:06:11 +02:00
|
|
|
|
|
|
|
|
def allow_cross_origin_requests
|
|
|
|
|
headers['Access-Control-Allow-Origin'] = "*"
|
|
|
|
|
headers['Access-Control-Allow-Methods'] = "GET"
|
|
|
|
|
end
|
2023-03-18 13:35:02 +07:00
|
|
|
end
|