2013-10-30 15:45:13 -04:00
|
|
|
# Searches for a user by username or full text or name (if enabled in SiteSettings)
|
2013-02-07 04:34:49 -05:00
|
|
|
class UserSearch
|
2013-02-07 05:54:55 -05:00
|
|
|
|
2014-05-13 11:44:06 -04:00
|
|
|
def initialize(term, opts={})
|
2013-10-30 15:45:13 -04:00
|
|
|
@term = term
|
|
|
|
@term_like = "#{term.downcase}%"
|
2014-05-13 11:44:06 -04:00
|
|
|
@topic_id = opts[:topic_id]
|
|
|
|
@searching_user = opts[:searching_user]
|
2014-05-14 22:22:35 -04:00
|
|
|
@limit = opts[:limit] || 20
|
2013-10-30 15:45:13 -04:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
def search
|
2014-05-14 22:22:35 -04:00
|
|
|
users = User.order(User.sql_fragment("CASE WHEN username_lower = ? THEN 0 ELSE 1 END ASC", @term.downcase))
|
2013-02-07 04:34:49 -05:00
|
|
|
|
2014-08-13 13:30:25 -04:00
|
|
|
users = users.where(active: true)
|
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
if @term.present?
|
|
|
|
if SiteSetting.enable_names?
|
2014-07-04 19:11:41 -04:00
|
|
|
query = Search.ts_query(@term, "simple")
|
|
|
|
users = users.includes(:user_search_data)
|
|
|
|
.references(:user_search_data)
|
|
|
|
.where("username_lower LIKE :term_like OR user_search_data.search_data @@ #{query}",
|
|
|
|
term: @term, term_like: @term_like)
|
|
|
|
.order(User.sql_fragment("CASE WHEN username_lower LIKE ? THEN 0 ELSE 1 END ASC", @term_like))
|
2013-10-30 15:45:13 -04:00
|
|
|
else
|
|
|
|
users = users.where("username_lower LIKE :term_like", term_like: @term_like)
|
|
|
|
end
|
2013-02-07 04:34:49 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
if @topic_id
|
|
|
|
users = users.joins(User.sql_fragment("LEFT JOIN (SELECT DISTINCT p.user_id FROM POSTS p WHERE topic_id = ?) s ON s.user_id = users.id", @topic_id))
|
|
|
|
.order("CASE WHEN s.user_id IS NULL THEN 0 ELSE 1 END DESC")
|
2013-02-07 04:34:49 -05:00
|
|
|
end
|
|
|
|
|
2014-05-13 11:44:06 -04:00
|
|
|
unless @searching_user && @searching_user.staff?
|
|
|
|
users = users.not_suspended
|
|
|
|
end
|
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
users.order("CASE WHEN last_seen_at IS NULL THEN 0 ELSE 1 END DESC, last_seen_at DESC, username ASC")
|
2014-05-14 22:22:35 -04:00
|
|
|
.limit(@limit)
|
2013-02-07 04:34:49 -05:00
|
|
|
end
|
2013-10-30 15:45:13 -04:00
|
|
|
|
2013-02-10 07:37:24 -05:00
|
|
|
end
|