2015-03-26 22:06:19 -04:00
|
|
|
class AutocompleteController < ApplicationController
|
2017-08-30 03:48:55 -04:00
|
|
|
AWARD_EMOJI_MAX = 100
|
|
|
|
|
|
|
|
skip_before_action :authenticate_user!, only: [:users, :award_emojis]
|
2016-08-16 05:54:55 -04:00
|
|
|
before_action :load_project, only: [:users]
|
2017-09-08 05:00:31 -04:00
|
|
|
before_action :load_group, only: [:users]
|
2015-07-10 20:36:24 -04:00
|
|
|
|
2015-03-26 22:06:19 -04:00
|
|
|
def users
|
2017-09-08 05:00:31 -04:00
|
|
|
@users = AutocompleteUsersFinder.new(params: params, current_user: current_user, project: @project, group: @group).execute
|
2015-08-21 00:33:00 -04:00
|
|
|
|
2015-03-26 22:06:19 -04:00
|
|
|
render json: @users, only: [:name, :username, :id], methods: [:avatar_url]
|
|
|
|
end
|
|
|
|
|
|
|
|
def user
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
render json: @user, only: [:name, :username, :id], methods: [:avatar_url]
|
|
|
|
end
|
2015-11-19 08:49:35 -05:00
|
|
|
|
2016-05-16 10:24:51 -04:00
|
|
|
def projects
|
|
|
|
project = Project.find_by_id(params[:project_id])
|
2016-08-05 09:29:20 -04:00
|
|
|
projects = projects_finder.execute(project, search: params[:search], offset_id: params[:offset_id])
|
2016-05-16 10:24:51 -04:00
|
|
|
|
|
|
|
render json: projects.to_json(only: [:id, :name_with_namespace], methods: :name_with_namespace)
|
|
|
|
end
|
|
|
|
|
2017-08-30 03:48:55 -04:00
|
|
|
def award_emojis
|
|
|
|
emoji_with_count = AwardEmoji
|
|
|
|
.limit(AWARD_EMOJI_MAX)
|
|
|
|
.where(user: current_user)
|
|
|
|
.group(:name)
|
2017-08-30 06:30:12 -04:00
|
|
|
.order('count_all DESC, name ASC')
|
2017-08-30 03:48:55 -04:00
|
|
|
.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
|
|
|
|
|
2015-11-19 08:49:35 -05:00
|
|
|
private
|
|
|
|
|
2017-09-08 05:00:31 -04:00
|
|
|
def load_group
|
|
|
|
@group ||= begin
|
|
|
|
if @project.blank? && params[:group_id].present?
|
2015-11-19 08:49:35 -05:00
|
|
|
group = Group.find(params[:group_id])
|
|
|
|
return render_404 unless can?(current_user, :read_group, group)
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2017-09-08 05:00:31 -04:00
|
|
|
group
|
2015-11-19 08:49:35 -05:00
|
|
|
end
|
2017-09-08 05:00:31 -04:00
|
|
|
end
|
2015-11-19 08:49:35 -05:00
|
|
|
end
|
2016-08-16 05:46:08 -04:00
|
|
|
|
|
|
|
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)
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-08-16 05:46:08 -04:00
|
|
|
project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-05 09:29:20 -04:00
|
|
|
|
|
|
|
def projects_finder
|
|
|
|
MoveToProjectFinder.new(current_user)
|
|
|
|
end
|
2015-03-26 22:06:19 -04:00
|
|
|
end
|