The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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