debian-mirror-gitlab/app/controllers/autocomplete_controller.rb

68 lines
1.8 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
class AutocompleteController < ApplicationController
2018-03-17 18:26:18 +05:30
AWARD_EMOJI_MAX = 100
skip_before_action :authenticate_user!, only: [:users, :award_emojis]
2016-09-13 17:45:13 +05:30
before_action :load_project, only: [:users]
2018-03-17 18:26:18 +05:30
before_action :load_group, only: [:users]
2015-09-11 14:41:01 +05:30
2015-04-26 12:48:37 +05:30
def users
2018-03-17 18:26:18 +05:30
@users = AutocompleteUsersFinder.new(params: params, current_user: current_user, project: @project, group: @group).execute
2015-09-11 14:41:01 +05:30
2018-03-17 18:26:18 +05:30
render json: UserSerializer.new.represent(@users)
2015-04-26 12:48:37 +05:30
end
def user
@user = User.find(params[:id])
2018-03-17 18:26:18 +05:30
render json: UserSerializer.new.represent(@user)
2015-04-26 12:48:37 +05:30
end
2015-12-23 02:04:40 +05:30
def projects
project = Project.find_by_id(params[:project_id])
2016-09-13 17:45:13 +05:30
projects = projects_finder.execute(project, search: params[:search], offset_id: params[:offset_id])
render json: projects.to_json(only: [:id, :name_with_namespace], methods: :name_with_namespace)
end
2018-03-17 18:26:18 +05:30
def award_emojis
emoji_with_count = AwardEmoji
.limit(AWARD_EMOJI_MAX)
.where(user: current_user)
.group(:name)
.order('count_all DESC, name ASC')
.count
# Transform from hash to array to guarantee json order
# e.g. { 'thumbsup' => 2, 'thumbsdown' = 1 }
# => [{ name: 'thumbsup' }, { name: 'thumbsdown' }]
render json: emoji_with_count.map { |k, v| { name: k } }
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
private
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def load_group
@group ||= begin
if @project.blank? && params[:group_id].present?
2015-12-23 02:04:40 +05:30
group = Group.find(params[:group_id])
return render_404 unless can?(current_user, :read_group, group)
2018-03-17 18:26:18 +05:30
group
2015-12-23 02:04:40 +05:30
end
2018-03-17 18:26:18 +05:30
end
2015-12-23 02:04:40 +05:30
end
2016-09-13 17:45:13 +05:30
def load_project
@project ||= begin
if params[:project_id].present?
project = Project.find(params[:project_id])
return render_404 unless can?(current_user, :read_project, project)
2018-03-17 18:26:18 +05:30
2016-09-13 17:45:13 +05:30
project
end
end
end
def projects_finder
MoveToProjectFinder.new(current_user)
end
2015-04-26 12:48:37 +05:30
end