diff --git a/.app_version b/.app_version index 74aaa3f3..ce62dc55 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.16.8 +0.16.9 diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d29ae67..2491962e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# 0.16.9 - 2024-11-24 + +### Changed + +- Rate limit for the Photon API is now 1 request per second. If you host your own Photon API instance, reverse geocoding requests will not be limited. +- Requests to the Photon API are now have User-Agent header set to "Dawarich #{APP_VERSION} (https://dawarich.app)" + # 0.16.8 - 2024-11-20 ### Changed diff --git a/Gemfile b/Gemfile index a1a27f8c..c096f50e 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,7 @@ gem 'rswag-ui' gem 'shrine', '~> 3.6' gem 'sidekiq' gem 'sidekiq-cron' +gem 'sidekiq-limit_fetch' gem 'sprockets-rails' gem 'stimulus-rails' gem 'tailwindcss-rails' diff --git a/Gemfile.lock b/Gemfile.lock index e9d78b74..5e910553 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -370,6 +370,8 @@ GEM fugit (~> 1.8, >= 1.11.1) globalid (>= 1.0.1) sidekiq (>= 6.5.0) + sidekiq-limit_fetch (4.4.1) + sidekiq (>= 6) simplecov (0.22.0) docile (~> 1.1) simplecov-html (~> 0.11) @@ -465,6 +467,7 @@ DEPENDENCIES shrine (~> 3.6) sidekiq sidekiq-cron + sidekiq-limit_fetch simplecov sprockets-rails stimulus-rails diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0b55457b..a5be07f5 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -69,10 +69,6 @@ module ApplicationHelper CheckAppVersion.new.call end - def app_version - File.read('.app_version').strip - end - def app_theme current_user&.theme == 'light' ? 'light' : 'dark' end diff --git a/app/jobs/reverse_geocoding_job.rb b/app/jobs/reverse_geocoding_job.rb index ec57b4e7..dc49d2a2 100644 --- a/app/jobs/reverse_geocoding_job.rb +++ b/app/jobs/reverse_geocoding_job.rb @@ -6,6 +6,8 @@ class ReverseGeocodingJob < ApplicationJob def perform(klass, id) return unless REVERSE_GEOCODING_ENABLED + rate_limit_for_photon_api + data_fetcher(klass, id).call end @@ -14,4 +16,10 @@ class ReverseGeocodingJob < ApplicationJob def data_fetcher(klass, id) "ReverseGeocoding::#{klass.pluralize.camelize}::FetchData".constantize.new(id) end + + def rate_limit_for_photon_api + return unless PHOTON_API_HOST == 'photon.komoot.io' + + sleep 1 if PHOTON_API_HOST == 'photon.komoot.io' + end end diff --git a/app/services/check_app_version.rb b/app/services/check_app_version.rb index 6dc59932..3ae5ed76 100644 --- a/app/services/check_app_version.rb +++ b/app/services/check_app_version.rb @@ -5,11 +5,10 @@ class CheckAppVersion def initialize @repo_url = 'https://api.github.com/repos/Freika/dawarich/tags' - @app_version = File.read('.app_version').strip end def call - latest_version != @app_version + latest_version != APP_VERSION rescue StandardError false end diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index e24c176a..6ba5ead7 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -19,10 +19,10 @@ <% if new_version_available? %> - + <% else %> - + <% end %> diff --git a/config/initializers/01_constants.rb b/config/initializers/01_constants.rb index d1ce75e1..5065345f 100644 --- a/config/initializers/01_constants.rb +++ b/config/initializers/01_constants.rb @@ -5,3 +5,4 @@ REVERSE_GEOCODING_ENABLED = ENV.fetch('REVERSE_GEOCODING_ENABLED', 'true') == 't PHOTON_API_HOST = ENV.fetch('PHOTON_API_HOST', nil) PHOTON_API_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'true') == 'true' DISTANCE_UNIT = ENV.fetch('DISTANCE_UNIT', 'km').to_sym +APP_VERSION = File.read('.app_version').strip diff --git a/config/initializers/geocoder.rb b/config/initializers/geocoder.rb index 724a0531..d873c8ea 100644 --- a/config/initializers/geocoder.rb +++ b/config/initializers/geocoder.rb @@ -6,6 +6,7 @@ settings = { cache: Redis.new, always_raise: :all, use_https: PHOTON_API_USE_HTTPS, + http_headers: { 'User-Agent' => "Dawarich #{APP_VERSION} (https://dawarich.app)" }, cache_options: { expiration: 1.day } diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 02c0f5ad..f242910b 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -24,3 +24,5 @@ end Sidekiq.configure_client do |config| config.redis = { url: ENV['REDIS_URL'] } end + +Sidekiq::Queue['reverse_geocoding'].limit = 1 if PHOTON_API_HOST == 'photon.komoot.io' diff --git a/spec/services/check_app_version_spec.rb b/spec/services/check_app_version_spec.rb index 58f002f4..b58cc2e5 100644 --- a/spec/services/check_app_version_spec.rb +++ b/spec/services/check_app_version_spec.rb @@ -6,27 +6,25 @@ RSpec.describe CheckAppVersion do describe '#call' do subject(:check_app_version) { described_class.new.call } - let(:app_version) { File.read('.app_version').strip } - before do stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags') .to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {}) + + stub_const('APP_VERSION', '1.0.0') end context 'when latest version is newer' do - before { allow(File).to receive(:read).with('.app_version').and_return('0.9.0') } + before { stub_const('APP_VERSION', '0.9.0') } it { is_expected.to be true } end context 'when latest version is the same' do - before { allow(File).to receive(:read).with('.app_version').and_return('1.0.0') } - it { is_expected.to be false } end context 'when latest version is older' do - before { allow(File).to receive(:read).with('.app_version').and_return('1.1.0') } + before { stub_const('APP_VERSION', '1.1.0') } it { is_expected.to be true } end @@ -34,7 +32,7 @@ RSpec.describe CheckAppVersion do context 'when request fails' do before do allow(Net::HTTP).to receive(:get).and_raise(StandardError) - allow(File).to receive(:read).with('.app_version').and_return(app_version) + allow(File).to receive(:read).with('.app_version').and_return(APP_VERSION) end it { is_expected.to be false }