PERF: Try to match users before groups.

User mentions are more common than group mentions so
this will allow us to avoid an extra query.
This commit is contained in:
Guo Xiang Tan 2018-11-16 16:41:20 +08:00
parent 44d7249a17
commit 45f299dfdd
2 changed files with 9 additions and 1 deletions

View File

@ -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)

View File

@ -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