2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-08 03:33:41 -04:00
|
|
|
module API
|
|
|
|
class AwardEmoji < Grape::API
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-06-08 03:33:41 -04:00
|
|
|
before { authenticate! }
|
2017-02-28 00:59:02 -05:00
|
|
|
AWARDABLES = [
|
2017-03-02 05:41:59 -05:00
|
|
|
{ type: 'issue', find_by: :iid },
|
|
|
|
{ type: 'merge_request', find_by: :iid },
|
|
|
|
{ type: 'snippet', find_by: :id }
|
2017-02-28 00:59:02 -05:00
|
|
|
].freeze
|
2016-06-08 03:33:41 -04:00
|
|
|
|
2017-03-15 14:09:24 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2018-11-08 07:18:17 -05:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2017-02-28 00:59:02 -05:00
|
|
|
AWARDABLES.each do |awardable_params|
|
|
|
|
awardable_string = awardable_params[:type].pluralize
|
2017-03-02 05:41:59 -05:00
|
|
|
awardable_id_string = "#{awardable_params[:type]}_#{awardable_params[:find_by]}"
|
2016-06-08 03:33:41 -04:00
|
|
|
|
2016-08-12 03:27:52 -04:00
|
|
|
params do
|
|
|
|
requires :"#{awardable_id_string}", type: Integer, desc: "The ID of an Issue, Merge Request or Snippet"
|
|
|
|
end
|
|
|
|
|
2017-02-22 12:35:20 -05:00
|
|
|
[
|
|
|
|
":id/#{awardable_string}/:#{awardable_id_string}/award_emoji",
|
|
|
|
":id/#{awardable_string}/:#{awardable_id_string}/notes/:note_id/award_emoji"
|
2016-06-17 09:44:38 -04:00
|
|
|
].each do |endpoint|
|
2016-08-12 03:27:52 -04:00
|
|
|
desc 'Get a list of project +awardable+ award emoji' do
|
|
|
|
detail 'This feature was introduced in 8.9'
|
|
|
|
success Entities::AwardEmoji
|
|
|
|
end
|
2016-12-04 12:11:19 -05:00
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2016-06-17 09:44:38 -04:00
|
|
|
get endpoint do
|
|
|
|
if can_read_awardable?
|
2017-01-16 23:45:07 -05:00
|
|
|
awards = awardable.award_emoji
|
|
|
|
present paginate(awards), with: Entities::AwardEmoji
|
2016-06-17 09:44:38 -04:00
|
|
|
else
|
|
|
|
not_found!("Award Emoji")
|
|
|
|
end
|
2016-06-08 03:33:41 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 03:27:52 -04:00
|
|
|
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
|
2016-06-17 09:44:38 -04:00
|
|
|
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
|
2016-06-08 03:33:41 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 03:27:52 -04:00
|
|
|
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
|
2016-06-17 09:44:38 -04:00
|
|
|
post endpoint do
|
2016-08-17 07:21:06 -04:00
|
|
|
not_found!('Award Emoji') unless can_read_awardable? && can_award_awardable?
|
2016-06-08 03:33:41 -04:00
|
|
|
|
2019-06-17 21:44:43 -04:00
|
|
|
service = AwardEmojis::AddService.new(awardable, params[:name], current_user).execute
|
2016-06-08 03:33:41 -04:00
|
|
|
|
2019-06-17 21:44:43 -04:00
|
|
|
if service[:status] == :success
|
|
|
|
present service[:award], with: Entities::AwardEmoji
|
2016-06-17 09:44:38 -04:00
|
|
|
else
|
2019-06-17 21:44:43 -04:00
|
|
|
not_found!("Award Emoji #{service[:message]}")
|
2016-06-17 09:44:38 -04:00
|
|
|
end
|
2016-06-08 03:33:41 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 03:27:52 -04:00
|
|
|
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
|
2016-06-17 09:44:38 -04:00
|
|
|
delete "#{endpoint}/:award_id" do
|
|
|
|
award = awardable.award_emoji.find(params[:award_id])
|
2016-06-08 03:33:41 -04:00
|
|
|
|
2016-06-17 09:44:38 -04:00
|
|
|
unauthorized! unless award.user == current_user || current_user.admin?
|
2016-06-08 03:33:41 -04:00
|
|
|
|
2017-03-02 07:14:13 -05:00
|
|
|
destroy_conditionally!(award)
|
2016-06-17 09:44:38 -04:00
|
|
|
end
|
2016-06-08 03:33:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-10 02:57:56 -04:00
|
|
|
|
2016-06-17 09:44:38 -04:00
|
|
|
helpers do
|
|
|
|
def can_read_awardable?
|
2016-09-07 08:48:43 -04:00
|
|
|
can?(current_user, read_ability(awardable), awardable)
|
2016-06-08 03:33:41 -04:00
|
|
|
end
|
2016-06-17 09:44:38 -04:00
|
|
|
|
2016-08-17 07:21:06 -04:00
|
|
|
def can_award_awardable?
|
2018-08-24 06:13:41 -04:00
|
|
|
awardable.user_can_award?(current_user)
|
2016-08-17 07:21:06 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-17 09:44:38 -04:00
|
|
|
def awardable
|
|
|
|
@awardable ||=
|
|
|
|
begin
|
|
|
|
if params.include?(:note_id)
|
2016-09-07 08:48:43 -04:00
|
|
|
note_id = params.delete(:note_id)
|
2016-06-30 04:56:56 -04:00
|
|
|
|
|
|
|
awardable.notes.find(note_id)
|
2017-02-28 00:59:02 -05:00
|
|
|
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])
|
2016-06-17 09:44:38 -04:00
|
|
|
else
|
2016-06-30 04:56:56 -04:00
|
|
|
user_project.snippets.find(params[:snippet_id])
|
2016-06-17 09:44:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-17 09:44:38 -04:00
|
|
|
|
2016-09-07 08:48:43 -04:00
|
|
|
def read_ability(awardable)
|
2016-06-30 04:56:56 -04:00
|
|
|
case awardable
|
|
|
|
when Note
|
2016-09-07 08:48:43 -04:00
|
|
|
read_ability(awardable.noteable)
|
2020-01-23 07:08:38 -05:00
|
|
|
when Snippet, ProjectSnippet
|
|
|
|
:read_snippet
|
2016-09-07 08:48:43 -04:00
|
|
|
else
|
|
|
|
:"read_#{awardable.class.to_s.underscore}"
|
2016-06-17 09:44:38 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-08 03:33:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|