2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-16 15:09:08 -04:00
|
|
|
class AwardEmoji < ActiveRecord::Base
|
|
|
|
DOWNVOTE_NAME = "thumbsdown".freeze
|
|
|
|
UPVOTE_NAME = "thumbsup".freeze
|
|
|
|
|
|
|
|
include Participable
|
2017-04-06 06:06:36 -04:00
|
|
|
include GhostUser
|
2016-04-16 15:09:08 -04:00
|
|
|
|
2017-06-02 08:29:30 -04:00
|
|
|
belongs_to :awardable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2016-04-16 15:09:08 -04:00
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :awardable, :user, presence: true
|
2016-06-25 17:48:12 -04:00
|
|
|
validates :name, presence: true, inclusion: { in: Gitlab::Emoji.emojis_names }
|
2017-04-06 06:06:36 -04:00
|
|
|
validates :name, uniqueness: { scope: [:user, :awardable_type, :awardable_id] }, unless: :ghost_user?
|
2016-04-16 15:09:08 -04:00
|
|
|
|
|
|
|
participant :user
|
|
|
|
|
|
|
|
scope :downvotes, -> { where(name: DOWNVOTE_NAME) }
|
|
|
|
scope :upvotes, -> { where(name: UPVOTE_NAME) }
|
|
|
|
|
2017-07-17 14:36:29 -04:00
|
|
|
after_save :expire_etag_cache
|
|
|
|
after_destroy :expire_etag_cache
|
|
|
|
|
2017-01-23 15:40:25 -05:00
|
|
|
class << self
|
|
|
|
def votes_for_collection(ids, type)
|
2017-06-21 09:48:12 -04:00
|
|
|
select('name', 'awardable_id', 'COUNT(*) as count')
|
|
|
|
.where('name IN (?) AND awardable_type = ? AND awardable_id IN (?)', [DOWNVOTE_NAME, UPVOTE_NAME], type, ids)
|
|
|
|
.group('name', 'awardable_id')
|
2017-01-23 15:40:25 -05:00
|
|
|
end
|
2018-07-30 11:45:49 -04:00
|
|
|
|
|
|
|
# Returns the top 100 emoji awarded by the given user.
|
|
|
|
#
|
|
|
|
# The returned value is a Hash mapping emoji names to the number of times
|
|
|
|
# they were awarded:
|
|
|
|
#
|
|
|
|
# { 'thumbsup' => 2, 'thumbsdown' => 1 }
|
|
|
|
#
|
|
|
|
# user - The User to get the awards for.
|
|
|
|
# limt - The maximum number of emoji to return.
|
|
|
|
def award_counts_for_user(user, limit = 100)
|
|
|
|
limit(limit)
|
|
|
|
.where(user: user)
|
|
|
|
.group(:name)
|
|
|
|
.order('count_all DESC, name ASC')
|
|
|
|
.count
|
|
|
|
end
|
2017-01-23 15:40:25 -05:00
|
|
|
end
|
|
|
|
|
2016-04-16 15:09:08 -04:00
|
|
|
def downvote?
|
|
|
|
self.name == DOWNVOTE_NAME
|
|
|
|
end
|
|
|
|
|
|
|
|
def upvote?
|
|
|
|
self.name == UPVOTE_NAME
|
|
|
|
end
|
2017-07-17 14:36:29 -04:00
|
|
|
|
|
|
|
def expire_etag_cache
|
2017-08-18 06:48:04 -04:00
|
|
|
awardable.try(:expire_etag_cache)
|
2017-07-17 14:36:29 -04:00
|
|
|
end
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|