Merge pull request 'Handle all kind 0 display-name variants in the Nostr profile editor' (#270) from feature/nostr-profile-display-name-variants into master
CI / Test (push) Successful in 47s
CI / Test (push) Successful in 47s
Reviewed-on: #270
This commit was merged in pull request #270.
This commit is contained in:
6 files changed
+70
-6
No files matched your search
@@ -22,7 +22,8 @@
|
||||
controller: "nostr-profile-editor",
|
||||
action: "submit->nostr-profile-editor#save",
|
||||
:'nostr-profile-editor-profile-value' => @profile.to_json,
|
||||
:'nostr-profile-editor-handle-value' => @user.cn
|
||||
:'nostr-profile-editor-handle-value' => @user.cn,
|
||||
:'nostr-profile-editor-display-name-keys-value' => NostrManagerService::DISPLAY_NAME_KEYS.to_json
|
||||
}
|
||||
}) do |f| %>
|
||||
<%= render FormElements::FieldsetComponent.new(tag: "div", title: "Display name") do %>
|
||||
|
||||
@@ -12,10 +12,11 @@ module Settings
|
||||
|
||||
if profile_event.present? && profile_event["content"].present?
|
||||
@profile = JSON.parse(profile_event["content"]) rescue {}
|
||||
@display_name = @profile["display_name"] || @profile["displayName"]
|
||||
end
|
||||
|
||||
@display_name ||= @user.display_name
|
||||
@display_name = NostrManagerService::DISPLAY_NAME_KEYS
|
||||
.map { |key| @profile[key] }
|
||||
.find(&:present?) || @user.display_name
|
||||
|
||||
@nip05_state = compute_field_state(@profile["nip05"])
|
||||
@lud16_state = compute_field_state(@profile["lud16"])
|
||||
|
||||
@@ -6,7 +6,11 @@ const ADDRESS_PATTERN = /^[^@\s]+@[^@\s]+\.[^@\s]+$/
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = [ "error", "submit" ]
|
||||
static values = { profile: { type: Object, default: {} }, handle: String }
|
||||
static values = {
|
||||
profile: { type: Object, default: {} },
|
||||
handle: String,
|
||||
displayNameKeys: { type: Array, default: [ "display_name" ] }
|
||||
}
|
||||
|
||||
async save (event) {
|
||||
event.preventDefault()
|
||||
@@ -52,8 +56,7 @@ export default class extends Controller {
|
||||
content (displayName, nip05, lud16) {
|
||||
const content = { ...(this.profileValue || {}) }
|
||||
|
||||
this.assign(content, "display_name", displayName)
|
||||
delete content.displayName
|
||||
this.displayNameKeysValue.forEach((key) => this.assign(content, key, displayName))
|
||||
this.assign(content, "nip05", nip05)
|
||||
this.assign(content, "lud16", lud16)
|
||||
|
||||
|
||||
@@ -5,6 +5,10 @@ class NostrManagerService < ApplicationService
|
||||
RELAY_LIST_KIND = 10002
|
||||
MEDIA_SERVER_LIST_KIND = 10063
|
||||
|
||||
# Priority order for reading a profile display name. `display_name`/`displayName` are the
|
||||
# display forms, `name`/`username` the basic and deprecated ones (NIP-01/NIP-24).
|
||||
DISPLAY_NAME_KEYS = %w[display_name displayName name username].freeze
|
||||
|
||||
METADATA_CACHE_SUFFIXES = %w[nip65 write_relays profile blossom].freeze
|
||||
|
||||
MAX_RELAY_LIST_SIZE = 10
|
||||
|
||||
@@ -66,6 +66,8 @@ The account settings page lets users publish their own Nostr data:
|
||||
|
||||
* **Profile** (kind `0`) — display name, NIP-05 address and Lightning address. Edited fields are
|
||||
merged into the existing profile JSON, so unknown keys (`about`, `picture`, …) are preserved.
|
||||
The display name is read from the first present of `display_name`, `displayName`, `name` or
|
||||
`username`, and written to all of them for maximum client compatibility.
|
||||
* **Relay list** (NIP-65, kind `10002`) — the site relay is pre-included; entries can be marked
|
||||
read-only or write-only.
|
||||
* **Media server list** (Blossom, kind `10063`) — the configured Blossom server is pre-included.
|
||||
|
||||
@@ -23,6 +23,59 @@ RSpec.describe Settings::NostrEditProfileComponent, type: :component do
|
||||
expect(rendered.css("[data-nostr-profile-editor-target='submit']")).to be_present
|
||||
end
|
||||
|
||||
it "passes the display-name key list to the editor" do
|
||||
rendered = render_inline(described_class.new(user: user, profile_event: nil))
|
||||
|
||||
form = rendered.css("form").first
|
||||
keys = JSON.parse(form["data-nostr-profile-editor-display-name-keys-value"])
|
||||
expect(keys).to eq(%w[display_name displayName name username])
|
||||
end
|
||||
|
||||
describe "display name resolution" do
|
||||
def rendered_display_name(profile_event)
|
||||
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
|
||||
rendered.css("input[name='user[display_name]']").first["value"]
|
||||
end
|
||||
|
||||
def profile_event(content)
|
||||
{ "content" => content.to_json }
|
||||
end
|
||||
|
||||
it "reads display_name" do
|
||||
expect(rendered_display_name(profile_event("display_name" => "Alice D"))).to eq("Alice D")
|
||||
end
|
||||
|
||||
it "falls back to displayName" do
|
||||
expect(rendered_display_name(profile_event("displayName" => "Alice Camel"))).to eq("Alice Camel")
|
||||
end
|
||||
|
||||
it "falls back to name" do
|
||||
expect(rendered_display_name(profile_event("name" => "alice"))).to eq("alice")
|
||||
end
|
||||
|
||||
it "falls back to username" do
|
||||
expect(rendered_display_name(profile_event("username" => "alice_legacy"))).to eq("alice_legacy")
|
||||
end
|
||||
|
||||
it "prefers display_name over name" do
|
||||
profile = profile_event("display_name" => "Alice D", "name" => "alice")
|
||||
expect(rendered_display_name(profile)).to eq("Alice D")
|
||||
end
|
||||
|
||||
it "skips blank values" do
|
||||
profile = profile_event("display_name" => "", "name" => "alice")
|
||||
expect(rendered_display_name(profile)).to eq("alice")
|
||||
end
|
||||
|
||||
it "falls back to the account display name" do
|
||||
expect(rendered_display_name(profile_event("about" => "hi"))).to eq("Alice Wonderland")
|
||||
end
|
||||
|
||||
it "uses the account display name without a profile event" do
|
||||
expect(rendered_display_name(nil)).to eq("Alice Wonderland")
|
||||
end
|
||||
end
|
||||
|
||||
it "passes the existing profile to the editor so unknown keys survive" do
|
||||
profile_event = {
|
||||
"content" => { "about" => "hi", "nip05" => "alice@kosmos.org" }.to_json
|
||||
|
||||
Reference in new issue
Block a user