Skip to content

A platform for community discussion. Free, open, simple.

PERF: Try to match users before groups.

Guo Xiang Tan45f299dfdda7
diff --git a/lib/pretty_text/helpers.rb b/lib/pretty_text/helpers.rb
index 09d7b1a31b9..54b33a27ea1 100644
--- a/lib/pretty_text/helpers.rb
+++ b/lib/pretty_text/helpers.rb
@@ -41,8 +41,8 @@ module PrettyText
def mention_lookup(name)
return false if name.blank?
- return "group" if Group.exists?(name: name)
return "user" if User.exists?(username_lower: name.downcase)
+ return "group" if Group.exists?(name: name)
end
def category_hashtag_lookup(category_slug)
diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb
index dd68a554fca..adb4bec8fda 100644
--- a/spec/components/pretty_text_spec.rb
+++ b/spec/components/pretty_text_spec.rb
@@ -226,6 +226,14 @@ describe PrettyText do
expect(PrettyText.cook("hi\n@sam.")).to eq("<p>hi<br>\n<a class=\"mention\" href=\"/u/sam\">@sam</a>.</p>")
end
+ it "can handle group mention" do
+ group = Fabricate(:group)
+
+ expect(PrettyText.cook("hi @#{group.name}! hi")).to match_html(
+ %Q{<p>hi <a class="mention-group" href="/groups/#{group.name}">@#{group.name}</a>! hi</p>}
+ )
+ end
+
it "can handle mentions inside a hyperlink" do
expect(PrettyText.cook("<a> @inner</a> ")).to match_html '<p><a> @inner</a></p>'
end