2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
class DirectoryItemsController < ApplicationController
|
|
|
|
PAGE_SIZE = 50
|
|
|
|
|
|
|
|
def index
|
2015-03-26 11:26:19 -04:00
|
|
|
unless SiteSetting.enable_user_directory?
|
|
|
|
raise Discourse::InvalidAccess.new(:enable_user_directory)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2015-03-26 11:26:19 -04:00
|
|
|
|
2015-03-19 11:48:16 -04:00
|
|
|
period = params.require(:period)
|
|
|
|
period_type = DirectoryItem.period_types[period.to_sym]
|
2015-03-16 15:14:33 -04:00
|
|
|
raise Discourse::InvalidAccess.new(:period_type) unless period_type
|
2021-06-23 11:45:18 -04:00
|
|
|
result = DirectoryItem.where(period_type: period_type).includes(user: :user_custom_fields)
|
2015-03-16 15:14:33 -04:00
|
|
|
|
2018-05-16 10:20:17 -04:00
|
|
|
if params[:group]
|
2020-03-09 16:04:05 -04:00
|
|
|
group = Group.find_by(name: params[:group])
|
|
|
|
raise Discourse::InvalidParameters.new(:group) if group.blank?
|
|
|
|
guardian.ensure_can_see!(group)
|
|
|
|
guardian.ensure_can_see_group_members!(group)
|
|
|
|
|
|
|
|
result = result.includes(user: :groups).where(users: { groups: { id: group.id } })
|
2018-12-06 06:18:52 -05:00
|
|
|
else
|
|
|
|
result = result.includes(user: :primary_group)
|
2018-05-16 10:20:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if params[:exclude_usernames]
|
2018-05-16 10:35:04 -04:00
|
|
|
result =
|
|
|
|
result
|
|
|
|
.references(:user)
|
|
|
|
.where.not(users: { username: params[:exclude_usernames].split(",") })
|
2018-05-16 10:20:17 -04:00
|
|
|
end
|
|
|
|
|
2021-06-22 14:00:04 -04:00
|
|
|
order = params[:order] || DirectoryColumn.automatic_column_names.first
|
2020-08-26 10:14:20 -04:00
|
|
|
dir = params[:asc] ? "ASC" : "DESC"
|
2021-06-23 15:55:17 -04:00
|
|
|
active_directory_column_names = DirectoryColumn.active_column_names
|
|
|
|
if active_directory_column_names.include?(order.to_sym)
|
2020-11-27 13:12:49 -05:00
|
|
|
result = result.order("directory_items.#{order} #{dir}, directory_items.id")
|
2020-08-26 10:14:20 -04:00
|
|
|
elsif params[:order] === "username"
|
2020-11-27 13:12:49 -05:00
|
|
|
result = result.order("users.#{order} #{dir}, directory_items.id")
|
2021-06-07 13:34:01 -04:00
|
|
|
else
|
2021-06-22 14:00:04 -04:00
|
|
|
# Ordering by user field value
|
2021-06-07 13:34:01 -04:00
|
|
|
user_field = UserField.find_by(name: params[:order])
|
|
|
|
if user_field
|
|
|
|
result =
|
|
|
|
result
|
2022-02-15 21:27:35 -05:00
|
|
|
.references(:user)
|
2021-06-07 13:34:01 -04:00
|
|
|
.joins(
|
|
|
|
"LEFT OUTER JOIN user_custom_fields ON user_custom_fields.user_id = users.id AND user_custom_fields.name = 'user_field_#{user_field.id}'",
|
|
|
|
)
|
|
|
|
.order(
|
|
|
|
"user_custom_fields.name = 'user_field_#{user_field.id}' ASC, user_custom_fields.value #{dir}",
|
|
|
|
)
|
|
|
|
end
|
2015-03-16 15:14:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
result = result.includes(:user_stat) if period_type == DirectoryItem.period_types[:all]
|
|
|
|
page = params[:page].to_i
|
2015-03-19 15:31:29 -04:00
|
|
|
|
2015-03-19 18:07:31 -04:00
|
|
|
user_ids = nil
|
|
|
|
if params[:name].present?
|
2017-11-18 19:17:31 -05:00
|
|
|
user_ids = UserSearch.new(params[:name], include_staged_users: true).search.pluck(:id)
|
2015-03-19 18:07:31 -04:00
|
|
|
if user_ids.present?
|
|
|
|
# Add the current user if we have at least one other match
|
2017-08-23 18:54:51 -04:00
|
|
|
user_ids << current_user.id if current_user && result.dup.where(user_id: user_ids).exists?
|
2015-03-19 18:07:31 -04:00
|
|
|
result = result.where(user_id: user_ids)
|
|
|
|
else
|
|
|
|
result = result.where("false")
|
|
|
|
end
|
|
|
|
end
|
2015-03-19 15:31:29 -04:00
|
|
|
|
2015-12-29 23:00:00 -05:00
|
|
|
if params[:username]
|
2019-10-21 06:32:27 -04:00
|
|
|
user_id = User.where(username_lower: params[:username].to_s.downcase).pluck_first(:id)
|
2015-12-29 23:00:00 -05:00
|
|
|
if user_id
|
|
|
|
result = result.where(user_id: user_id)
|
|
|
|
else
|
|
|
|
result = result.where("false")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-14 13:13:58 -04:00
|
|
|
result_count = result.count
|
2015-03-19 18:07:31 -04:00
|
|
|
result = result.limit(PAGE_SIZE).offset(PAGE_SIZE * page).to_a
|
2015-03-16 15:14:33 -04:00
|
|
|
|
2021-06-29 15:43:38 -04:00
|
|
|
more_params = params.slice(:period, :order, :asc, :group, :user_field_ids).permit!
|
2015-03-16 15:14:33 -04:00
|
|
|
more_params[:page] = page + 1
|
2020-08-11 14:35:23 -04:00
|
|
|
load_more_uri = URI.parse(directory_items_path(more_params))
|
|
|
|
load_more_directory_items_json = "#{load_more_uri.path}.json?#{load_more_uri.query}"
|
2015-03-16 15:14:33 -04:00
|
|
|
|
2015-03-24 16:19:15 -04:00
|
|
|
# Put yourself at the top of the first page
|
2020-11-22 18:22:14 -05:00
|
|
|
if result.present? && current_user.present? && page == 0 && !params[:group].present?
|
2015-04-02 14:51:49 -04:00
|
|
|
position = result.index { |r| r.user_id == current_user.id }
|
|
|
|
|
|
|
|
# Don't show the record unless you're not in the top positions already
|
|
|
|
if (position || 10) >= 10
|
|
|
|
your_item = DirectoryItem.where(period_type: period_type, user_id: current_user.id).first
|
|
|
|
result.insert(0, your_item) if your_item
|
|
|
|
end
|
2015-03-24 16:19:15 -04:00
|
|
|
end
|
2015-03-16 15:14:33 -04:00
|
|
|
|
2020-03-30 14:27:49 -04:00
|
|
|
last_updated_at = DirectoryItem.last_updated_at(period_type)
|
2021-06-07 13:34:01 -04:00
|
|
|
|
|
|
|
serializer_opts = {}
|
|
|
|
if params[:user_field_ids]
|
2021-06-23 11:45:18 -04:00
|
|
|
serializer_opts[:user_custom_field_map] = {}
|
|
|
|
|
|
|
|
user_field_ids = params[:user_field_ids]&.split("|")&.map(&:to_i)
|
|
|
|
user_field_ids.each do |user_field_id|
|
|
|
|
serializer_opts[:user_custom_field_map][
|
|
|
|
"#{User::USER_FIELD_PREFIX}#{user_field_id}"
|
|
|
|
] = user_field_id
|
|
|
|
end
|
2021-06-07 13:34:01 -04:00
|
|
|
end
|
|
|
|
|
2021-06-22 14:00:04 -04:00
|
|
|
if params[:plugin_column_ids]
|
|
|
|
serializer_opts[:plugin_column_ids] = params[:plugin_column_ids]&.split("|")&.map(&:to_i)
|
|
|
|
end
|
|
|
|
|
2021-06-23 15:55:17 -04:00
|
|
|
serializer_opts[:attributes] = active_directory_column_names
|
|
|
|
|
2021-06-07 13:34:01 -04:00
|
|
|
serialized = serialize_data(result, DirectoryItemSerializer, serializer_opts)
|
|
|
|
render_json_dump(
|
|
|
|
directory_items: serialized,
|
2020-04-02 22:02:50 -04:00
|
|
|
meta: {
|
|
|
|
last_updated_at: last_updated_at,
|
|
|
|
total_rows_directory_items: result_count,
|
2020-08-11 14:35:23 -04:00
|
|
|
load_more_directory_items: load_more_directory_items_json,
|
2020-04-02 22:02:50 -04:00
|
|
|
},
|
|
|
|
)
|
2015-03-16 15:14:33 -04:00
|
|
|
end
|
|
|
|
end
|