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?
|
|
|
|
|
2021-10-19 17:12:08 -04:00
|
|
|
validates :name, presence: true, 'gitlab/emoji_name': true
|
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
|
|
|
|
|
2022-04-19 11:08:32 -04:00
|
|
|
delegate :resource_parent, to: :awardable, allow_nil: true
|
|
|
|
|
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
|
|
|
|
2021-07-08 05:09:33 -04:00
|
|
|
after_save :expire_cache
|
|
|
|
after_destroy :expire_cache
|
2017-07-17 14:36:29 -04:00
|
|
|
|
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
|
|
|
|
2022-04-02 08:08:25 -04:00
|
|
|
def url
|
2022-04-19 11:08:32 -04:00
|
|
|
return if TanukiEmoji.find_by_alpha_code(name)
|
|
|
|
|
|
|
|
CustomEmoji.for_resource(resource_parent).by_name(name).select(:url).first&.url
|
2022-04-02 08:08:25 -04:00
|
|
|
end
|
|
|
|
|
2021-07-08 05:09:33 -04:00
|
|
|
def expire_cache
|
|
|
|
awardable.try(:bump_updated_at)
|
2022-05-25 08:08:10 -04:00
|
|
|
awardable.expire_etag_cache if awardable.is_a?(Note)
|
2021-07-14 08:09:23 -04:00
|
|
|
awardable.try(:update_upvotes_count) if upvote?
|
2021-07-12 08:09:39 -04:00
|
|
|
end
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|