FIX: when creating new PM username/groupname should be case-insensitive

diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index c440d63..4830b3b 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -747,8 +747,8 @@ class PostsController < ApplicationController
     end
 
     if recipients
-      recipients = recipients.split(",")
-      groups = Group.messageable(current_user).where('name in (?)', recipients).pluck('name')
+      recipients = recipients.split(",").map(&:downcase)
+      groups = Group.messageable(current_user).where('lower(name) in (?)', recipients).pluck('name')
       recipients -= groups
       emails = recipients.select { |user| user.match(/@/) }
       recipients -= emails
diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb
index 4155878..6610896 100644
--- a/lib/topic_creator.rb
+++ b/lib/topic_creator.rb
@@ -208,10 +208,10 @@ class TopicCreator
   def add_users(topic, usernames)
     return unless usernames
 
-    names = usernames.split(',').flatten
+    names = usernames.split(',').map(&:downcase).flatten
     len = 0
 
-    User.includes(:user_option).where(username: names).find_each do |user|
+    User.includes(:user_option).where(username_lower: names).find_each do |user|
       check_can_send_permission!(topic, user)
       @added_users << user
       topic.topic_allowed_users.build(user_id: user.id)
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index ff355ad..910cb5b 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -825,7 +825,7 @@ describe PostsController do
         post "/posts.json", params: {
           raw: 'I can haz a test',
           title: 'I loves my test',
-          target_recipients: group.name,
+          target_recipients: "test_Group",
           archetype: Archetype.private_message
         }
 
@@ -965,13 +965,13 @@ describe PostsController do
 
       it 'creates a private post' do
         user_2 = Fabricate(:user)
-        user_3 = Fabricate(:user)
+        user_3 = Fabricate(:user, username: "foo_bar")
 
         post "/posts.json", params: {
           raw: 'this is the test content',
           archetype: 'private_message',
           title: "this is some post",
-          target_recipients: "#{user_2.username},#{user_3.username}"
+          target_recipients: "#{user_2.username},Foo_Bar"
         }
 
         expect(response.status).to eq(200)

GitHub sha: 2be79d94

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/remove-case-sensitive-in-adding-users-to-a-message/151275/7

This commit has been mentioned on Discourse Meta. There might be relevant details there: