FIX: sending messages to groups with non-lowercase names

FIX: sending messages to groups with non-lowercase names

Fixes a regression in

e8fb9d406667c758cba0f7b1cc779e1ceb352c4c

which caused a bug where you couldn’t send a message to a group that contained an Uppercase letter. Added a test case for this.

Bug report: https://meta.discourse.org/t/-/152999

diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 4830b3b..4ee3b29 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -748,7 +748,7 @@ class PostsController < ApplicationController
 
     if recipients
       recipients = recipients.split(",").map(&:downcase)
-      groups = Group.messageable(current_user).where('lower(name) in (?)', recipients).pluck('name')
+      groups = Group.messageable(current_user).where('lower(name) in (?)', recipients).pluck('lower(name)')
       recipients -= groups
       emails = recipients.select { |user| user.match(/@/) }
       recipients -= emails
diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb
index d86f1c4..c52396b 100644
--- a/lib/topic_creator.rb
+++ b/lib/topic_creator.rb
@@ -246,10 +246,10 @@ class TopicCreator
 
   def add_groups(topic, groups)
     return unless groups
-    names = groups.split(',').flatten
+    names = groups.split(',').flatten.map(&:downcase)
     len = 0
 
-    Group.where(name: names).each do |group|
+    Group.where('lower(name) in (?)', names).each do |group|
       check_can_send_permission!(topic, group)
       topic.topic_allowed_groups.build(group_id: group.id)
       len += 1
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 910cb5b..e9ed6c4 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -838,6 +838,30 @@ describe PostsController do
         expect(post.topic.topic_allowed_groups.length).to eq(1)
       end
 
+      it "can send a message to a group with caps" do
+        group = Group.create(name: 'Test_group', messageable_level: Group::ALIAS_LEVELS[:nobody])
+        user1 = user
+        group.add(user1)
+
+        # allow pm to this group
+        group.update_columns(messageable_level: Group::ALIAS_LEVELS[:everyone])
+
+        post "/posts.json", params: {
+          raw: 'I can haz a test',
+          title: 'I loves my test',
+          target_recipients: "test_Group",
+          archetype: Archetype.private_message
+        }
+
+        expect(response.status).to eq(200)
+
+        parsed = response.parsed_body
+        post = Post.find(parsed['id'])
+
+        expect(post.topic.topic_allowed_users.length).to eq(1)
+        expect(post.topic.topic_allowed_groups.length).to eq(1)
+      end
+
       it "returns the nested post with a param" do
         post "/posts.json", params: {
           raw: 'this is the test content',

GitHub sha: 6548cd1a

2 Likes

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

https://meta.discourse.org/t/error-sending-messages-to-groups/152999/8