custom_emoji.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}'
  26. SCAN_RE = /(?<=[^[:alnum:]:]|\n|^)
  27. :(#{SHORTCODE_RE_FRAGMENT}):
  28. (?=[^[:alnum:]:]|$)/x
  29. IMAGE_MIME_TYPES = %w(image/png image/gif image/webp).freeze
  30. belongs_to :category, class_name: 'CustomEmojiCategory', optional: true
  31. has_one :local_counterpart, -> { where(domain: nil) }, class_name: 'CustomEmoji', primary_key: :shortcode, foreign_key: :shortcode
  32. has_attached_file :image, styles: { static: { format: 'png', convert_options: '-coalesce -strip' } }, validate_media_type: false
  33. before_validation :downcase_domain
  34. validates_attachment :image, content_type: { content_type: IMAGE_MIME_TYPES }, presence: true, size: { less_than: LIMIT }
  35. validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 }
  36. scope :local, -> { where(domain: nil) }
  37. scope :remote, -> { where.not(domain: nil) }
  38. scope :alphabetic, -> { order(domain: :asc, shortcode: :asc) }
  39. scope :by_domain_and_subdomains, ->(domain) { where(domain: domain).or(where(arel_table[:domain].matches('%.' + domain))) }
  40. scope :listed, -> { local.where(disabled: false).where(visible_in_picker: true) }
  41. remotable_attachment :image, LIMIT
  42. after_commit :remove_entity_cache
  43. def local?
  44. domain.nil?
  45. end
  46. def object_type
  47. :emoji
  48. end
  49. def copy!
  50. copy = self.class.find_or_initialize_by(domain: nil, shortcode: shortcode)
  51. copy.image = image
  52. copy.tap(&:save!)
  53. end
  54. class << self
  55. def from_text(text, domain = nil)
  56. return [] if text.blank?
  57. shortcodes = text.scan(SCAN_RE).map(&:first).uniq
  58. return [] if shortcodes.empty?
  59. EntityCache.instance.emoji(shortcodes, domain)
  60. end
  61. def search(shortcode)
  62. where('"custom_emojis"."shortcode" ILIKE ?', "%#{shortcode}%")
  63. end
  64. end
  65. private
  66. def remove_entity_cache
  67. Rails.cache.delete(EntityCache.instance.to_key(:emoji, shortcode, domain))
  68. end
  69. def downcase_domain
  70. self.domain = domain.downcase unless domain.nil?
  71. end
  72. end