BigW Consortium Gitlab

award_emoji.rb 3.83 KB
Newer Older
1 2 3
module API
  class AwardEmoji < Grape::API
    before { authenticate! }
4
    AWARDABLES = %w[issue merge_request snippet]
5 6 7

    resource :projects do
      AWARDABLES.each do |awardable_type|
8 9
        awardable_string = awardable_type.pluralize
        awardable_id_string = "#{awardable_type}_id"
10

11 12 13 14 15
        params do
          requires :id, type: String, desc: 'The ID of a project'
          requires :"#{awardable_id_string}", type: Integer, desc: "The ID of an Issue, Merge Request or Snippet"
        end

16 17 18
        [ ":id/#{awardable_string}/:#{awardable_id_string}/award_emoji",
          ":id/#{awardable_string}/:#{awardable_id_string}/notes/:note_id/award_emoji"
        ].each do |endpoint|
19 20 21 22 23

          desc 'Get a list of project +awardable+ award emoji' do
            detail 'This feature was introduced in 8.9'
            success Entities::AwardEmoji
          end
24 25 26 27 28 29 30
          get endpoint do
            if can_read_awardable?
              awards = paginate(awardable.award_emoji)
              present awards, with: Entities::AwardEmoji
            else
              not_found!("Award Emoji")
            end
31 32
          end

33 34 35 36 37 38 39
          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
40 41 42 43 44 45
          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
46 47
          end

48 49 50 51 52 53 54
          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
55
          post endpoint do
56
            not_found!('Award Emoji') unless can_read_awardable? && can_award_awardable?
57

58
            award = awardable.create_award_emoji(params[:name], current_user)
59

60
            if award.persisted?
61 62 63 64
              present award, with: Entities::AwardEmoji
            else
              not_found!("Award Emoji #{award.errors.messages}")
            end
65 66
          end

67 68 69 70 71 72 73
          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
74 75
          delete "#{endpoint}/:award_id" do
            award = awardable.award_emoji.find(params[:award_id])
76

77
            unauthorized! unless award.user == current_user || current_user.admin?
78

79 80 81
            award.destroy
            present award, with: Entities::AwardEmoji
          end
82 83 84
        end
      end
    end
85

86 87
    helpers do
      def can_read_awardable?
88
        can?(current_user, read_ability(awardable), awardable)
89
      end
90

91 92 93 94
      def can_award_awardable?
        awardable.user_can_award?(current_user, params[:name])
      end

95 96 97 98
      def awardable
        @awardable ||=
          begin
            if params.include?(:note_id)
99
              note_id = params.delete(:note_id)
100 101 102 103 104 105

              awardable.notes.find(note_id)
            elsif params.include?(:issue_id)
              user_project.issues.find(params[:issue_id])
            elsif params.include?(:merge_request_id)
              user_project.merge_requests.find(params[:merge_request_id])
106
            else
107
              user_project.snippets.find(params[:snippet_id])
108 109 110 111
            end
          end
      end

112
      def read_ability(awardable)
113 114
        case awardable
        when Note
115 116 117
          read_ability(awardable.noteable)
        else
          :"read_#{awardable.class.to_s.underscore}"
118 119
        end
      end
120 121 122
    end
  end
end