2016-06-22 15:30:34 +05:30
|
|
|
module API
|
|
|
|
class AwardEmoji < Grape::API
|
2017-08-17 22:00:37 +05:30
|
|
|
include PaginationParams
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
before { authenticate! }
|
|
|
|
AWARDABLES = [
|
|
|
|
{ type: 'issue', find_by: :iid },
|
|
|
|
{ type: 'merge_request', find_by: :iid },
|
|
|
|
{ type: 'snippet', find_by: :id }
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
|
2017-08-17 22:00:37 +05:30
|
|
|
AWARDABLES.each do |awardable_params|
|
|
|
|
awardable_string = awardable_params[:type].pluralize
|
|
|
|
awardable_id_string = "#{awardable_params[:type]}_#{awardable_params[:find_by]}"
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
params do
|
|
|
|
requires :"#{awardable_id_string}", type: Integer, desc: "The ID of an Issue, Merge Request or Snippet"
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
[
|
|
|
|
":id/#{awardable_string}/:#{awardable_id_string}/award_emoji",
|
2016-06-22 15:30:34 +05:30
|
|
|
":id/#{awardable_string}/:#{awardable_id_string}/notes/:note_id/award_emoji"
|
|
|
|
].each do |endpoint|
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
desc 'Get a list of project +awardable+ award emoji' do
|
|
|
|
detail 'This feature was introduced in 8.9'
|
|
|
|
success Entities::AwardEmoji
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
get endpoint do
|
|
|
|
if can_read_awardable?
|
2017-08-17 22:00:37 +05:30
|
|
|
awards = awardable.award_emoji
|
|
|
|
present paginate(awards), with: Entities::AwardEmoji
|
2016-06-22 15:30:34 +05:30
|
|
|
else
|
|
|
|
not_found!("Award Emoji")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
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-22 15:30:34 +05:30
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
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-22 15:30:34 +05:30
|
|
|
post endpoint do
|
2016-09-29 09:46:39 +05:30
|
|
|
not_found!('Award Emoji') unless can_read_awardable? && can_award_awardable?
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
award = awardable.create_award_emoji(params[:name], current_user)
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
if award.persisted?
|
2016-06-22 15:30:34 +05:30
|
|
|
present award, with: Entities::AwardEmoji
|
|
|
|
else
|
|
|
|
not_found!("Award Emoji #{award.errors.messages}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
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-22 15:30:34 +05:30
|
|
|
delete "#{endpoint}/:award_id" do
|
|
|
|
award = awardable.award_emoji.find(params[:award_id])
|
|
|
|
|
|
|
|
unauthorized! unless award.user == current_user || current_user.admin?
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
destroy_conditionally!(award)
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def can_read_awardable?
|
2016-09-29 09:46:39 +05:30
|
|
|
can?(current_user, read_ability(awardable), awardable)
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def can_award_awardable?
|
|
|
|
awardable.user_can_award?(current_user, params[:name])
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def awardable
|
|
|
|
@awardable ||=
|
|
|
|
begin
|
|
|
|
if params.include?(:note_id)
|
2016-09-29 09:46:39 +05:30
|
|
|
note_id = params.delete(:note_id)
|
|
|
|
|
|
|
|
awardable.notes.find(note_id)
|
2017-08-17 22:00:37 +05:30
|
|
|
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-22 15:30:34 +05:30
|
|
|
else
|
2016-09-29 09:46:39 +05:30
|
|
|
user_project.snippets.find(params[:snippet_id])
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def read_ability(awardable)
|
|
|
|
case awardable
|
|
|
|
when Note
|
|
|
|
read_ability(awardable.noteable)
|
2016-06-22 15:30:34 +05:30
|
|
|
else
|
2016-09-29 09:46:39 +05:30
|
|
|
:"read_#{awardable.class.to_s.underscore}"
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|