BigW Consortium Gitlab

award_emoji.rb 2.45 KB
Newer Older
1 2 3 4
module Gitlab
  class AwardEmoji
    CATEGORIES = {
      objects: "Objects",
5 6
      travel: "Travel",
      symbols: "Symbols",
7 8 9 10
      nature: "Nature",
      people: "People",
      activity: "Activity",
      flags: "Flags",
11
      food: "Food"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
    }.with_indifferent_access

    def self.normalize_emoji_name(name)
      aliases[name] || name
    end

    def self.emoji_by_category
      unless @emoji_by_category
        @emoji_by_category = Hash.new { |h, key| h[key] = [] }

        emojis.each do |emoji_name, data|
          data["name"] = emoji_name

          # Skip Fitzpatrick(tone) modifiers
          next if data["category"] == "modifier"

28
          category = data["category"]
29 30 31 32 33 34 35 36 37 38 39

          @emoji_by_category[category] << data
        end

        @emoji_by_category = @emoji_by_category.sort.to_h
      end

      @emoji_by_category
    end

    def self.emojis
40 41 42 43 44
      @emojis ||=
        begin
          json_path = File.join(Rails.root, 'fixtures', 'emojis', 'index.json' )
          JSON.parse(File.read(json_path))
        end
45 46 47
    end

    def self.aliases
48 49
      @aliases ||=
        begin
50 51 52
          json_path = File.join(Rails.root, 'fixtures', 'emojis', 'aliases.json')
          JSON.parse(File.read(json_path))
        end
53 54 55 56 57 58
    end

    # Returns an Array of Emoji names and their asset URLs.
    def self.urls
      @urls ||= begin
                  path = File.join(Rails.root, 'fixtures', 'emojis', 'digests.json')
59 60 61
                  # Construct the full asset path ourselves because
                  # ActionView::Helpers::AssetUrlHelper.asset_url is slow for hundreds
                  # of entries since it has to do a lot of extra work (e.g. regexps).
62 63
                  prefix = Gitlab::Application.config.assets.prefix
                  digest = Gitlab::Application.config.assets.digest
64 65 66 67 68 69
                  base =
                    if defined?(Gitlab::Application.config.relative_url_root) && Gitlab::Application.config.relative_url_root
                      Gitlab::Application.config.relative_url_root
                    else
                      ''
                    end
70 71 72 73 74 75 76 77

                  JSON.parse(File.read(path)).map do |hash|
                    if digest
                      fname = "#{hash['unicode']}-#{hash['digest']}"
                    else
                      fname = hash['unicode']
                    end

78
                    { name: hash['name'], path: File.join(base, prefix, "#{fname}.png") }
79 80 81 82 83
                  end
                end
    end
  end
end