BigW Consortium Gitlab

award_emoji.rb 4.11 KB
Newer Older
1 2
module API
  class AwardEmoji < Grape::API
3 4
    include PaginationParams

5
    before { authenticate! }
6
    AWARDABLES = [
7 8 9
      { type: 'issue', find_by: :iid },
      { type: 'merge_request', find_by: :iid },
      { type: 'snippet', find_by: :id }
10
    ].freeze
11

12 13 14 15
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
    resource :projects, requirements: { id: %r{[^/]+} } do
16 17
      AWARDABLES.each do |awardable_params|
        awardable_string = awardable_params[:type].pluralize
18
        awardable_id_string = "#{awardable_params[:type]}_#{awardable_params[:find_by]}"
19

20 21 22 23
        params do
          requires :"#{awardable_id_string}", type: Integer, desc: "The ID of an Issue, Merge Request or Snippet"
        end

24 25 26
        [
          ":id/#{awardable_string}/:#{awardable_id_string}/award_emoji",
          ":id/#{awardable_string}/:#{awardable_id_string}/notes/:note_id/award_emoji"
27
        ].each do |endpoint|
28 29 30 31 32

          desc 'Get a list of project +awardable+ award emoji' do
            detail 'This feature was introduced in 8.9'
            success Entities::AwardEmoji
          end
33 34 35
          params do
            use :pagination
          end
36 37
          get endpoint do
            if can_read_awardable?
38 39
              awards = awardable.award_emoji
              present paginate(awards), with: Entities::AwardEmoji
40 41 42
            else
              not_found!("Award Emoji")
            end
43 44
          end

45 46 47 48 49 50 51
          desc 'Get a specific award emoji' do
            detail 'This feature was introduced in 8.9'
            success Entities::AwardEmoji
          end
          params do
            requires :award_id, type: Integer, desc: 'The ID of the award'
          end
52 53 54 55 56 57
          get "#{endpoint}/:award_id" do
            if can_read_awardable?
              present awardable.award_emoji.find(params[:award_id]), with: Entities::AwardEmoji
            else
              not_found!("Award Emoji")
            end
58 59
          end

60 61 62 63 64 65 66
          desc 'Award a new Emoji' do
            detail 'This feature was introduced in 8.9'
            success Entities::AwardEmoji
          end
          params do
            requires :name, type: String, desc: 'The name of a award_emoji (without colons)'
          end
67
          post endpoint do
68
            not_found!('Award Emoji') unless can_read_awardable? && can_award_awardable?
69

70
            award = awardable.create_award_emoji(params[:name], current_user)
71

72
            if award.persisted?
73 74 75 76
              present award, with: Entities::AwardEmoji
            else
              not_found!("Award Emoji #{award.errors.messages}")
            end
77 78
          end

79 80 81 82 83 84 85
          desc 'Delete a +awardables+ award emoji' do
            detail 'This feature was introduced in 8.9'
            success Entities::AwardEmoji
          end
          params do
            requires :award_id, type: Integer, desc: 'The ID of an award emoji'
          end
86 87
          delete "#{endpoint}/:award_id" do
            award = awardable.award_emoji.find(params[:award_id])
88

89
            unauthorized! unless award.user == current_user || current_user.admin?
90

91 92
            award.destroy
          end
93 94 95
        end
      end
    end
96

97 98
    helpers do
      def can_read_awardable?
99
        can?(current_user, read_ability(awardable), awardable)
100
      end
101

102 103 104 105
      def can_award_awardable?
        awardable.user_can_award?(current_user, params[:name])
      end

106 107 108 109
      def awardable
        @awardable ||=
          begin
            if params.include?(:note_id)
110
              note_id = params.delete(:note_id)
111 112

              awardable.notes.find(note_id)
113 114 115 116
            elsif params.include?(:issue_iid)
              user_project.issues.find_by!(iid: params[:issue_iid])
            elsif params.include?(:merge_request_iid)
              user_project.merge_requests.find_by!(iid: params[:merge_request_iid])
117
            else
118
              user_project.snippets.find(params[:snippet_id])
119 120 121 122
            end
          end
      end

123
      def read_ability(awardable)
124 125
        case awardable
        when Note
126 127 128
          read_ability(awardable.noteable)
        else
          :"read_#{awardable.class.to_s.underscore}"
129 130
        end
      end
131 132 133
    end
  end
end