FIX: Support mixing usernames and emails

FIX: Support mixing usernames and emails

diff --git a/app/jobs/bcc_post.rb b/app/jobs/bcc_post.rb
index 8cf1e13..e5d4672 100644
--- a/app/jobs/bcc_post.rb
+++ b/app/jobs/bcc_post.rb
@@ -9,10 +9,19 @@ class ::Jobs::BccPost < ::Jobs::Base
 
     create_params = args[:create_params]
     create_params[:skip_validations] = true
-    split_usernames = create_params.delete(:target_usernames).split(',')
 
-    split_usernames.each do |username|
-      creator = PostCreator.new(sender, create_params.merge(target_usernames: username))
+    split_usernames = (create_params.delete(:target_usernames) || '').split(',')
+    split_emails = (create_params.delete(:target_emails) || '').split(',')
+
+    send_to(split_usernames, :target_usernames, create_params, sender)
+    send_to(split_emails, :target_emails, create_params, sender)
+  end
+
+  private
+
+  def send_to(targets, targets_key, params, sender)
+    targets.each do |target|
+      creator = PostCreator.new(sender, params.merge(Hash[targets_key, target]))
       creator.create
     end
   end
diff --git a/spec/jobs/bcc_post_spec.rb b/spec/jobs/bcc_post_spec.rb
index c7ddcf2..09ca685 100644
--- a/spec/jobs/bcc_post_spec.rb
+++ b/spec/jobs/bcc_post_spec.rb
@@ -31,6 +31,30 @@ describe ::Jobs::BccPost do
       ::Jobs::BccPost.new.execute(user_id: sender.id, create_params: create_params)
       expect(Topic.count).to eq(topic_count + 2)
     end
+
+    it 'works when mixing emails and usernames' do
+      SiteSetting.enable_staged_users = true
+      topic_count = Topic.count
+
+      ::Jobs::BccPost.new.execute(user_id: sender.id, create_params: create_params.merge(target_emails: 'test@test.com'))
+      
+      expect(Topic.count).to eq(topic_count + 3)
+    end
+
+    it 'works when only using emails' do
+      SiteSetting.enable_staged_users = true
+      topic_count = Topic.count
+
+      ::Jobs::BccPost.new.execute(
+        user_id: sender.id, 
+        create_params: create_params.merge(
+          target_usernames: nil, 
+          target_emails: 'test@test.com,test2@test.com'
+        )
+      )
+      
+      expect(Topic.count).to eq(topic_count + 2)
+    end
   end
 
 end

GitHub sha: 269bceb7

This commit appears in #2 which was approved by eviltrout. It was merged by romanrizzi.