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.

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