Skip to content

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

PERF: Stop parsing event invitees past the bulk invite limit

Régis Hanolbc203eeadd8f
diff --git a/plugins/discourse-events/app/services/discourse_events/events/action/parse_invitees_csv.rb b/plugins/discourse-events/app/services/discourse_events/events/action/parse_invitees_csv.rb
index 152891a3d5c..ccbdd38180d 100644
--- a/plugins/discourse-events/app/services/discourse_events/events/action/parse_invitees_csv.rb
+++ b/plugins/discourse-events/app/services/discourse_events/events/action/parse_invitees_csv.rb
@@ -11,8 +11,10 @@ module DiscourseEvents
option :file
def call
+ max_invitees = SiteSetting.discourse_post_event_max_bulk_invitees
invitees = []
CSV.foreach(file.tempfile) do |identifier, attendance|
+ break if invitees.size >= max_invitees
next if identifier.blank?
invitees << { identifier: identifier, attendance: attendance || DEFAULT_ATTENDANCE }
end
diff --git a/plugins/discourse-events/app/services/discourse_events/events/bulk_invite.rb b/plugins/discourse-events/app/services/discourse_events/events/bulk_invite.rb
index 991b879fa07..47c8565d0a1 100644
--- a/plugins/discourse-events/app/services/discourse_events/events/bulk_invite.rb
+++ b/plugins/discourse-events/app/services/discourse_events/events/bulk_invite.rb
@@ -9,7 +9,10 @@ module DiscourseEvents
attribute :event_id, :integer
attribute :invitees, :array
- before_validation { self.invitees = Array(invitees).reject(&:blank?) }
+ before_validation do
+ max_invitees = SiteSetting.discourse_post_event_max_bulk_invitees
+ self.invitees = Array(invitees).reject(&:blank?).first(max_invitees)
+ end
validates :event_id, presence: true
end
diff --git a/plugins/discourse-events/spec/services/discourse_events/events/action/parse_invitees_csv_spec.rb b/plugins/discourse-events/spec/services/discourse_events/events/action/parse_invitees_csv_spec.rb
index bc49d1334a6..0b5cbb3ffe9 100644
--- a/plugins/discourse-events/spec/services/discourse_events/events/action/parse_invitees_csv_spec.rb
+++ b/plugins/discourse-events/spec/services/discourse_events/events/action/parse_invitees_csv_spec.rb
@@ -40,6 +40,16 @@ RSpec.describe(DiscourseEvents::Events::Action::ParseInviteesCsv) do
end
end
+ context "when the file has more rows than the maximum number of bulk invitees" do
+ let(:file) { csv_file("a,going\nb,going\nc,going\n") }
+
+ before { SiteSetting.discourse_post_event_max_bulk_invitees = 2 }
+
+ it "stops parsing at the maximum" do
+ expect(result.size).to eq(2)
+ end
+ end
+
context "when the file is malformed" do
let(:file) { csv_file("alice,\"unterminated") }
diff --git a/plugins/discourse-events/spec/services/discourse_events/events/bulk_invite_spec.rb b/plugins/discourse-events/spec/services/discourse_events/events/bulk_invite_spec.rb
index f49d16d00ac..d47a95aa65d 100644
--- a/plugins/discourse-events/spec/services/discourse_events/events/bulk_invite_spec.rb
+++ b/plugins/discourse-events/spec/services/discourse_events/events/bulk_invite_spec.rb
@@ -93,6 +93,25 @@ RSpec.describe(DiscourseEvents::Events::BulkInvite) do
},
) { result }
end
+
+ context "with more invitees than the maximum" do
+ let(:invitees) do
+ Array.new(3) { |i| { "identifier" => "user#{i}", "attendance" => "going" } }
+ end
+
+ before { SiteSetting.discourse_post_event_max_bulk_invitees = 2 }
+
+ it "enqueues no more invitees than the job will process" do
+ expect_enqueued_with(
+ job: :discourse_post_event_bulk_invite,
+ args: {
+ event_id: event.id,
+ invitees: invitees.first(2),
+ current_user_id: admin.id,
+ },
+ ) { result }
+ end
+ end
end
end
end