mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:29:30 +08:00
Extract search logic to UserSearch model
This commit is contained in:
parent
0136727595
commit
972b9d735c
|
@ -288,43 +288,12 @@ class UsersController < ApplicationController
|
|||
end
|
||||
|
||||
def search_users
|
||||
|
||||
term = (params[:term] || "").strip.downcase
|
||||
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
|
||||
end
|
||||
|
||||
|
|
38
app/models/user_search.rb
Normal file
38
app/models/user_search.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
class UserSearch
|
||||
|
||||
def self.search term, 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
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user