mirror of
https://github.com/discourse/discourse.git
synced 2025-03-23 03:05:48 +08:00
Merge pull request #57 from blowmage/user_search_refactor
Refactor UserController#search_users
This commit is contained in:
commit
63c0fdda44
app
spec
@ -288,44 +288,14 @@ class UsersController < ApplicationController
|
||||
end
|
||||
|
||||
def search_users
|
||||
|
||||
term = (params[:term] || "").strip.downcase
|
||||
term = params[:term].to_s.strip
|
||||
topic_id = params[:topic_id]
|
||||
topic_id = topic_id.to_i if topic_id
|
||||
|
||||
sql = "select username, name, email from users u "
|
||||
if topic_id
|
||||
sql << "left join (select distinct p.user_id from posts p where topic_id = :topic_id) s on
|
||||
s.user_id = u.id "
|
||||
end
|
||||
results = UserSearch.search term, topic_id
|
||||
|
||||
if term.length > 0
|
||||
sql << "where username_lower like :term_like or
|
||||
to_tsvector('simple', name) @@
|
||||
to_tsquery('simple',
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
cast(plainto_tsquery(:term) as text)
|
||||
,'\''(?: |$)', ':*''', 'g'),
|
||||
'''', '', 'g')
|
||||
) "
|
||||
|
||||
end
|
||||
|
||||
sql << "order by case when username_lower = :term then 0 else 1 end asc, "
|
||||
if topic_id
|
||||
sql << " case when s.user_id is null then 0 else 1 end desc, "
|
||||
end
|
||||
|
||||
sql << " case when last_seen_at is null then 0 else 1 end desc, last_seen_at desc, username asc limit(20)"
|
||||
|
||||
results = User.exec_sql(sql, topic_id: topic_id, term_like: "#{term}%", term: term)
|
||||
results = results.map do |r|
|
||||
r["avatar_template"] = User.avatar_template(r["email"])
|
||||
r.delete("email")
|
||||
r
|
||||
end
|
||||
render :json => results
|
||||
render json: { users: results.as_json( only: [ :username, :name ],
|
||||
methods: :avatar_template ) }
|
||||
end
|
||||
|
||||
private
|
||||
|
43
app/models/user_search.rb
Normal file
43
app/models/user_search.rb
Normal file
@ -0,0 +1,43 @@
|
||||
class UserSearch
|
||||
|
||||
def self.search term, topic_id = nil
|
||||
User.find_by_sql sql(term, topic_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.sql term, topic_id
|
||||
sql = "select id, username, name, email from users u "
|
||||
if topic_id
|
||||
sql << "left join (select distinct p.user_id from posts p where topic_id = :topic_id) s on
|
||||
s.user_id = u.id "
|
||||
end
|
||||
|
||||
if term.present?
|
||||
sql << "where username ilike :term_like or
|
||||
to_tsvector('simple', name) @@
|
||||
to_tsquery('simple',
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
cast(plainto_tsquery(:term) as text)
|
||||
,'\''(?: |$)', ':*''', 'g'),
|
||||
'''', '', 'g')
|
||||
) "
|
||||
|
||||
end
|
||||
|
||||
sql << "order by case when username_lower = :term then 0 else 1 end asc, "
|
||||
if topic_id
|
||||
sql << " case when s.user_id is null then 0 else 1 end desc, "
|
||||
end
|
||||
|
||||
sql << " case when last_seen_at is null then 0 else 1 end desc, last_seen_at desc, username asc limit(20)"
|
||||
|
||||
sanitize_sql_array(sql, topic_id: topic_id, term_like: "#{term}%", term: term)
|
||||
end
|
||||
|
||||
def self.sanitize_sql_array *args
|
||||
ActiveRecord::Base.send(:sanitize_sql_array, args)
|
||||
end
|
||||
|
||||
end
|
@ -671,4 +671,36 @@ describe UsersController do
|
||||
|
||||
end
|
||||
|
||||
describe "search_users" do
|
||||
|
||||
let(:topic) { Fabricate :topic }
|
||||
let(:user) { Fabricate :user, username: "joecabot", name: "Lawrence Tierney" }
|
||||
|
||||
before do
|
||||
Fabricate :post, user: user, topic: topic
|
||||
end
|
||||
|
||||
it "searches when provided the term only" do
|
||||
xhr :post, :search_users, term: user.name.split(" ").last
|
||||
response.should be_success
|
||||
json = JSON.parse(response.body)
|
||||
json["users"].map { |u| u["username"] }.should include(user.username)
|
||||
end
|
||||
|
||||
it "searches when provided the topic only" do
|
||||
xhr :post, :search_users, topic_id: topic.id
|
||||
response.should be_success
|
||||
json = JSON.parse(response.body)
|
||||
json["users"].map { |u| u["username"] }.should include(user.username)
|
||||
end
|
||||
|
||||
it "searches when provided the term and topic" do
|
||||
xhr :post, :search_users, term: user.name.split(" ").last, topic_id: topic.id
|
||||
response.should be_success
|
||||
json = JSON.parse(response.body)
|
||||
json["users"].map { |u| u["username"] }.should include(user.username)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
83
spec/models/user_search_spec.rb
Normal file
83
spec/models/user_search_spec.rb
Normal file
@ -0,0 +1,83 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe UserSearch do
|
||||
|
||||
let(:topic) { Fabricate :topic }
|
||||
let(:topic2) { Fabricate :topic }
|
||||
let(:topic3) { Fabricate :topic }
|
||||
let(:user1) { Fabricate :user, username: "mrblonde", name: "Michael Madsen" }
|
||||
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Bunker" }
|
||||
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth" }
|
||||
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi" }
|
||||
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino" }
|
||||
let(:user6) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel" }
|
||||
|
||||
before do
|
||||
Fabricate :post, user: user1, topic: topic
|
||||
Fabricate :post, user: user2, topic: topic2
|
||||
Fabricate :post, user: user3, topic: topic
|
||||
Fabricate :post, user: user4, topic: topic
|
||||
Fabricate :post, user: user5, topic: topic3
|
||||
Fabricate :post, user: user6, topic: topic
|
||||
end
|
||||
|
||||
context "all user search" do
|
||||
it "searches the user's name" do
|
||||
results = UserSearch.search user1.name.split(" ").first
|
||||
results.size.should == 1
|
||||
results.first.should == user1
|
||||
end
|
||||
|
||||
it "searches the user's name case insensitive" do
|
||||
results = UserSearch.search user1.name.split(" ").first.downcase
|
||||
results.size.should == 1
|
||||
results.first.should == user1
|
||||
end
|
||||
|
||||
it "searches the user's username" do
|
||||
results = UserSearch.search user4.username
|
||||
results.size.should == 1
|
||||
results.first.should == user4
|
||||
end
|
||||
|
||||
it "searches the user's username case insensitive" do
|
||||
results = UserSearch.search user4.username.upcase
|
||||
results.size.should == 1
|
||||
results.first.should == user4
|
||||
end
|
||||
|
||||
it "searches the user's username substring" do
|
||||
results = UserSearch.search "mr"
|
||||
results.size.should == 6
|
||||
|
||||
results = UserSearch.search "mrb"
|
||||
results.size.should == 3
|
||||
end
|
||||
|
||||
it "searches the user's username substring upper case" do
|
||||
results = UserSearch.search "MR"
|
||||
results.size.should == 6
|
||||
|
||||
results = UserSearch.search "MRB"
|
||||
results.size.should == 3
|
||||
end
|
||||
end
|
||||
|
||||
context "sort order respects users with posts on the topic" do
|
||||
it "Mr. Blond is first when searching his topic" do
|
||||
results = UserSearch.search "mrb", topic.id
|
||||
results.first.should == user1
|
||||
end
|
||||
|
||||
it "Mr. Blue is first when searching his topic" do
|
||||
results = UserSearch.search "mrb", topic2.id
|
||||
results.first.should == user2
|
||||
end
|
||||
|
||||
it "Mr. Brown is first when searching his topic" do
|
||||
results = UserSearch.search "mrb", topic3.id
|
||||
results.first.should == user5
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user