Skip to content

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

let caching of avatar response base on setting

ulferts817e7918bc9c
diff --git a/lib/api/helpers/attachment_renderer.rb b/lib/api/helpers/attachment_renderer.rb
index 5cde04ca8cf..6b9b1d81517 100644
--- a/lib/api/helpers/attachment_renderer.rb
+++ b/lib/api/helpers/attachment_renderer.rb
@@ -53,8 +53,8 @@ module API
end
end
- def external_avatar_link_expires_in
- seconds = external_avatar_link_expiry_seconds
+ def avatar_link_expires_in
+ seconds = avatar_link_expiry_seconds
if seconds == 0
nil
@@ -63,8 +63,8 @@ module API
end
end
- 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_i
end
end
end
diff --git a/lib/open_project/configuration.rb b/lib/open_project/configuration.rb
index 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 returned
diff --git a/modules/avatars/lib/api/v3/users/user_avatar_api.rb b/modules/avatars/lib/api/v3/users/user_avatar_api.rb
index 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 API
def 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)
end
end
get '/avatar' do
set_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)
else
diff --git a/modules/avatars/spec/requests/user_avatar_api_spec.rb b/modules/avatars/spec/requests/user_avatar_api_spec.rb
index 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 do
include Rack::Test::Methods
include 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_user
end
describe '/avatar', with_settings: { protocol: 'http' } do
@@ -51,7 +52,7 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json do
setup
- get api_v3_paths.user(user.id) + "/avatar"
+ get api_v3_paths.user(other_user.id) + "/avatar"
end
context 'with neither enabled' do
@@ -63,6 +64,18 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json do
end
end
+ 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' do
let(:gravatars) { true }
let(:local_avatars) { false }
@@ -71,14 +84,16 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json do
expect(response.status).to eq 302
expect(response.location).to match /gravatar\.com/
end
+
+ it_behaves_like 'cache headers set to 24 hours'
end
context 'with local avatar' do
let(:gravatars) { true }
let(:local_avatars) { true }
- let(:user) do
- u = FactoryBot.create :admin
+ let(:other_user) do
+ u = FactoryBot.create :user
u.attachments = [FactoryBot.build(:avatar_attachment, author: u)]
u
end
@@ -87,6 +102,8 @@ describe 'API v3 User avatar resource', type: :request, content_type: :json do
expect(response.status).to eq 200
end
+ it_behaves_like 'cache headers set to 24 hours'
+
context 'with external file storage (S3)' do
let(:setup) do
allow_any_instance_of(Attachment).to receive(:external_storage?).and_return true