A platform for community discussion. Free, open, simple.
FEATURE: Allow searching users using a list of usernames (#23902)
Sérgio Saquetim526d2dc582d0
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex 1392384f66a..4fa518e6f35 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -1203,7 +1203,10 @@ class UsersController < ApplicationControllerSEARCH_USERS_LIMIT = 50def search_users+ # the search can specify the parameter term or usernames, term will perform the classic user search algorithm while+ # usernames will perform an exact search on the usernames passed as parameterterm = params[:term].to_s.strip+ usernames = params[:usernames]&.split(",")&.map { |username| username.downcase.strip }topic_id = params[:topic_id].to_i if params[:topic_id].present?category_id = params[:category_id].to_i if params[:category_id].present?@@ -1232,13 +1235,18 @@ class UsersController < ApplicationControlleroptions[:topic_id] = topic_id if topic_idoptions[:category_id] = category_id if category_id- results = UserSearch.new(term, options).search+ results =+ if usernames.blank?+ UserSearch.new(term, options).search+ else+ User.where(username_lower: usernames).limit(limit)+ endto_render = serialize_found_users(results)# blank term is only handy for in-topic search of users after @# we do not want group results ever if term is blankgroups =- if term.present? && current_user+ if (term.present? || usernames.present?) && current_userif params[:include_groups] == "true"Group.visible_groups(current_user)elsif params[:include_mentionable_groups] == "true"@@ -1260,7 +1268,12 @@ class UsersController < ApplicationController# register_modifier(:groups_for_users_search) will be evaluated without needing the# param.groups = DiscoursePluginRegistry.apply_modifier(:groups_for_users_search, groups)- groups = Group.search_groups(term, groups: groups, sort: :auto)+ groups =+ if usernames.blank?+ Group.search_groups(term, groups: groups, sort: :auto)+ else+ groups.where(name: usernames).limit(limit)+ endto_render[:groups] = groups.map { |m| { name: m.name, full_name: m.full_name } }enddiff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex 868fde5826d..37182039147 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -4901,6 +4901,35 @@ RSpec.describe UsersController doexpect(json["users"].map { |u| u["username"] }).to include(user.username)end+ context "when searching usernames" do+ it "searches when provided a list of usernames" do+ users = Fabricate.times(3, :user)++ get "/u/search/users.json", params: { usernames: users.map(&:username).join(",") }+ expect(response.status).to eq(200)+ json = response.parsed_body+ expect(json["users"].map { |u| u["username"] }).to match_array(users.map(&:username))+ end++ it "searches groups if include_groups = true" do+ users = Fabricate.times(3, :user)+ group = Fabricate(:group)++ sign_in(user)++ get "/u/search/users.json",+ params: {+ usernames: [group.name, users.first.username].join(","),+ include_groups: true,+ }+ expect(response.status).to eq(200)+ json = response.parsed_body++ expect(json["users"].map { |u| u["username"] }).to contain_exactly(users.first.username)+ expect(json["groups"].map { |u| u["name"] }).to contain_exactly(group.name)+ end+ end+it "searches when provided the topic only" doget "/u/search/users.json", params: { topic_id: topic.id }expect(response.status).to eq(200)