DEV: optimize bulk invite process

DEV: optimize bulk invite process

diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb
index e0d1bb5..c01eaf7 100644
--- a/app/controllers/invites_controller.rb
+++ b/app/controllers/invites_controller.rb
@@ -176,20 +176,22 @@ class InvitesController < ApplicationController
       begin
         file = params[:file] || params[:files].first
 
-        if File.read(file.tempfile).scan(/\n/).count.to_i > 50000
-          return render json: failed_json.merge(errors: [I18n.t("bulk_invite.max_rows")]), status: 422
-        end
-
+        count = 0
         invites = []
+        max_bulk_invites = SiteSetting.max_bulk_invites
         CSV.foreach(file.tempfile) do |row|
-          invite_hash = { email: row[0], groups: row[1], topic_id: row[2] }
-          if invite_hash[:email].present?
-            invites.push(invite_hash)
-          end
+          count += 1
+          invites.push({ email: row[0], groups: row[1], topic_id: row[2] }) if row[0].present?
+          break if count >= max_bulk_invites
         end
+
         if invites.present?
           Jobs.enqueue(:bulk_invite, invites: invites, current_user_id: current_user.id)
-          render json: success_json
+          if count >= max_bulk_invites
+            render json: failed_json.merge(errors: [I18n.t("bulk_invite.max_rows", max_bulk_invites: max_bulk_invites)]), status: 422
+          else
+            render json: success_json
+          end
         else
           render json: failed_json.merge(errors: [I18n.t("bulk_invite.error")]), status: 422
         end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index cea98cf..ad33b61 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -218,7 +218,7 @@ en:
 
   bulk_invite:
     file_should_be_csv: "The uploaded file should be of csv format."
-    max_rows: "Maximum of 50,000 invites can be sent at a time. Try splitting the file in smaller parts."
+    max_rows: "First %{max_bulk_invites} invites has been sent. Try splitting the file in smaller parts."
     error: "There was an error uploading that file. Please try again later."
 
   topic_invite:
diff --git a/config/site_settings.yml b/config/site_settings.yml
index acca5be..0714123 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1912,6 +1912,10 @@ uncategorized:
     default: false
     hidden: true
 
+  max_bulk_invites:
+    default: 50000
+    hidden: true
+
 user_preferences:
   default_email_digest_frequency:
     enum: "DigestEmailSiteSetting"
diff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rb
index dcbd5c0..8b83368 100644
--- a/spec/requests/invites_controller_spec.rb
+++ b/spec/requests/invites_controller_spec.rb
@@ -508,6 +508,17 @@ describe InvitesController do
         expect(response.status).to eq(200)
         expect(Jobs::BulkInvite.jobs.size).to eq(1)
       end
+
+      it "sends limited invites at a time" do
+        SiteSetting.max_bulk_invites = 3
+        sign_in(Fabricate(:admin))
+        post "/invites/upload_csv.json", params: { file: file, name: filename }
+
+        expect(response.status).to eq(422)
+        expect(Jobs::BulkInvite.jobs.size).to eq(1)
+        json = ::JSON.parse(response.body)
+        expect(json["errors"][0]).to eq(I18n.t("bulk_invite.max_rows", max_bulk_invites: SiteSetting.max_bulk_invites))
+      end
     end
   end
 end

GitHub sha: 7b66f8fb

1 Like