2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class AwardEmoji < ApplicationRecord
|
2019-08-31 19:57:00 +00:00
|
|
|
DOWNVOTE_NAME = "thumbsdown"
|
|
|
|
UPVOTE_NAME = "thumbsup"
|
2016-04-16 19:09:08 +00:00
|
|
|
|
|
|
|
include Participable
|
2017-04-06 10:06:36 +00:00
|
|
|
include GhostUser
|
2019-11-11 18:06:27 +00:00
|
|
|
include Importable
|
2016-04-16 19:09:08 +00:00
|
|
|
|
2017-06-02 12:29:30 +00:00
|
|
|
belongs_to :awardable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
|
2016-04-16 19:09:08 +00:00
|
|
|
belongs_to :user
|
|
|
|
|
2019-11-11 18:06:27 +00:00
|
|
|
validates :user, presence: true
|
|
|
|
validates :awardable, presence: true, unless: :importing?
|
|
|
|
|
2021-10-19 21:12:08 +00:00
|
|
|
validates :name, presence: true, 'gitlab/emoji_name': true
|
2020-04-02 15:08:01 +00:00
|
|
|
validates :name, uniqueness: { scope: [:user, :awardable_type, :awardable_id] }, unless: -> { ghost_user? || importing? }
|
2016-04-16 19:09:08 +00:00
|
|
|
|
|
|
|
participant :user
|
|
|
|
|
2022-04-19 15:08:32 +00:00
|
|
|
delegate :resource_parent, to: :awardable, allow_nil: true
|
|
|
|
|
2019-06-18 01:44:43 +00: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 19:09:08 +00:00
|
|
|
|
2021-07-08 09:09:33 +00:00
|
|
|
after_save :expire_cache
|
|
|
|
after_destroy :expire_cache
|
2017-07-17 18:36:29 +00:00
|
|
|
|
2017-01-23 20:40:25 +00:00
|
|
|
class << self
|
|
|
|
def votes_for_collection(ids, type)
|
2017-06-21 13:48:12 +00: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 20:40:25 +00:00
|
|
|
end
|
2018-07-30 15:45:49 +00: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 20:40:25 +00:00
|
|
|
end
|
|
|
|
|
2016-04-16 19:09:08 +00:00
|
|
|
def downvote?
|
|
|
|
self.name == DOWNVOTE_NAME
|
|
|
|
end
|
|
|
|
|
|
|
|
def upvote?
|
|
|
|
self.name == UPVOTE_NAME
|
|
|
|
end
|
2017-07-17 18:36:29 +00:00
|
|
|
|
2022-04-02 12:08:25 +00:00
|
|
|
def url
|
2022-04-19 15:08:32 +00:00
|
|
|
return if TanukiEmoji.find_by_alpha_code(name)
|
|
|
|
|
|
|
|
CustomEmoji.for_resource(resource_parent).by_name(name).select(:url).first&.url
|
2022-04-02 12:08:25 +00:00
|
|
|
end
|
|
|
|
|
2021-07-08 09:09:33 +00:00
|
|
|
def expire_cache
|
|
|
|
awardable.try(:bump_updated_at)
|
2022-05-25 12:08:10 +00:00
|
|
|
awardable.expire_etag_cache if awardable.is_a?(Note)
|
2021-07-14 12:09:23 +00:00
|
|
|
awardable.try(:update_upvotes_count) if upvote?
|
2021-07-12 12:09:39 +00:00
|
|
|
end
|
2022-10-03 09:09:43 +00:00
|
|
|
|
|
|
|
def to_ability_name
|
|
|
|
'emoji'
|
|
|
|
end
|
2016-04-16 19:09:08 +00:00
|
|
|
end
|