1
0

custom_emoji.rb 2.7 KB

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