diff --git a/app/models/user_search.rb b/app/models/user_search.rb index aafc0d3e812..7498b230beb 100644 --- a/app/models/user_search.rb +++ b/app/models/user_search.rb @@ -64,11 +64,14 @@ class UserSearch def search_ids users = Set.new - # 1. exact username matches active in the past year + # 1. exact username matches if @term.present? - scoped_users - .where(username_lower: @term) - .where('last_seen_at > ?', 1.year.ago) + exact_matches = scoped_users.where(username_lower: @term) + + # don't polute mentions with users who haven't shown up in over a year + exact_matches = exact_matches.where('last_seen_at > ?', 1.year.ago) if @topic_id || @category_id + + exact_matches .limit(@limit) .pluck(:id) .each { |id| users << id } diff --git a/spec/models/user_search_spec.rb b/spec/models/user_search_spec.rb index d631d59d972..c72abc84686 100644 --- a/spec/models/user_search_spec.rb +++ b/spec/models/user_search_spec.rb @@ -212,13 +212,13 @@ describe UserSearch do expect(results).to eq [mr_b, mr_brown, mr_blue].map(&:username) end - it "doesn't prioritises exact matches for users who haven't been seen in more than 1 year" do + it "doesn't prioritises exact matches mentions for users who haven't been seen in over a year" do abcdef = Fabricate(:user, username: "abcdef", last_seen_at: 2.days.ago) abcde = Fabricate(:user, username: "abcde", last_seen_at: 2.weeks.ago) abcd = Fabricate(:user, username: "abcd", last_seen_at: 2.months.ago) abc = Fabricate(:user, username: "abc", last_seen_at: 2.years.ago) - results = search_for("abc") + results = search_for("abc", topic_id: topic.id) expect(results).to eq [abcdef, abcde, abcd, abc].map(&:username) end