OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
let caching of avatar response base on setting
ulferts817e7918bc9c
diff --git a/lib/api/helpers/attachment_renderer.rb b/lib/api/helpers/attachment_renderer.rbindex 5cde04ca8cf..6b9b1d81517 100644--- a/lib/api/helpers/attachment_renderer.rb+++ b/lib/api/helpers/attachment_renderer.rb@@ -53,8 +53,8 @@ module APIendend- def external_avatar_link_expires_in- seconds = external_avatar_link_expiry_seconds+ def avatar_link_expires_in+ seconds = avatar_link_expiry_secondsif seconds == 0nil@@ -63,8 +63,8 @@ module APIendend- def external_avatar_link_expiry_seconds- @external_avatar_link_expiry_seconds ||= OpenProject::Configuration.external_avatar_link_expiry_seconds.to_i+ def avatar_link_expiry_seconds+ @avatar_link_expiry_seconds ||= OpenProject::Configuration.avatar_link_expiry_seconds.to_iendendenddiff --git a/lib/open_project/configuration.rb b/lib/open_project/configuration.rbindex 36594d7ad57..cf97a7e0f30 100644--- a/lib/open_project/configuration.rb+++ b/lib/open_project/configuration.rb@@ -133,7 +133,7 @@ module OpenProject# Allow in-context translations to be loaded with CSP'crowdin_in_context_translations' => true,- 'external_avatar_link_expiry_seconds' => 24.hours.to_i,+ 'avatar_link_expiry_seconds' => 24.hours.to_i,# Default gravatar image, set to something other than 404# to ensure a default is returneddiff --git a/modules/avatars/lib/api/v3/users/user_avatar_api.rb b/modules/avatars/lib/api/v3/users/user_avatar_api.rbindex 6cc2f1f1000..b2c38c4ad7d 100644--- a/modules/avatars/lib/api/v3/users/user_avatar_api.rb+++ b/modules/avatars/lib/api/v3/users/user_avatar_api.rb@@ -38,18 +38,16 @@ module APIdef set_cache_headers!return if @user == current_user- # Cache for one day if not the current user- expire_in = 60 * 60 * 24- header "Cache-Control", "public, max-age=#{expire_in}"- header "Expires", CGI.rfc1123_date(Time.now.utc + expire_in)+ header "Cache-Control", "public, max-age=#{avatar_link_expiry_seconds}"+ header "Expires", CGI.rfc1123_date(Time.now.utc + avatar_link_expiry_seconds)endendget '/avatar' doset_cache_headers!- if local_avatar = local_avatar?(@user)- respond_with_attachment(local_avatar, external_link_expires_in: external_avatar_link_expires_in)+ if (local_avatar = local_avatar?(@user))+ respond_with_attachment(local_avatar, external_link_expires_in: avatar_link_expires_in)elsif avatar_manager.gravatar_enabled?redirect build_gravatar_image_url(@user)elsediff --git a/modules/avatars/spec/requests/user_avatar_api_spec.rb b/modules/avatars/spec/requests/user_avatar_api_spec.rbindex d74dda1f341..13772f7bae7 100644--- a/modules/avatars/spec/requests/user_avatar_api_spec.rb+++ b/modules/avatars/spec/requests/user_avatar_api_spec.rb@@ -33,14 +33,15 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json doinclude Rack::Test::Methodsinclude API::V3::Utilities::PathHelper- let(:user) { FactoryBot.create(:admin) }+ let(:current_user) { FactoryBot.create(:admin) }+ let(:other_user) { FactoryBot.create(:user) }subject(:response) { last_response }let(:setup) { nil }before do- login_as user+ login_as current_userenddescribe '/avatar', with_settings: { protocol: 'http' } do@@ -51,7 +52,7 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json dosetup- get api_v3_paths.user(user.id) + "/avatar"+ get api_v3_paths.user(other_user.id) + "/avatar"endcontext 'with neither enabled' do@@ -63,6 +64,18 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json doendend+ shared_examples_for 'cache headers set to 24 hours' do+ it 'has the cache headers set to 24 hours' do+ expect(response.headers["Cache-Control"]).to eq "public, max-age=86400"+ expect(response.headers["Expires"]).to be_present++ expires_time = Time.parse response.headers["Expires"]++ expect(expires_time < Time.now.utc + 86400).to be_truthy+ expect(expires_time > Time.now.utc + 86400 - 60).to be_truthy+ end+ end+context 'when gravatar enabled' dolet(:gravatars) { true }let(:local_avatars) { false }@@ -71,14 +84,16 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json doexpect(response.status).to eq 302expect(response.location).to match /gravatar\.com/end++ it_behaves_like 'cache headers set to 24 hours'endcontext 'with local avatar' dolet(:gravatars) { true }let(:local_avatars) { true }- let(:user) do- u = FactoryBot.create :admin+ let(:other_user) do+ u = FactoryBot.create :useru.attachments = [FactoryBot.build(:avatar_attachment, author: u)]uend@@ -87,6 +102,8 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json doexpect(response.status).to eq 200end+ it_behaves_like 'cache headers set to 24 hours'+context 'with external file storage (S3)' dolet(:setup) doallow_any_instance_of(Attachment).to receive(:external_storage?).and_return true