Class: AwardEmoji

Inherits:
ApplicationRecord show all
Includes:
EachBatch, GhostUser, Importable, Participable
Defined in:
app/models/award_emoji.rb

Constant Summary collapse

THUMBS_UP =
'thumbsup'
THUMBS_DOWN =
'thumbsdown'
UPVOTE_NAME =
THUMBS_UP
DOWNVOTE_NAME =
THUMBS_DOWN

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from Importable

#importing, #user_contributions

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GhostUser

#ghost_user?

Methods included from Participable

#participant?, #participants, #visible_participants

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.award_counts_for_user(user, limit = 100) ⇒ Object

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.



58
59
60
61
62
63
64
# File 'app/models/award_emoji.rb', line 58

def award_counts_for_user(user, limit = 100)
  limit(limit)
    .where(user: user)
    .group(:name)
    .order('count_all DESC, name ASC')
    .count
end

.votes_for_collection(ids, type) ⇒ Object



43
44
45
46
47
# File 'app/models/award_emoji.rb', line 43

def votes_for_collection(ids, type)
  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')
end

Instance Method Details

#broadcast_note_updateObject



92
93
94
95
# File 'app/models/award_emoji.rb', line 92

def broadcast_note_update
  awardable.broadcast_noteable_notes_changed
  awardable.trigger_note_subscription_update
end

#downvote?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/award_emoji.rb', line 67

def downvote?
  name == DOWNVOTE_NAME
end

#expire_cacheObject



87
88
89
90
# File 'app/models/award_emoji.rb', line 87

def expire_cache
  awardable.try(:bump_updated_at)
  awardable.try(:update_upvotes_count) if upvote?
end

#hook_attrsObject



101
102
103
# File 'app/models/award_emoji.rb', line 101

def hook_attrs
  Gitlab::HookData::EmojiBuilder.new(self).build
end

#to_ability_nameObject



97
98
99
# File 'app/models/award_emoji.rb', line 97

def to_ability_name
  'emoji'
end

#upvote?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/award_emoji.rb', line 71

def upvote?
  name == UPVOTE_NAME
end

#urlObject



75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/award_emoji.rb', line 75

def url
  return if TanukiEmoji.find_by_alpha_code(name)

  BatchLoader.for(name).batch(key: resource_parent) do |names, loader|
    emoji = Groups::CustomEmojiFinder.new(resource_parent, { include_ancestor_groups: true }).execute.by_name(names).select(:file)

    emoji.each do |e|
      loader.call(e.name, e.url)
    end
  end
end