debian-mirror-gitlab/app/services/award_emojis/add_service.rb

50 lines
1.2 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# 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-04-22 19:07:51 +05:30
after_create(award)
2019-12-04 20:38:33 +05:30
success(award: award)
else
error(award.errors.full_messages, award: award)
end
end
private
2020-04-22 19:07:51 +05:30
def after_create(award)
TodoService.new.new_award_emoji(todoable, current_user) if todoable
end
2019-12-04 20:38:33 +05:30
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-22 19:07:51 +05:30
else
# No todos for all other awardables
2019-12-04 20:38:33 +05:30
nil
end
end
end
end
end
2020-04-22 19:07:51 +05:30
2021-06-08 01:23:25 +05:30
AwardEmojis::AddService.prepend_mod_with('AwardEmojis::AddService')