2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-07 17:34:49 +08:00
|
|
|
class UserSearch
|
2024-10-16 10:09:07 +08:00
|
|
|
MAX_SIZE_PRIORITY_MENTION = 500
|
2019-08-07 08:31:25 +08:00
|
|
|
|
2014-05-13 23:44:06 +08:00
|
|
|
def initialize(term, opts = {})
|
2021-01-25 18:23:36 +08:00
|
|
|
@term = term.downcase
|
|
|
|
@term_like = @term.gsub("_", "\\_") + "%"
|
2014-05-13 23:44:06 +08:00
|
|
|
@topic_id = opts[:topic_id]
|
2019-08-06 15:57:45 +08:00
|
|
|
@category_id = opts[:category_id]
|
2015-04-13 23:03:13 +08:00
|
|
|
@topic_allowed_users = opts[:topic_allowed_users]
|
2014-05-13 23:44:06 +08:00
|
|
|
@searching_user = opts[:searching_user]
|
2017-11-19 08:17:31 +08:00
|
|
|
@include_staged_users = opts[:include_staged_users] || false
|
2021-07-21 21:14:53 +08:00
|
|
|
@last_seen_users = opts[:last_seen_users] || false
|
2014-05-15 10:22:35 +08:00
|
|
|
@limit = opts[:limit] || 20
|
2019-05-10 23:35:36 +08:00
|
|
|
@groups = opts[:groups]
|
2021-01-25 18:23:36 +08:00
|
|
|
|
|
|
|
@topic = Topic.find(@topic_id) if @topic_id
|
|
|
|
@category = Category.find(@category_id) if @category_id
|
|
|
|
|
2017-02-10 07:45:39 +08:00
|
|
|
@guardian = Guardian.new(@searching_user)
|
2021-01-25 18:23:36 +08:00
|
|
|
@guardian.ensure_can_see_groups_members!(@groups) if @groups
|
|
|
|
@guardian.ensure_can_see_category!(@category) if @category
|
|
|
|
@guardian.ensure_can_see_topic!(@topic) if @topic
|
2013-10-31 03:45:13 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2015-05-14 12:38:47 +08:00
|
|
|
def scoped_users
|
2017-11-19 08:17:31 +08:00
|
|
|
users = User.where(active: true)
|
2023-03-01 12:23:29 +08:00
|
|
|
users = users.where(approved: true) if SiteSetting.must_approve_users?
|
2017-11-19 08:17:31 +08:00
|
|
|
users = users.where(staged: false) unless @include_staged_users
|
2021-01-25 18:23:36 +08:00
|
|
|
users = users.not_suspended unless @searching_user&.staff?
|
2015-05-14 12:38:47 +08:00
|
|
|
|
2019-05-10 23:35:36 +08:00
|
|
|
if @groups
|
|
|
|
users = users.joins(:group_users).where("group_users.group_id IN (?)", @groups.map(&:id))
|
2017-02-10 07:45:39 +08:00
|
|
|
end
|
|
|
|
|
2015-05-14 12:38:47 +08:00
|
|
|
# Only show users who have access to private topic
|
2021-01-25 18:23:36 +08:00
|
|
|
if @topic_allowed_users == "true" && @topic&.category&.read_restricted
|
|
|
|
users =
|
|
|
|
users
|
|
|
|
.references(:categories)
|
|
|
|
.includes(:secure_categories)
|
|
|
|
.where("users.admin OR categories.id = ?", @topic.category_id)
|
2015-05-14 12:38:47 +08:00
|
|
|
end
|
2013-02-07 17:34:49 +08:00
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
users
|
2015-05-14 12:38:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_by_term_users
|
2021-01-25 18:23:36 +08:00
|
|
|
if @term.blank?
|
|
|
|
scoped_users
|
|
|
|
elsif SiteSetting.enable_names? && @term !~ /[_\.-]/
|
|
|
|
query = Search.ts_query(term: @term, ts_config: "simple")
|
|
|
|
|
|
|
|
scoped_users
|
|
|
|
.includes(:user_search_data)
|
|
|
|
.where("user_search_data.search_data @@ #{query}")
|
|
|
|
.order(DB.sql_fragment("CASE WHEN username_lower LIKE ? THEN 0 ELSE 1 END ASC", @term_like))
|
|
|
|
else
|
|
|
|
scoped_users.where("username_lower LIKE :term_like", term_like: @term_like)
|
2013-02-07 17:34:49 +08:00
|
|
|
end
|
2015-05-14 12:38:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def search_ids
|
|
|
|
users = Set.new
|
|
|
|
|
2021-01-26 04:27:15 +08:00
|
|
|
# 1. exact username matches
|
2015-05-14 12:38:47 +08:00
|
|
|
if @term.present?
|
2021-01-26 04:27:15 +08:00
|
|
|
exact_matches = scoped_users.where(username_lower: @term)
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
# don't pollute mentions with users who haven't shown up in over a year
|
2021-01-26 04:27:15 +08:00
|
|
|
exact_matches = exact_matches.where("last_seen_at > ?", 1.year.ago) if @topic_id ||
|
|
|
|
@category_id
|
|
|
|
|
2015-11-05 06:04:37 +08:00
|
|
|
exact_matches.limit(@limit).pluck(:id).each { |id| users << id }
|
2014-05-13 23:44:06 +08:00
|
|
|
end
|
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
return users.to_a if users.size >= @limit
|
2015-04-13 23:03:13 +08:00
|
|
|
|
2015-05-14 12:38:47 +08:00
|
|
|
# 2. in topic
|
|
|
|
if @topic_id
|
2019-08-06 15:57:45 +08:00
|
|
|
in_topic =
|
|
|
|
filtered_by_term_users.where(
|
2022-01-07 19:36:11 +08:00
|
|
|
"users.id IN (SELECT user_id FROM posts WHERE topic_id = ? AND post_type = ? AND deleted_at IS NULL)",
|
|
|
|
@topic_id,
|
|
|
|
Post.types[:regular],
|
|
|
|
)
|
2019-02-20 10:33:56 +08:00
|
|
|
|
|
|
|
in_topic = in_topic.where("users.id <> ?", @searching_user.id) if @searching_user.present?
|
|
|
|
|
|
|
|
in_topic
|
2021-01-25 18:23:36 +08:00
|
|
|
.order("last_seen_at DESC NULLS LAST")
|
|
|
|
.limit(@limit - users.size)
|
2015-05-14 12:38:47 +08:00
|
|
|
.pluck(:id)
|
2015-11-05 06:04:37 +08:00
|
|
|
.each { |id| users << id }
|
2015-04-13 23:03:13 +08:00
|
|
|
end
|
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
return users.to_a if users.size >= @limit
|
|
|
|
|
|
|
|
# 3. in category
|
|
|
|
secure_category_id =
|
|
|
|
if @category_id
|
|
|
|
DB.query_single(<<~SQL, @category_id).first
|
|
|
|
SELECT id
|
|
|
|
FROM categories
|
|
|
|
WHERE read_restricted
|
|
|
|
AND id = ?
|
|
|
|
SQL
|
|
|
|
elsif @topic_id
|
|
|
|
DB.query_single(<<~SQL, @topic_id).first
|
|
|
|
SELECT id
|
|
|
|
FROM categories
|
|
|
|
WHERE read_restricted
|
|
|
|
AND id IN (SELECT category_id FROM topics WHERE id = ?)
|
|
|
|
SQL
|
|
|
|
end
|
2019-08-06 15:57:45 +08:00
|
|
|
|
|
|
|
if secure_category_id
|
2019-10-24 20:23:19 +08:00
|
|
|
category_groups = Group.where(<<~SQL, secure_category_id, MAX_SIZE_PRIORITY_MENTION)
|
|
|
|
groups.id IN (
|
2021-01-25 18:23:36 +08:00
|
|
|
SELECT group_id
|
|
|
|
FROM category_groups
|
|
|
|
JOIN groups g ON group_id = g.id
|
|
|
|
WHERE category_id = ?
|
|
|
|
AND user_count < ?
|
2019-10-24 20:23:19 +08:00
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
if @searching_user.present?
|
|
|
|
category_groups = category_groups.members_visible_groups(@searching_user)
|
|
|
|
end
|
2019-10-24 20:23:19 +08:00
|
|
|
|
|
|
|
in_category = filtered_by_term_users.where(<<~SQL, category_groups.pluck(:id))
|
2019-08-06 15:57:45 +08:00
|
|
|
users.id IN (
|
|
|
|
SELECT gu.user_id
|
2021-01-25 18:23:36 +08:00
|
|
|
FROM group_users gu
|
|
|
|
WHERE group_id IN (?)
|
|
|
|
LIMIT 200
|
2019-08-06 15:57:45 +08:00
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
if @searching_user.present?
|
|
|
|
in_category = in_category.where("users.id <> ?", @searching_user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
in_category
|
2021-01-25 18:23:36 +08:00
|
|
|
.order("last_seen_at DESC NULLS LAST")
|
|
|
|
.limit(@limit - users.size)
|
2019-08-06 15:57:45 +08:00
|
|
|
.pluck(:id)
|
|
|
|
.each { |id| users << id }
|
|
|
|
end
|
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
return users.to_a if users.size >= @limit
|
|
|
|
|
2019-08-06 15:57:45 +08:00
|
|
|
# 4. global matches
|
|
|
|
if @term.present?
|
2021-01-25 18:23:36 +08:00
|
|
|
filtered_by_term_users
|
|
|
|
.order("last_seen_at DESC NULLS LAST")
|
|
|
|
.limit(@limit - users.size)
|
2019-02-20 10:33:56 +08:00
|
|
|
.pluck(:id)
|
|
|
|
.each { |id| users << id }
|
|
|
|
end
|
2015-05-14 12:38:47 +08:00
|
|
|
|
2023-02-03 00:35:04 +08:00
|
|
|
return users.to_a if users.size >= @limit
|
|
|
|
|
2021-07-21 21:14:53 +08:00
|
|
|
# 5. last seen users (for search auto-suggestions)
|
|
|
|
if @last_seen_users
|
|
|
|
scoped_users
|
|
|
|
.order("last_seen_at DESC NULLS LAST")
|
|
|
|
.limit(@limit - users.size)
|
|
|
|
.pluck(:id)
|
|
|
|
.each { |id| users << id }
|
|
|
|
end
|
|
|
|
|
2015-05-14 12:38:47 +08:00
|
|
|
users.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def search
|
|
|
|
ids = search_ids
|
2023-08-22 01:23:42 +08:00
|
|
|
ids = DiscoursePluginRegistry.apply_modifier(:user_search_ids, ids)
|
|
|
|
return User.none if ids.empty?
|
2015-05-14 12:38:47 +08:00
|
|
|
|
2022-08-09 18:54:33 +08:00
|
|
|
results =
|
|
|
|
User.joins(
|
|
|
|
"JOIN (SELECT unnest uid, row_number() OVER () AS rn
|
2015-05-14 12:38:47 +08:00
|
|
|
FROM unnest('{#{ids.join(",")}}'::int[])
|
|
|
|
) x on uid = users.id",
|
|
|
|
).order("rn")
|
2022-08-09 18:54:33 +08:00
|
|
|
|
2024-02-26 21:40:48 +08:00
|
|
|
results = results.includes(:user_option)
|
2022-08-09 18:54:33 +08:00
|
|
|
results = results.includes(:user_status) if SiteSetting.enable_user_status
|
|
|
|
|
|
|
|
results
|
2013-02-07 17:34:49 +08:00
|
|
|
end
|
2013-02-10 20:37:24 +08:00
|
|
|
end
|