The personal finance app for everyone (by everyone)
Ruby
Render stored Brandfetch logos with the current client id (#5038)
O-B-ONE13526ba228c6
diff --git a/app/models/security.rb b/app/models/security.rbindex e6cb19239b..9ca84e9c04 100644--- a/app/models/security.rb+++ b/app/models/security.rb@@ -154,13 +154,15 @@ class Security < ApplicationRecord# - With no domain, a stored provider logo (e.g. T-Invest's CDN for MOEX# instruments) is authoritative and beats the ticker-only Brandfetch# lettermark placeholder.+ # A stored Brandfetch logo_url is rewritten to the current size and client+ # id, since holdings and the API render this value without transforming it.def display_logo_urlif crypto?- self.class.brandfetch_crypto_url(crypto_base_asset).presence || logo_url.presence+ self.class.brandfetch_crypto_url(crypto_base_asset).presence || stored_logo_urlelsif website_url.present?- brandfetch_icon_url.presence || logo_url.presence+ brandfetch_icon_url.presence || stored_logo_urlelse- logo_url.presence || brandfetch_icon_url.presence+ stored_logo_url || brandfetch_icon_url.presenceendend@@ -289,6 +291,10 @@ class Security < ApplicationRecordprivate+ def stored_logo_url+ Setting.transform_brand_fetch_url(logo_url).presence+ end+def extract_domain(url)uri = URI.parse(url)host = uri.host || urldiff --git a/app/models/setting.rb b/app/models/setting.rbindex c47812bf51..58d4dc11e0 100644--- a/app/models/setting.rb+++ b/app/models/setting.rb@@ -72,12 +72,19 @@ class Setting < RailsSettings::Basebrand_fetch_high_res_logos ? BRAND_FETCH_LOGO_SIZE_HIGH_RES : BRAND_FETCH_LOGO_SIZE_STANDARDend- # Transforms a stored Brandfetch URL to use the current logo size setting+ # Transforms a stored Brandfetch URL to use the current logo size and client+ # id. Stored URLs keep the client id they were generated with, so without+ # this a changed id in Self-Hosting settings would leave them broken.def self.transform_brand_fetch_url(url)return url unless url.present? && url.match?(BRAND_FETCH_URL_PATTERN)size = brand_fetch_logo_size- url.gsub(BRAND_FETCH_URL_PATTERN, "\\1w/#{size}/h/#{size}\\2")+ url = url.gsub(BRAND_FETCH_URL_PATTERN, "\\1w/#{size}/h/#{size}\\2")++ client_id = brand_fetch_client_id+ return url if client_id.blank?++ url.sub(/\?c=[^&]*/) { "?c=#{client_id}" }enddef self.brand_fetch_icon_url(identifier, fallback: "lettermark", namespace: nil, width: nil, height: nil)diff --git a/test/models/security_test.rb b/test/models/security_test.rbindex cca9d64bec..de641740d2 100644--- a/test/models/security_test.rb+++ b/test/models/security_test.rb@@ -354,6 +354,22 @@ class SecurityTest < ActiveSupport::TestCaseassert_equal "https://invest-brands.cdn-tinkoff.ru/SBERx160.png", sec.display_logo_urlend+ test "display_logo_url rewrites a stored Brandfetch logo to the current client id" do+ Setting.stubs(:brand_fetch_client_id).returns("new-client-id")+ Setting.stubs(:brand_fetch_logo_size).returns(120)++ sec = Security.new(+ ticker: "AAPL",+ exchange_operating_mic: "XNAS",+ logo_url: "https://cdn.brandfetch.io/AAPL/icon/fallback/lettermark/w/40/h/40?c=old-client-id"+ )++ assert_equal(+ "https://cdn.brandfetch.io/AAPL/icon/fallback/lettermark/w/120/h/120?c=new-client-id",+ sec.display_logo_url+ )+ end+test "before_save writes the /crypto/{base} URL to logo_url for new crypto securities" doSetting.stubs(:brand_fetch_client_id).returns("test-client-id")Setting.stubs(:brand_fetch_logo_size).returns(120)diff --git a/test/models/setting_test.rb b/test/models/setting_test.rbindex c329d8c903..238d87a029 100644--- a/test/models/setting_test.rb+++ b/test/models/setting_test.rb@@ -166,6 +166,35 @@ class SettingTest < ActiveSupport::TestCaseassert_nil Setting.brand_fetch_icon_url("example.com")end+ test "transform_brand_fetch_url renders a stored logo with the current client id and size" do+ Setting.stubs(:brand_fetch_client_id).returns("new-client-id")+ Setting.stubs(:brand_fetch_logo_size).returns(120)++ assert_equal(+ "https://cdn.brandfetch.io/walmart.com/icon/fallback/lettermark/w/120/h/120?c=new-client-id",+ Setting.transform_brand_fetch_url("https://cdn.brandfetch.io/walmart.com/icon/fallback/lettermark/w/40/h/40?c=old-client-id")+ )+ assert_equal(+ "https://cdn.brandfetch.io/crypto/BTC/icon/fallback/lettermark/w/120/h/120?c=new-client-id",+ Setting.transform_brand_fetch_url("https://cdn.brandfetch.io/crypto/BTC/icon/fallback/lettermark/w/40/h/40?c=old-client-id")+ )+ end++ test "transform_brand_fetch_url keeps the stored client id when none is configured" do+ Setting.stubs(:brand_fetch_client_id).returns(nil)+ Setting.stubs(:brand_fetch_logo_size).returns(40)++ stored = "https://cdn.brandfetch.io/walmart.com/icon/fallback/lettermark/w/40/h/40?c=old-client-id"+ assert_equal stored, Setting.transform_brand_fetch_url(stored)+ end++ test "transform_brand_fetch_url leaves other logo URLs alone" do+ Setting.stubs(:brand_fetch_client_id).returns("new-client-id")++ assert_equal "https://plaid.com/walmart.png?c=old-client-id", Setting.transform_brand_fetch_url("https://plaid.com/walmart.png?c=old-client-id")+ assert_nil Setting.transform_brand_fetch_url(nil)+ end+test "LLM_PROVIDER env wins over a stored llm_provider" dooriginal = Setting.raw_llm_providerSetting.llm_provider = "anthropic"