diff --git a/app/graphql/types/custom_emoji_type.rb b/app/graphql/types/custom_emoji_type.rb index 08ac3172f2caca398fcdf39772d89299bd580935..cf01b6326cc520977576e186ca0f6f8ddbcd1c30 100644 --- a/app/graphql/types/custom_emoji_type.rb +++ b/app/graphql/types/custom_emoji_type.rb @@ -21,7 +21,6 @@ class CustomEmojiType < BaseObject field :url, GraphQL::Types::String, null: false, - method: :file, description: 'Link to file of the emoji.' field :external, GraphQL::Types::Boolean, diff --git a/app/models/award_emoji.rb b/app/models/award_emoji.rb index 9c1005e19c7da868b874e7d77e64f1d8569c7d90..55ef95236d995fcdb1871540d48a9f9c99696691 100644 --- a/app/models/award_emoji.rb +++ b/app/models/award_emoji.rb @@ -67,7 +67,7 @@ def url return if TanukiEmoji.find_by_alpha_code(name) Groups::CustomEmojiFinder.new(resource_parent, { include_ancestor_groups: true }).execute - .by_name(name)&.select(:url)&.first&.url + .by_name(name)&.select(:file)&.first&.url end def expire_cache diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb index aea8e2881cb724511fa61ef5934df88a70b4be19..80dc6868d81df8fa4d65dd6e89cde7ecd1f46fd6 100644 --- a/app/models/custom_emoji.rb +++ b/app/models/custom_emoji.rb @@ -45,8 +45,6 @@ class CustomEmoji < ApplicationRecord .order(order) end - alias_attribute :url, :file # this might need a change in https://gitlab.com/gitlab-org/gitlab/-/issues/230467 - scope :for_resource, -> (resource) do return none if resource.nil? return none unless resource.is_a?(Group) @@ -54,6 +52,10 @@ class CustomEmoji < ApplicationRecord resource.custom_emoji end + def url + Gitlab::AssetProxy.proxy_url(file) + end + private def valid_emoji_name diff --git a/spec/graphql/mutations/custom_emoji/create_spec.rb b/spec/graphql/mutations/custom_emoji/create_spec.rb index 7c98e53a72cb74d4c9ae842a8e489ccdf05c96a4..36352354257c3a6dba724530f9394237fd12e235 100644 --- a/spec/graphql/mutations/custom_emoji/create_spec.rb +++ b/spec/graphql/mutations/custom_emoji/create_spec.rb @@ -6,7 +6,7 @@ let_it_be(:group) { create(:group) } let_it_be(:user) { create(:user) } - let(:args) { { group_path: group.full_path, name: 'tanuki', url: 'https://about.gitlab.com/images/press/logo/png/gitlab-icon-rgb.png' } } + let(:args) { { group_path: group.full_path, name: 'tanuki', file: 'https://about.gitlab.com/images/press/logo/png/gitlab-icon-rgb.png' } } before do group.add_developer(user) diff --git a/spec/lib/banzai/filter/custom_emoji_filter_spec.rb b/spec/lib/banzai/filter/custom_emoji_filter_spec.rb index 701a45aa54de84c643e35a8657efd51c45644b1a..440cbff2f821f6aaabe47d0d1b68b72bda03215b 100644 --- a/spec/lib/banzai/filter/custom_emoji_filter_spec.rb +++ b/spec/lib/banzai/filter/custom_emoji_filter_spec.rb @@ -63,4 +63,20 @@ expect(doc.css('gl-emoji').size).to eq 1 end + + context 'when asset proxy is configured' do + before do + stub_asset_proxy_setting( + enabled: true, + secret_key: 'shared-secret', + url: 'https://assets.example.com' + ) + end + + it 'uses the proxied url' do + doc = filter('<p>:tanuki:</p>') + + expect(doc.css('gl-emoji').first.attributes['data-fallback-src'].value).to start_with('https://assets.example.com') + end + end end diff --git a/spec/models/custom_emoji_spec.rb b/spec/models/custom_emoji_spec.rb index 52b36d980497b43d79c130100db908ff4e788482..22eb098bb8f4dee26ac3ea0a10669d54b78af95c 100644 --- a/spec/models/custom_emoji_spec.rb +++ b/spec/models/custom_emoji_spec.rb @@ -81,4 +81,20 @@ it { expect(described_class.for_namespaces([subgroup.id, group.id])).to eq([subgroup_emoji]) } end end + + describe '#url' do + before do + stub_asset_proxy_setting( + enabled: true, + secret_key: 'shared-secret', + url: 'https://assets.example.com' + ) + end + + it 'uses the asset proxy' do + emoji = build(:custom_emoji, name: 'gitlab', file: "http://example.com/test.png") + + expect(emoji.url).to eq("https://assets.example.com/08df250eeeef1a8cf2c761475ac74c5065105612/687474703a2f2f6578616d706c652e636f6d2f746573742e706e67") + end + end end diff --git a/spec/requests/api/graphql/custom_emoji_query_spec.rb b/spec/requests/api/graphql/custom_emoji_query_spec.rb index 7de5965f9534d2ca6cff465bd88fa31809e3b471..e09a71d1f903b35c59a4b8134a8037be4b54f37e 100644 --- a/spec/requests/api/graphql/custom_emoji_query_spec.rb +++ b/spec/requests/api/graphql/custom_emoji_query_spec.rb @@ -7,7 +7,7 @@ let_it_be(:current_user) { create(:user) } let_it_be(:group) { create(:group, :private) } - let_it_be(:custom_emoji) { create(:custom_emoji, group: group) } + let_it_be(:custom_emoji) { create(:custom_emoji, group: group, file: 'http://example.com/test.png') } before do group.add_developer(current_user) @@ -32,6 +32,7 @@ def custom_emoji_query(group) expect(response).to have_gitlab_http_status(:ok) expect(graphql_data['group']['customEmoji']['nodes'].count).to eq(1) expect(graphql_data['group']['customEmoji']['nodes'].first['name']).to eq(custom_emoji.name) + expect(graphql_data['group']['customEmoji']['nodes'].first['url']).to eq(custom_emoji.file) end it 'returns nil group when unauthorised' do @@ -40,5 +41,20 @@ def custom_emoji_query(group) expect(graphql_data['group']).to be_nil end + + context 'when asset proxy is configured' do + before do + stub_asset_proxy_setting( + enabled: true, + secret_key: 'shared-secret', + url: 'https://assets.example.com' + ) + end + + it 'uses the proxied url' do + post_graphql(custom_emoji_query(group), current_user: current_user) + expect(graphql_data['group']['customEmoji']['nodes'].first['url']).to start_with('https://assets.example.com') + end + end end end