BigW Consortium Gitlab

toggle_award_emoji.rb 801 Bytes
Newer Older
1 2 3 4
module ToggleAwardEmoji
  extend ActiveSupport::Concern

  def toggle_award_emoji
5
    authenticate_user!
6
    name = params.require(:name)
ZJ van de Weg committed
7

8 9
    if awardable.user_can_award?(current_user, name)
      awardable.toggle_award_emoji(name, current_user)
10

11 12
      todoable = to_todoable(awardable)
      TodoService.new.new_award_emoji(todoable, current_user) if todoable
13

14 15 16 17
      render json: { ok: true }
    else
      render json: { ok: false }
    end
18 19 20 21
  end

  private

22 23 24
  def to_todoable(awardable)
    case awardable
    when Note
25 26
      # we don't create todos for personal snippet comments for now
      awardable.for_personal_snippet? ? nil : awardable.noteable
27
    when MergeRequest, Issue
28
      awardable
29 30
    when Snippet
      nil
31 32 33
    end
  end

34 35 36 37
  def awardable
    raise NotImplementedError
  end
end