1
0

custom_emoji.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: custom_emojis
  5. #
  6. # id :bigint(8) 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. # category_id :bigint(8)
  20. # image_storage_schema_version :integer
  21. #
  22. class CustomEmoji < ApplicationRecord
  23. include Attachmentable
  24. LIMIT = 256.kilobytes
  25. MINIMUM_SHORTCODE_SIZE = 2
  26. SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}'
  27. SCAN_RE = /(?<=[^[:alnum:]:]|\n|^)
  28. :(#{SHORTCODE_RE_FRAGMENT}):
  29. (?=[^[:alnum:]:]|$)/x
  30. SHORTCODE_ONLY_RE = /\A#{SHORTCODE_RE_FRAGMENT}\z/
  31. IMAGE_MIME_TYPES = %w(image/png image/gif image/webp).freeze
  32. belongs_to :category, class_name: 'CustomEmojiCategory', optional: true
  33. has_one :local_counterpart, -> { where(domain: nil) }, class_name: 'CustomEmoji', primary_key: :shortcode, foreign_key: :shortcode, inverse_of: false, dependent: nil
  34. has_attached_file :image, styles: { static: { format: 'png', convert_options: '-coalesce +profile "!icc,*" +set date:modify +set date:create +set date:timestamp', file_geometry_parser: FastGeometryParser } }, validate_media_type: false, processors: [:lazy_thumbnail]
  35. normalizes :domain, with: ->(domain) { domain.downcase }
  36. validates_attachment :image, content_type: { content_type: IMAGE_MIME_TYPES }, presence: true, size: { less_than: LIMIT }
  37. validates :shortcode, uniqueness: { scope: :domain }, format: { with: SHORTCODE_ONLY_RE }, length: { minimum: MINIMUM_SHORTCODE_SIZE }
  38. scope :local, -> { where(domain: nil) }
  39. scope :remote, -> { where.not(domain: nil) }
  40. scope :enabled, -> { where(disabled: false) }
  41. scope :alphabetic, -> { order(domain: :asc, shortcode: :asc) }
  42. scope :by_domain_and_subdomains, ->(domain) { where(domain: domain).or(where(arel_table[:domain].matches("%.#{domain}"))) }
  43. scope :listed, -> { local.enabled.where(visible_in_picker: true) }
  44. remotable_attachment :image, LIMIT
  45. after_commit :remove_entity_cache
  46. def local?
  47. domain.nil?
  48. end
  49. def object_type
  50. :emoji
  51. end
  52. def copy!
  53. copy = self.class.find_or_initialize_by(domain: nil, shortcode: shortcode)
  54. copy.image = image
  55. copy.tap(&:save!)
  56. end
  57. def to_log_human_identifier
  58. shortcode
  59. end
  60. class << self
  61. def from_text(text, domain = nil)
  62. return [] if text.blank?
  63. shortcodes = text.scan(SCAN_RE).map(&:first).uniq
  64. return [] if shortcodes.empty?
  65. EntityCache.instance.emoji(shortcodes, domain)
  66. end
  67. def search(shortcode)
  68. where(arel_table[:shortcode].matches("%#{sanitize_sql_like(shortcode)}%"))
  69. end
  70. end
  71. private
  72. def remove_entity_cache
  73. Rails.cache.delete(EntityCache.instance.to_key(:emoji, shortcode, domain))
  74. end
  75. end