2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-16 14:14:33 -05:00
|
|
|
class DirectoryItemsController < ApplicationController
|
|
|
|
PAGE_SIZE = 50
|
|
|
|
|
|
|
|
def index
|
2015-03-26 10:26:19 -05:00
|
|
|
raise Discourse::InvalidAccess.new(:enable_user_directory) unless SiteSetting.enable_user_directory?
|
|
|
|
|
2015-03-19 10:48:16 -05:00
|
|
|
period = params.require(:period)
|
|
|
|
period_type = DirectoryItem.period_types[period.to_sym]
|
2015-03-16 14:14:33 -05:00
|
|
|
raise Discourse::InvalidAccess.new(:period_type) unless period_type
|
|
|
|
result = DirectoryItem.where(period_type: period_type).includes(:user)
|
|
|
|
|
2018-05-16 09:20:17 -05:00
|
|
|
if params[:group]
|
2020-03-09 15:04:05 -05: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 05:18:52 -06:00
|
|
|
else
|
|
|
|
result = result.includes(user: :primary_group)
|
2018-05-16 09:20:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if params[:exclude_usernames]
|
2018-05-16 09:35:04 -05:00
|
|
|
result = result.references(:user).where.not(users: { username: params[:exclude_usernames].split(",") })
|
2018-05-16 09:20:17 -05:00
|
|
|
end
|
|
|
|
|
2015-03-16 14:14:33 -05:00
|
|
|
order = params[:order] || DirectoryItem.headings.first
|
2020-08-26 09:14:20 -05:00
|
|
|
dir = params[:asc] ? 'ASC' : 'DESC'
|
2015-03-16 14:14:33 -05:00
|
|
|
if DirectoryItem.headings.include?(order.to_sym)
|
|
|
|
result = result.order("directory_items.#{order} #{dir}")
|
2020-08-26 09:14:20 -05:00
|
|
|
elsif params[:order] === 'username'
|
|
|
|
result = result.order("users.#{order} #{dir}")
|
2015-03-16 14:14:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if period_type == DirectoryItem.period_types[:all]
|
|
|
|
result = result.includes(:user_stat)
|
|
|
|
end
|
|
|
|
page = params[:page].to_i
|
2015-03-19 14:31:29 -05:00
|
|
|
|
2015-03-19 17:07:31 -05:00
|
|
|
user_ids = nil
|
|
|
|
if params[:name].present?
|
2017-11-18 18:17:31 -06:00
|
|
|
user_ids = UserSearch.new(params[:name], include_staged_users: true).search.pluck(:id)
|
2015-03-19 17:07:31 -05:00
|
|
|
if user_ids.present?
|
|
|
|
# Add the current user if we have at least one other match
|
2017-08-23 17:54:51 -05:00
|
|
|
if current_user && result.dup.where(user_id: user_ids).exists?
|
2015-03-19 17:07:31 -05:00
|
|
|
user_ids << current_user.id
|
|
|
|
end
|
|
|
|
result = result.where(user_id: user_ids)
|
|
|
|
else
|
|
|
|
result = result.where('false')
|
|
|
|
end
|
|
|
|
end
|
2015-03-19 14:31:29 -05:00
|
|
|
|
2015-12-29 22:00:00 -06:00
|
|
|
if params[:username]
|
2019-10-21 05:32:27 -05:00
|
|
|
user_id = User.where(username_lower: params[:username].to_s.downcase).pluck_first(:id)
|
2015-12-29 22:00:00 -06:00
|
|
|
if user_id
|
|
|
|
result = result.where(user_id: user_id)
|
|
|
|
else
|
|
|
|
result = result.where('false')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-14 12:13:58 -05:00
|
|
|
result_count = result.count
|
2015-03-19 17:07:31 -05:00
|
|
|
result = result.limit(PAGE_SIZE).offset(PAGE_SIZE * page).to_a
|
2015-03-16 14:14:33 -05:00
|
|
|
|
2017-08-30 23:06:56 -05:00
|
|
|
more_params = params.slice(:period, :order, :asc).permit!
|
2015-03-16 14:14:33 -05:00
|
|
|
more_params[:page] = page + 1
|
2020-08-11 13:35:23 -05: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 14:14:33 -05:00
|
|
|
|
2015-03-24 15:19:15 -05:00
|
|
|
# Put yourself at the top of the first page
|
2015-04-02 13:51:49 -05:00
|
|
|
if result.present? && current_user.present? && page == 0
|
|
|
|
|
|
|
|
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 15:19:15 -05:00
|
|
|
end
|
2015-03-16 14:14:33 -05:00
|
|
|
|
2020-03-30 13:27:49 -05:00
|
|
|
last_updated_at = DirectoryItem.last_updated_at(period_type)
|
2015-03-24 15:19:15 -05:00
|
|
|
render_json_dump(directory_items: serialize_data(result, DirectoryItemSerializer),
|
2020-04-02 21:02:50 -05:00
|
|
|
meta: {
|
|
|
|
last_updated_at: last_updated_at,
|
|
|
|
total_rows_directory_items: result_count,
|
2020-08-11 13:35:23 -05:00
|
|
|
load_more_directory_items: load_more_directory_items_json
|
2020-04-02 21:02:50 -05:00
|
|
|
}
|
|
|
|
)
|
2015-03-16 14:14:33 -05:00
|
|
|
end
|
|
|
|
end
|