diff --git a/app/jobs/scheduled/purge_old_web_hook_events.rb b/app/jobs/scheduled/purge_old_web_hook_events.rb new file mode 100644 index 00000000000..04e21d09b14 --- /dev/null +++ b/app/jobs/scheduled/purge_old_web_hook_events.rb @@ -0,0 +1,9 @@ +module Jobs + class PurgeOldWebHookEvents < Jobs::Scheduled + every 1.week + + def execute(_) + WebHookEvent.purge_old + end + end +end diff --git a/app/models/web_hook_event.rb b/app/models/web_hook_event.rb index ce2c134bc12..204efba8aaf 100644 --- a/app/models/web_hook_event.rb +++ b/app/models/web_hook_event.rb @@ -5,6 +5,12 @@ class WebHookEvent < ActiveRecord::Base default_scope { order('created_at DESC') } + def self.purge_old + where( + 'created_at < ?', SiteSetting.retain_web_hook_events_period_days.days.ago + ).delete_all + end + def update_web_hook_delivery_status web_hook.last_delivery_status = case status diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 6e2a7c6d1dd..8e9025d6a6a 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3485,6 +3485,7 @@ en: developer: 'Developer' embedding: "Embedding" legal: "Legal" + api: 'API' user_api: 'User API' uncategorized: 'Other' backups: "Backups" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index b31c87349ad..e4c0c8fe1f4 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1556,6 +1556,8 @@ en: default_categories_muted: "List of categories that are muted by default." default_categories_watching_first_post: "List of categories in which first post in each new topic will be watched by default." + retain_web_hook_events_period_days: "Number of days to retain web hook event records." + max_user_api_reqs_per_day: "Maximum number of user API requests per key per day" max_user_api_reqs_per_minute: "Maximum number of user API requests per key per minute" allow_user_api_keys: "Allow generation of user API keys" diff --git a/config/site_settings.yml b/config/site_settings.yml index 5df79b5672e..175fb21ff06 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -1427,6 +1427,10 @@ user_preferences: type: category_list default: '' +api: + retain_web_hook_events_period_days: + default: 30 + user_api: max_user_api_reqs_per_day: default: 2880 diff --git a/spec/models/web_hook_event_spec.rb b/spec/models/web_hook_event_spec.rb index 5f6a9046bc5..b09b3c30ed0 100644 --- a/spec/models/web_hook_event_spec.rb +++ b/spec/models/web_hook_event_spec.rb @@ -4,13 +4,36 @@ describe WebHookEvent do let(:event) { WebHookEvent.new(status: 200, web_hook: Fabricate(:web_hook)) } let(:failed_event) { WebHookEvent.new(status: 400, web_hook: Fabricate(:web_hook)) } - it 'update last delivery status for associated WebHook record' do - event.update_web_hook_delivery_status - expect(event.web_hook.last_delivery_status).to eq(WebHook.last_delivery_statuses[:successful]) + describe '.purge_old' do + before do + SiteSetting.retain_web_hook_events_period_days = 1 + end + + it "should be able to purge old web hook events" do + web_hook = Fabricate(:web_hook) + web_hook_event = WebHookEvent.create!(status: 200, web_hook: web_hook) + WebHookEvent.create!(status: 200, web_hook: web_hook, created_at: 2.days.ago) + + expect { described_class.purge_old } + .to change { WebHookEvent.count }.by(-1) + + expect(WebHookEvent.find(web_hook_event.id)).to eq(web_hook_event) + end end - it 'sets last delivery status to failed' do - failed_event.update_web_hook_delivery_status - expect(failed_event.web_hook.last_delivery_status).to eq(WebHook.last_delivery_statuses[:failed]) + describe '#update_web_hook_delivery_status' do + it 'update last delivery status for associated WebHook record' do + event.update_web_hook_delivery_status + + expect(event.web_hook.last_delivery_status) + .to eq(WebHook.last_delivery_statuses[:successful]) + end + + it 'sets last delivery status to failed' do + failed_event.update_web_hook_delivery_status + + expect(failed_event.web_hook.last_delivery_status) + .to eq(WebHook.last_delivery_statuses[:failed]) + end end end