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.

51 lines
1.5 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. # disabled :boolean default(FALSE), not null
  16. #
  17. class CustomEmoji < ApplicationRecord
  18. SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}'
  19. SCAN_RE = /(?<=[^[:alnum:]:]|\n|^)
  20. :(#{SHORTCODE_RE_FRAGMENT}):
  21. (?=[^[:alnum:]:]|$)/x
  22. has_attached_file :image, styles: { static: { format: 'png', convert_options: '-coalesce -strip' } }
  23. validates_attachment :image, content_type: { content_type: 'image/png' }, presence: true, size: { in: 0..50.kilobytes }
  24. validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 }
  25. scope :local, -> { where(domain: nil) }
  26. scope :remote, -> { where.not(domain: nil) }
  27. scope :alphabetic, -> { order(domain: :asc, shortcode: :asc) }
  28. include Remotable
  29. def local?
  30. domain.nil?
  31. end
  32. class << self
  33. def from_text(text, domain)
  34. return [] if text.blank?
  35. shortcodes = text.scan(SCAN_RE).map(&:first).uniq
  36. return [] if shortcodes.empty?
  37. where(shortcode: shortcodes, domain: domain, disabled: false)
  38. end
  39. end
  40. end