File: custom_emoji.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (67 lines) | stat: -rw-r--r-- 1,985 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true

class CustomEmoji < ApplicationRecord
  NAME_REGEXP = /[a-z0-9_-]+/

  belongs_to :namespace, inverse_of: :custom_emoji

  belongs_to :group, -> { where(type: Group.sti_name) }, foreign_key: 'namespace_id'
  belongs_to :creator, class_name: "User", inverse_of: :created_custom_emoji

  # For now only external emoji are supported. See https://gitlab.com/gitlab-org/gitlab/-/issues/230467
  validates :external, inclusion: { in: [true] }

  validates :file, public_url: true, if: :external

  validate :valid_emoji_name

  validates :group, presence: true
  validates :creator, presence: true
  validates :name,
    uniqueness: { scope: [:namespace_id, :name] },
    presence: true,
    length: { maximum: 36 },

    format: { with: /\A#{NAME_REGEXP}\z/o }

  scope :by_name, ->(names) { where(name: names) }
  scope :for_namespaces, ->(namespace_ids) do
    order = Gitlab::Pagination::Keyset::Order.build([
      Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
        attribute_name: 'name',
        order_expression: CustomEmoji.arel_table[:name].asc,
        nullable: :not_nullable
      ),
      Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
        attribute_name: 'current_namespace',
        order_expression: Arel::Nodes::Case.new.when(
          CustomEmoji.arel_table[:namespace_id].eq(namespace_ids.first)
        ).then(0).else(1).asc,
        nullable: :not_nullable,
        add_to_projections: true
      )
    ])
    where(namespace_id: namespace_ids)
      .select("DISTINCT ON (name) *")
      .order(order)
  end

  scope :for_resource, ->(resource) do
    return none if resource.nil?
    return none unless resource.is_a?(Group)

    resource.custom_emoji
  end

  def url
    Gitlab::AssetProxy.proxy_url(file)
  end

  private

  def valid_emoji_name
    if TanukiEmoji.find_by_alpha_code(name)
      errors.add(:name, _('%{name} is already being used for another emoji') % { name: self.name })
    end
  end
end