2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-08 03:33:41 -04:00
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class AwardEmoji < ::API::Base
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2021-05-05 02:10:38 -04:00
|
|
|
helpers ::API::Helpers::AwardEmoji
|
|
|
|
|
2016-06-08 03:33:41 -04:00
|
|
|
before { authenticate! }
|
2021-05-05 02:10:38 -04:00
|
|
|
|
|
|
|
Helpers::AwardEmoji.awardables.each do |awardable_params|
|
|
|
|
resource awardable_params[:resource], requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2017-02-28 00:59:02 -05:00
|
|
|
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
|
2021-05-05 02:10:38 -04:00
|
|
|
requires :id, type: String, desc: "The ID of a #{awardable_params[:resource] == :projects ? 'project' : 'group'}"
|
|
|
|
requires :"#{awardable_id_string}", type: Integer, desc: Helpers::AwardEmoji.awardable_id_desc
|
2016-08-12 03:27:52 -04:00
|
|
|
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
|
2020-10-28 11:08:49 -04:00
|
|
|
get endpoint, feature_category: awardable_params[:feature_category] do
|
2016-06-17 09:44:38 -04:00
|
|
|
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
|
2020-10-28 11:08:49 -04:00
|
|
|
get "#{endpoint}/:award_id", feature_category: awardable_params[:feature_category] do
|
2016-06-17 09:44:38 -04:00
|
|
|
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
|
2020-10-28 11:08:49 -04:00
|
|
|
post endpoint, feature_category: awardable_params[:feature_category] 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
|
2020-10-28 11:08:49 -04:00
|
|
|
delete "#{endpoint}/:award_id", feature_category: awardable_params[:feature_category] do
|
2016-06-17 09:44:38 -04:00
|
|
|
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
|
|
|
|
|
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
|