summaryrefslogtreecommitdiff
path: root/app/controllers/autocomplete_controller.rb
blob: 5c3ca8e23c9c0a2cdb7fe8fe019b25d13f45bb94 (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
class AutocompleteController < ApplicationController
  skip_before_action :authenticate_user!, only: [:users]

  def users
    begin
      @users =
        if params[:project_id].present?
          project = Project.find(params[:project_id])

          if can?(current_user, :read_project, project)
            project.team.users
          end
        elsif params[:group_id]
          group = Group.find(params[:group_id])

          if can?(current_user, :read_group, group)
            group.users
          end
        elsif current_user
          User.all
        end
    rescue ActiveRecord::RecordNotFound
      if current_user
        return render json: {}, status: 404
      end
    end

    if @users.nil? && current_user.nil?
      authenticate_user!
    end

    @users ||= User.none
    @users = @users.search(params[:search]) if params[:search].present?
    @users = @users.active
    @users = @users.page(params[:page]).per(PER_PAGE)
    # Always include current user if available to filter by "Me"
    @users = User.find(@users.pluck(:id) + [current_user.id]).uniq if current_user
    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
end