2019-06-17 21:44:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AwardEmojis
|
|
|
|
class AddService < AwardEmojis::BaseService
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
def execute
|
|
|
|
unless awardable.user_can_award?(current_user)
|
|
|
|
return error('User cannot award emoji to awardable', status: :forbidden)
|
|
|
|
end
|
|
|
|
|
|
|
|
unless awardable.emoji_awardable?
|
|
|
|
return error('Awardable cannot be awarded emoji', status: :unprocessable_entity)
|
|
|
|
end
|
|
|
|
|
|
|
|
award = awardable.award_emoji.create(name: name, user: current_user)
|
|
|
|
|
|
|
|
if award.persisted?
|
2020-03-31 14:07:42 -04:00
|
|
|
after_create(award)
|
2019-06-17 21:44:43 -04:00
|
|
|
success(award: award)
|
|
|
|
else
|
|
|
|
error(award.errors.full_messages, award: award)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-31 14:07:42 -04:00
|
|
|
def after_create(award)
|
|
|
|
TodoService.new.new_award_emoji(todoable, current_user) if todoable
|
|
|
|
end
|
|
|
|
|
2019-06-17 21:44:43 -04:00
|
|
|
def todoable
|
|
|
|
strong_memoize(:todoable) do
|
|
|
|
case awardable
|
|
|
|
when Note
|
|
|
|
# We don't create todos for personal snippet comments for now
|
|
|
|
awardable.noteable unless awardable.for_personal_snippet?
|
|
|
|
when MergeRequest, Issue
|
|
|
|
awardable
|
2020-04-03 05:09:31 -04:00
|
|
|
else
|
|
|
|
# No todos for all other awardables
|
2019-06-17 21:44:43 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-31 14:07:42 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
AwardEmojis::AddService.prepend_mod_with('AwardEmojis::AddService')
|