summaryrefslogtreecommitdiff
path: root/app/controllers/autocomplete_controller.rb
blob: 01cc1ef21c61cc25e4fa2cd9242bee93e390c86f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true

class AutocompleteController < ApplicationController
  include SearchRateLimitable

  skip_before_action :authenticate_user!, only: [:users, :award_emojis, :merge_request_target_branches]
  before_action :check_search_rate_limit!, only: [:users, :projects]

  feature_category :user_profile, [:users, :user]
  feature_category :projects, [:projects]
  feature_category :team_planning, [:award_emojis]
  feature_category :code_review_workflow, [:merge_request_target_branches]
  feature_category :continuous_delivery, [:deploy_keys_with_owners]

  urgency :low, [:merge_request_target_branches, :deploy_keys_with_owners, :users]
  urgency :low, [:award_emojis]
  urgency :medium, [:projects]

  def users
    group = Autocomplete::GroupFinder
      .new(current_user, project, params)
      .execute

    users = Autocomplete::UsersFinder
      .new(params: params, current_user: current_user, project: project, group: group)
      .execute

    presented_users = UserSerializer
                        .new(params.merge({ current_user: current_user }))
                        .represent(users, project: project)

    extra_users = presented_suggested_users

    if extra_users.present?
      presented_users.reject! do |user|
        extra_users.any? { |suggested_user| suggested_user[:id] == user[:id] }
      end
      presented_users += extra_users
    end

    render json: presented_users
  end

  def user
    user = UserFinder.new(params[:id]).find_by_id!

    render json: UserSerializer.new.represent(user)
  end

  # Displays projects to use for the dropdown when moving a resource from one
  # project to another.
  def projects
    projects = Autocomplete::MoveToProjectFinder
      .new(current_user, params)
      .execute

    render json: MoveToProjectSerializer.new.represent(projects)
  end

  def award_emojis
    render json: AwardEmojis::CollectUserEmojiService.new(current_user).execute
  end

  def merge_request_target_branches
    if target_branch_params.present?
      merge_requests = MergeRequestsFinder.new(current_user, target_branch_params).execute
      target_branches = merge_requests.recent_target_branches

      render json: target_branches.map { |target_branch| { title: target_branch } }
    else
      render json: { error: _('At least one of group_id or project_id must be specified') }, status: :bad_request
    end
  end

  def deploy_keys_with_owners
    deploy_keys = Autocomplete::DeployKeysWithWriteAccessFinder
      .new(current_user, project)
      .execute

    render json: DeployKeys::BasicDeployKeySerializer.new.represent(
      deploy_keys, { with_owner: true, user: current_user }
    )
  end

  private

  def project
    @project ||= Autocomplete::ProjectFinder
      .new(current_user, params)
      .execute
  end

  def target_branch_params
    params.permit(:group_id, :project_id).select { |_, v| v.present? }
  end

  # overridden in EE
  def presented_suggested_users
    []
  end
end

AutocompleteController.prepend_mod_with('AutocompleteController')