Reuse the account avatar as the Nostr profile picture fallback
When the published profile has no usable `picture` (missing, blank, or not an http(s) URL), the editor previews the account avatar and writes its public URL into the profile on save. An existing valid picture is left untouched. The component exposes the account avatar URL to the Stimulus controller, which applies it in `content()` only when `picture` is absent or not a URL, so the preview and the published content agree.
This commit is contained in:
5 files changed
+80
-6
No files matched your search
@@ -23,7 +23,8 @@
|
||||
action: "submit->nostr-profile-editor#save",
|
||||
:'nostr-profile-editor-profile-value' => @profile.to_json,
|
||||
:'nostr-profile-editor-handle-value' => @user.cn,
|
||||
:'nostr-profile-editor-display-name-keys-value' => NostrManagerService::DISPLAY_NAME_KEYS.to_json
|
||||
:'nostr-profile-editor-display-name-keys-value' => NostrManagerService::DISPLAY_NAME_KEYS.to_json,
|
||||
:'nostr-profile-editor-account-avatar-url-value' => account_avatar_url.to_s
|
||||
}
|
||||
}) do |f| %>
|
||||
<div class="grid grid-cols-1 gap-y-6 md:grid-cols-2 md:gap-x-8">
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
module Settings
|
||||
class NostrEditProfileComponent < ViewComponent::Base
|
||||
include ApplicationHelper
|
||||
|
||||
attr_reader :user, :profile, :profile_event, :display_name, :nip05_state, :lud16_state,
|
||||
:about, :website
|
||||
|
||||
@@ -31,7 +33,13 @@ module Settings
|
||||
end
|
||||
|
||||
def picture_url
|
||||
safe_image_url(@profile["picture"])
|
||||
safe_image_url(@profile["picture"]) || account_avatar_url
|
||||
end
|
||||
|
||||
def account_avatar_url
|
||||
return nil unless @user.avatar.attached?
|
||||
|
||||
safe_image_url(image_url_for(@user.avatar))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -10,7 +10,8 @@ export default class extends Controller {
|
||||
static values = {
|
||||
profile: { type: Object, default: {} },
|
||||
handle: String,
|
||||
displayNameKeys: { type: Array, default: [ "display_name" ] }
|
||||
displayNameKeys: { type: Array, default: [ "display_name" ] },
|
||||
accountAvatarUrl: { type: String, default: "" }
|
||||
}
|
||||
|
||||
async save (event) {
|
||||
@@ -68,6 +69,10 @@ export default class extends Controller {
|
||||
this.assign(content, "about", this.field("about"))
|
||||
this.assign(content, "website", this.field("website"))
|
||||
|
||||
if ((!content.picture || !URL_PATTERN.test(content.picture)) && this.accountAvatarUrlValue) {
|
||||
content.picture = this.accountAvatarUrlValue
|
||||
}
|
||||
|
||||
if (!content.name && this.handleValue) {
|
||||
content.name = this.handleValue
|
||||
}
|
||||
|
||||
+12
-3
@@ -65,9 +65,11 @@ it to your local strfry for example:
|
||||
The account settings page lets users publish their own Nostr data:
|
||||
|
||||
* **Profile** (kind `0`) — display name, about, website, NIP-05 address and Lightning address.
|
||||
Edited fields are merged into the existing profile JSON, so unknown keys (`picture`, `banner`, …)
|
||||
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.
|
||||
Edited fields are merged into the existing profile JSON, so other keys (`banner`, …) 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. When the
|
||||
published profile has no usable `picture` (missing, blank, or not an http(s) URL), the account
|
||||
avatar's public URL is used instead and written to the profile on save.
|
||||
* **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.
|
||||
@@ -95,5 +97,12 @@ Relevant settings (admin → Services → Nostr):
|
||||
entries cannot be saved unchanged.
|
||||
* Metadata discovery is sequential (relays → profile → media server list) and can take a while on
|
||||
slow relays.
|
||||
* The account-avatar fallback for `picture` carves out an exception to preserving existing values:
|
||||
a `picture` that is missing, blank, or not an http(s) URL is replaced by the account avatar URL.
|
||||
* That fallback is one-shot and can go stale: the published URL embeds the avatar's content hash, so
|
||||
it starts 404ing once the account avatar is replaced, and — because the published `picture` is
|
||||
then no longer blank — the fallback does not re-trigger.
|
||||
* The fallback URL's host is taken from the request at save time, so saving from an alternate
|
||||
hostname publishes that hostname.
|
||||
|
||||
[1]: https://github.com/hoytech/strfry
|
||||
@@ -148,6 +148,57 @@ RSpec.describe Settings::NostrEditProfileComponent, type: :component do
|
||||
end
|
||||
end
|
||||
|
||||
describe "account avatar fallback" do
|
||||
context "with an account avatar" do
|
||||
let(:user) { create(:user, cn: "alice") }
|
||||
let(:fixture_path) { Rails.root.join("spec/fixtures/files/taipei.jpg") }
|
||||
let(:img_hash) { Digest::SHA256.hexdigest(File.read(fixture_path)) }
|
||||
let(:avatar_src) { %r{users/alice/avatars/#{img_hash}\.jpg} }
|
||||
|
||||
before do
|
||||
ActiveStorage::Blob.create_and_upload!(
|
||||
io: File.open(fixture_path),
|
||||
filename: "#{img_hash}.jpg",
|
||||
content_type: "image/jpeg"
|
||||
).tap { |blob| user.avatar.attach(blob) }
|
||||
end
|
||||
|
||||
it "previews the account avatar and passes it to the editor when the profile has no picture" do
|
||||
rendered = render_inline(described_class.new(user: user, profile_event: nil))
|
||||
|
||||
expect(rendered.css("img").first["src"]).to match(avatar_src)
|
||||
|
||||
form = rendered.css("form").first
|
||||
expect(form["data-nostr-profile-editor-account-avatar-url-value"]).to match(avatar_src)
|
||||
end
|
||||
|
||||
it "prefers the profile's picture over the account avatar" do
|
||||
profile_event = { "content" => { "picture" => "https://example.com/p.png" }.to_json }
|
||||
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
|
||||
|
||||
expect(rendered.css("img").first["src"]).to eq("https://example.com/p.png")
|
||||
end
|
||||
|
||||
it "treats a non-URL picture as absent" do
|
||||
profile_event = { "content" => { "picture" => "ftp://example.com/x.png" }.to_json }
|
||||
rendered = render_inline(described_class.new(user: user, profile_event: profile_event))
|
||||
|
||||
expect(rendered.css("img").first["src"]).to match(avatar_src)
|
||||
end
|
||||
end
|
||||
|
||||
context "without an account avatar" do
|
||||
it "renders the placeholder and passes no avatar URL" do
|
||||
rendered = render_inline(described_class.new(user: user, profile_event: nil))
|
||||
|
||||
expect(rendered.css("img")).to be_empty
|
||||
|
||||
form = rendered.css("form").first
|
||||
expect(form["data-nostr-profile-editor-account-avatar-url-value"]).to eq("")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "when profile event is missing or fields are empty" do
|
||||
it "prefills NIP-05 and Lightning addresses with user address and marks them as added" do
|
||||
rendered = render_inline(described_class.new(user: user, profile_event: nil))
|
||||
|
||||
Reference in new issue
Block a user