Class: AwardEmoji

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

Constant Summary collapse

DOWNVOTE_NAME =
"thumbsdown"
UPVOTE_NAME =
"thumbsup"

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

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, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, 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 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.



52
53
54
55
56
57
58
# File 'app/models/award_emoji.rb', line 52

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



37
38
39
40
41
# File 'app/models/award_emoji.rb', line 37

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



80
81
82
83
# File 'app/models/award_emoji.rb', line 80

def broadcast_note_update
  awardable.broadcast_noteable_notes_changed
  awardable.trigger_note_subscription_update
end

#downvote?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/award_emoji.rb', line 61

def downvote?
  name == DOWNVOTE_NAME
end

#expire_cacheObject



75
76
77
78
# File 'app/models/award_emoji.rb', line 75

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

#hook_attrsObject



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

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

#to_ability_nameObject



85
86
87
# File 'app/models/award_emoji.rb', line 85

def to_ability_name
  'emoji'
end

#upvote?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/award_emoji.rb', line 65

def upvote?
  name == UPVOTE_NAME
end

#urlObject



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

def url
  return if TanukiEmoji.find_by_alpha_code(name)

  CustomEmoji.for_resource(resource_parent).by_name(name).select(:url).first&.url
end