FIX: Mention lookup should be case insensitive

FIX: Mention lookup should be case insensitive.

From 28a6cf8228cc5579fbca910693224f61fbc57c43 Mon Sep 17 00:00:00 2001
From: Guo Xiang Tan <tgx_world@hotmail.com>
Date: Thu, 22 Nov 2018 16:32:56 +0800
Subject: [PATCH] FIX: Mention lookup should be case insensitive.


diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index a22d269..9b7463b 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -429,7 +429,7 @@ module PrettyText
 
   def self.add_mentions(doc, user_id: nil)
     elements = doc.css("span.mention")
-    names = elements.map { |element| element.text[1..-1] }
+    names = elements.map { |element| name = element.text[1..-1] }
 
     mentions = lookup_mentions(names, user_id: user_id)
 
@@ -475,6 +475,7 @@ module PrettyText
     SQL
 
     user = User.find_by(id: user_id)
+    names.each(&:downcase!)
 
     results = DB.query(sql,
       names: names,
diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb
index 7b58eec..8a020d6 100644
--- a/spec/components/pretty_text_spec.rb
+++ b/spec/components/pretty_text_spec.rb
@@ -221,7 +221,7 @@ describe PrettyText do
     end
 
     it "handles user and group mentions correctly" do
-      ['user', 'user2'].each do |username |
+      ['User', 'user2'].each do |username |
         Fabricate(:user, username: username)
       end
 
@@ -231,12 +231,12 @@ describe PrettyText do
 
       [
         [
-          'hi @user! @user2 hi',
-          '<p>hi <a class="mention" href="/u/user">@user</a>! <a class="mention" href="/u/user2">@user2</a> hi</p>'
+          'hi @uSer! @user2 hi',
+          '<p>hi <a class="mention" href="/u/user">@uSer</a>! <a class="mention" href="/u/user2">@user2</a> hi</p>'
         ],
         [
-          "hi\n@user. @#{group.name} @somemention",
-          %Q|<p>hi<br>\n<a class="mention" href="/u/user">@user</a>. <a class="mention-group" href="/groups/#{group.name}">@#{group.name}</a> <span class="mention">@somemention</span></p>|
+          "hi\n@user. @#{group.name.capitalize} @somemention",
+          %Q|<p>hi<br>\n<a class="mention" href="/u/user">@user</a>. <a class="mention-group" href="/groups/#{group.name}">@#{group.name.capitalize}</a> <span class="mention">@somemention</span></p>|
         ]
       ].each do |input, expected|
         expect(PrettyText.cook(input)).to eq(expected)

GitHub

Do you need name anywhere?

3 Likes

Oops checked it in by mistake

2 Likes

Fixed in

2 Likes