BigW Consortium Gitlab

autocomplete_controller.rb 2.44 KB
Newer Older
1
class AutocompleteController < ApplicationController
2
  skip_before_action :authenticate_user!, only: [:users]
3
  before_action :load_project, only: [:users]
4
  before_action :find_users, only: [:users]
5

6
  def users
7
    @users ||= User.none
8
    @users = @users.search(params[:search]) if params[:search].present?
9
    @users = @users.where.not(id: params[:skip_users]) if params[:skip_users].present?
10
    @users = @users.active
11
    @users = @users.reorder(:name)
12
    @users = @users.page(params[:page]).per(params[:per_page])
13

14
    if params[:todo_filter].present? && current_user
15 16 17
      @users = @users.todo_authors(current_user.id, params[:todo_state_filter])
    end

18
    if params[:search].blank?
19
      # Include current user if available to filter by "Me"
20
      if params[:current_user].present? && current_user
21
        @users = [current_user, *@users].uniq
22
      end
23

24 25
      if params[:author_id].present?
        author = User.find_by_id(params[:author_id])
26
        @users = [author, *@users].uniq if author
27
      end
28 29
    end

30 31 32 33 34 35 36
    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
37

38 39
  def projects
    project = Project.find_by_id(params[:project_id])
40
    projects = projects_finder.execute(project, search: params[:search], offset_id: params[:offset_id])
41

Phil Hughes committed
42 43
    no_project = {
      id: 0,
44
      name_with_namespace: 'No project'
Phil Hughes committed
45
    }
46
    projects.unshift(no_project) unless params[:offset_id].present?
47 48 49 50

    render json: projects.to_json(only: [:id, :name_with_namespace], methods: :name_with_namespace)
  end

51 52 53 54
  private

  def find_users
    @users =
55
      if @project
56
        user_ids = @project.team.users.pluck(:id)
57 58 59 60 61 62

        if params[:author_id].present?
          user_ids << params[:author_id]
        end

        User.where(id: user_ids)
63 64 65 66 67 68 69 70 71 72 73
      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
74 75 76 77 78 79 80 81 82 83

  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
84 85 86 87

  def projects_finder
    MoveToProjectFinder.new(current_user)
  end
88
end