From 8637671d03f2ecf4901ba7dd8fa624a8d9c4487b Mon Sep 17 00:00:00 2001
From: Phil Hughes <me@iamphill.com>
Date: Mon, 8 Apr 2024 11:07:09 +0100
Subject: [PATCH] Update custom emoji to use the asset proxy

https://gitlab.com/gitlab-org/gitlab/-/issues/451485
---
 app/graphql/types/custom_emoji_type.rb         |  1 -
 app/models/award_emoji.rb                      |  2 +-
 app/models/custom_emoji.rb                     |  6 ++++--
 .../mutations/custom_emoji/create_spec.rb      |  2 +-
 .../banzai/filter/custom_emoji_filter_spec.rb  | 16 ++++++++++++++++
 spec/models/custom_emoji_spec.rb               | 16 ++++++++++++++++
 .../api/graphql/custom_emoji_query_spec.rb     | 18 +++++++++++++++++-
 7 files changed, 55 insertions(+), 6 deletions(-)

diff --git a/app/graphql/types/custom_emoji_type.rb b/app/graphql/types/custom_emoji_type.rb
index 08ac3172f2cac..cf01b6326cc52 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 9c1005e19c7da..55ef95236d995 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 aea8e2881cb72..80dc6868d81df 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 7c98e53a72cb7..36352354257c3 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 701a45aa54de8..440cbff2f821f 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 52b36d980497b..22eb098bb8f4d 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 7de5965f9534d..e09a71d1f903b 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
-- 
GitLab