2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class AwardEmoji < ApplicationRecord
|
2019-08-31 15:57:00 -04:00
|
|
|
DOWNVOTE_NAME = "thumbsdown"
|
|
|
|
UPVOTE_NAME = "thumbsup"
|
2016-04-16 15:09:08 -04:00
|
|
|
|
|
|
|
include Participable
|
2017-04-06 06:06:36 -04:00
|
|
|
include GhostUser
|
2019-11-11 13:06:27 -05:00
|
|
|
include Importable
|
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
|
|
|
|
|
2019-11-11 13:06:27 -05:00
|
|
|
validates :user, presence: true
|
|
|
|
validates :awardable, presence: true, unless: :importing?
|
|
|
|
|
2016-06-25 17:48:12 -04:00
|
|
|
validates :name, presence: true, inclusion: { in: Gitlab::Emoji.emojis_names }
|
2020-04-02 11:08:01 -04:00
|
|
|
validates :name, uniqueness: { scope: [:user, :awardable_type, :awardable_id] }, unless: -> { ghost_user? || importing? }
|
2016-04-16 15:09:08 -04:00
|
|
|
|
|
|
|
participant :user
|
|
|
|
|
2019-06-17 21:44:43 -04:00
|
|
|
scope :downvotes, -> { named(DOWNVOTE_NAME) }
|
|
|
|
scope :upvotes, -> { named(UPVOTE_NAME) }
|
|
|
|
scope :named, -> (names) { where(name: names) }
|
|
|
|
scope :awarded_by, -> (users) { where(user: users) }
|
2016-04-16 15:09:08 -04:00
|
|
|
|
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
|