Skip to content

A platform for community discussion. Free, open, simple.

PERF: clamp the limit of max occurrences (#38326)

Joffrey JAFFEUXa7651645e5af
diff --git a/plugins/discourse-calendar/app/services/discourse_post_event/action/expand_occurrences.rb b/plugins/discourse-calendar/app/services/discourse_post_event/action/expand_occurrences.rb
index cbf722d7bdc..f6468d70edf 100644
--- a/plugins/discourse-calendar/app/services/discourse_post_event/action/expand_occurrences.rb
+++ b/plugins/discourse-calendar/app/services/discourse_post_event/action/expand_occurrences.rb
@@ -3,6 +3,8 @@
module DiscoursePostEvent
module Action
class ExpandOccurrences < Service::ActionBase
+ MAX_LIMIT = 200
+
option :event
option :after
option :before, optional: true
@@ -27,8 +29,9 @@ module DiscoursePostEvent
occurrences = []
current_time = after
count = 0
+ capped_limit = limit.clamp(1, MAX_LIMIT)
- while count < limit
+ while count < capped_limit
occurrence = event.calculate_next_occurrence_from(current_time)
break unless occurrence
diff --git a/plugins/discourse-calendar/spec/services/discourse_post_event/action/expand_occurrences_spec.rb b/plugins/discourse-calendar/spec/services/discourse_post_event/action/expand_occurrences_spec.rb
new file mode 100644
index 00000000000..c2e5ad7151a
--- /dev/null
+++ b/plugins/discourse-calendar/spec/services/discourse_post_event/action/expand_occurrences_spec.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscoursePostEvent::Action::ExpandOccurrences do
+ describe ".call" do
+ subject(:action) { described_class.call(event:, after:, before:, limit:) }
+
+ let(:after) { Time.zone.parse("2030-01-01 00:00:00 UTC") }
+ let(:before) { nil }
+ let(:limit) { described_class::MAX_LIMIT + 1 }
+ let(:event) { instance_double(DiscoursePostEvent::Event, recurring?: true) }
+
+ before do
+ next_occurrence_starts_at = Time.zone.parse("2030-01-01 09:00:00 UTC")
+
+ allow(event).to receive(:calculate_next_occurrence_from) do
+ starts_at = next_occurrence_starts_at
+ next_occurrence_starts_at += 1.day
+ { starts_at:, ends_at: starts_at + 1.hour }
+ end
+ end
+
+ it "caps recurring occurrences at the maximum limit" do
+ expect(action[:occurrences].size).to eq(described_class::MAX_LIMIT)
+ end
+ end
+end