When a profile has no usable picture, the account avatar is now cropped and uploaded to the configured Blossom server on save, and the returned URL is written to the profile, instead of publishing the account's own avatar URL. A failed upload fails the save.
101 lines
3.0 KiB
Ruby
101 lines
3.0 KiB
Ruby
# frozen_string_literal: true
|
||
|
||
module Settings
|
||
class NostrEditProfileComponent < ViewComponent::Base
|
||
include ApplicationHelper
|
||
|
||
attr_reader :user, :profile, :profile_event, :display_name, :nip05_state, :lud16_state,
|
||
:about, :website
|
||
|
||
def initialize(user:, profile_event: nil)
|
||
@user = user
|
||
@profile_event = profile_event
|
||
@profile = {}
|
||
@display_name = nil
|
||
|
||
if profile_event.present? && profile_event["content"].present?
|
||
@profile = JSON.parse(profile_event["content"]) rescue {}
|
||
end
|
||
|
||
@profile_display_name = NostrManagerService::DISPLAY_NAME_KEYS
|
||
.map { |key| @profile[key] }
|
||
.find(&:present?)
|
||
@display_name = @profile_display_name || @user.display_name
|
||
@display_name_added = @profile_display_name.blank? && @user.display_name.present?
|
||
|
||
@about = @profile["about"] if @profile["about"].is_a?(String)
|
||
@website = @profile["website"] if @profile["website"].is_a?(String)
|
||
|
||
@nip05_state = compute_field_state(@profile["nip05"])
|
||
@lud16_state = compute_field_state(@profile["lud16"])
|
||
end
|
||
|
||
def banner_url
|
||
safe_image_url(@profile["banner"])
|
||
end
|
||
|
||
def picture_url
|
||
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
|
||
|
||
def picture_value
|
||
picture_url.to_s
|
||
end
|
||
|
||
def banner_value
|
||
banner_url.to_s
|
||
end
|
||
|
||
def blossom_enabled?
|
||
BlossomManagerService.server_url.present?
|
||
end
|
||
|
||
def seed_picture_from_avatar?
|
||
blossom_enabled? && safe_image_url(@profile["picture"]).nil? && account_avatar_url.present?
|
||
end
|
||
|
||
def asset_descriptions
|
||
{
|
||
"picture" => "Square image, cropped to 400×400. JPEG, PNG, WebP or GIF up to " \
|
||
"#{helpers.number_to_human_size(BlossomManagerService.max_upload_bytes("picture"))}. " \
|
||
"Location data is stripped; animated GIFs keep their animation.",
|
||
"banner" => "Wide image, cropped to 1500×500. JPEG, PNG, WebP or GIF up to " \
|
||
"#{helpers.number_to_human_size(BlossomManagerService.max_upload_bytes("banner"))}. " \
|
||
"Location data is stripped; animated GIFs keep their animation."
|
||
}
|
||
end
|
||
|
||
private
|
||
|
||
def safe_image_url(value)
|
||
return nil unless value.is_a?(String)
|
||
return nil if value.blank? || value.length > 2048
|
||
|
||
uri = URI.parse(value)
|
||
return nil unless %w[http https].include?(uri.scheme)
|
||
|
||
value
|
||
rescue URI::InvalidURIError
|
||
nil
|
||
end
|
||
|
||
def compute_field_state(current_value)
|
||
target_value = @user.address
|
||
|
||
if current_value.blank?
|
||
{ status: :added, value: target_value, suggested: nil }
|
||
elsif current_value == target_value
|
||
{ status: :matched, value: current_value, suggested: nil }
|
||
else
|
||
{ status: :different, value: current_value, suggested: target_value }
|
||
end
|
||
end
|
||
end
|
||
end
|