2015-03-26 22:06:19 -04:00
|
|
|
class AutocompleteController < ApplicationController
|
2015-07-10 20:36:24 -04:00
|
|
|
skip_before_action :authenticate_user!, only: [:users]
|
2016-08-16 05:54:55 -04:00
|
|
|
before_action :load_project, only: [:users]
|
2015-11-19 08:49:35 -05:00
|
|
|
before_action :find_users, only: [:users]
|
2015-07-10 20:36:24 -04:00
|
|
|
|
2015-03-26 22:06:19 -04:00
|
|
|
def users
|
2015-07-11 09:16:59 -04:00
|
|
|
@users ||= User.none
|
2015-03-26 22:06:19 -04:00
|
|
|
@users = @users.search(params[:search]) if params[:search].present?
|
2016-07-13 10:38:21 -04:00
|
|
|
@users = @users.where.not(id: params[:skip_users]) if params[:skip_users].present?
|
2015-03-26 22:06:19 -04:00
|
|
|
@users = @users.active
|
2015-09-15 17:48:24 -04:00
|
|
|
@users = @users.reorder(:name)
|
2016-03-19 17:37:54 -04:00
|
|
|
@users = @users.page(params[:page])
|
2015-08-21 00:33:00 -04:00
|
|
|
|
2016-11-29 04:49:43 -05:00
|
|
|
if params[:todo_filter].present? && current_user
|
2016-11-03 06:22:56 -04:00
|
|
|
@users = @users.todo_authors(current_user.id, params[:todo_state_filter])
|
|
|
|
end
|
|
|
|
|
2015-11-22 03:04:20 -05:00
|
|
|
if params[:search].blank?
|
2015-08-21 00:33:00 -04:00
|
|
|
# Include current user if available to filter by "Me"
|
2016-11-03 06:22:56 -04:00
|
|
|
if params[:current_user].present? && current_user
|
2017-01-25 09:44:44 -05:00
|
|
|
@users = @users.where.not(id: current_user.id)
|
2017-01-17 10:22:48 -05:00
|
|
|
@users = [current_user, *@users]
|
2015-08-21 00:33:00 -04:00
|
|
|
end
|
2016-02-09 06:02:52 -05:00
|
|
|
|
2016-04-06 03:37:46 -04:00
|
|
|
if params[:author_id].present?
|
|
|
|
author = User.find_by_id(params[:author_id])
|
2017-01-25 09:44:44 -05:00
|
|
|
@users = [author, *@users].uniq if author
|
2016-02-09 06:02:52 -05:00
|
|
|
end
|
2015-08-21 00:33:00 -04:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2016-05-25 10:26:02 -04:00
|
|
|
no_project = {
|
|
|
|
id: 0,
|
|
|
|
name_with_namespace: 'No project',
|
|
|
|
}
|
2016-08-05 09:29:20 -04:00
|
|
|
projects.unshift(no_project) unless params[:offset_id].present?
|
2016-05-16 10:24:51 -04:00
|
|
|
|
|
|
|
render json: projects.to_json(only: [:id, :name_with_namespace], methods: :name_with_namespace)
|
|
|
|
end
|
|
|
|
|
2015-11-19 08:49:35 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def find_users
|
|
|
|
@users =
|
2016-08-16 05:46:08 -04:00
|
|
|
if @project
|
2016-11-18 11:10:48 -05:00
|
|
|
user_ids = @project.team.users.pluck(:id)
|
2016-11-17 06:13:39 -05:00
|
|
|
|
|
|
|
if params[:author_id].present?
|
|
|
|
user_ids << params[:author_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
User.where(id: user_ids)
|
2015-11-19 08:49:35 -05:00
|
|
|
elsif params[:group_id].present?
|
|
|
|
group = Group.find(params[:group_id])
|
|
|
|
return render_404 unless can?(current_user, :read_group, group)
|
|
|
|
|
|
|
|
group.users
|
|
|
|
elsif current_user
|
|
|
|
User.all
|
|
|
|
else
|
|
|
|
User.none
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
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
|