You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.2 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: custom_emojis
  5. #
  6. # id :integer not null, primary key
  7. # shortcode :string default(""), not null
  8. # domain :string
  9. # image_file_name :string
  10. # image_content_type :string
  11. # image_file_size :integer
  12. # image_updated_at :datetime
  13. # created_at :datetime not null
  14. # updated_at :datetime not null
  15. #
  16. class CustomEmoji < ApplicationRecord
  17. SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}'
  18. SCAN_RE = /(?<=[^[:alnum:]:]|\n|^)
  19. :(#{SHORTCODE_RE_FRAGMENT}):
  20. (?=[^[:alnum:]:]|$)/x
  21. has_attached_file :image
  22. validates_attachment :image, content_type: { content_type: 'image/png' }, presence: true, size: { in: 0..50.kilobytes }
  23. validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 }
  24. scope :local, -> { where(domain: nil) }
  25. include Remotable
  26. class << self
  27. def from_text(text, domain)
  28. return [] if text.blank?
  29. shortcodes = text.scan(SCAN_RE).map(&:first)
  30. where(shortcode: shortcodes, domain: domain)
  31. end
  32. end
  33. end