2017-09-18 19:42:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: custom_emojis
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# shortcode :string default(""), not null
|
|
|
|
# domain :string
|
|
|
|
# image_file_name :string
|
|
|
|
# image_content_type :string
|
|
|
|
# image_file_size :integer
|
|
|
|
# image_updated_at :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2017-10-05 16:42:05 -05:00
|
|
|
# disabled :boolean default(FALSE), not null
|
2017-10-07 10:43:42 -05:00
|
|
|
# uri :string
|
|
|
|
# image_remote_url :string
|
2017-10-27 09:11:30 -05:00
|
|
|
# visible_in_picker :boolean default(TRUE), not null
|
2017-09-18 19:42:40 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
class CustomEmoji < ApplicationRecord
|
|
|
|
SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}'
|
|
|
|
|
|
|
|
SCAN_RE = /(?<=[^[:alnum:]:]|\n|^)
|
|
|
|
:(#{SHORTCODE_RE_FRAGMENT}):
|
|
|
|
(?=[^[:alnum:]:]|$)/x
|
|
|
|
|
2017-11-07 07:49:32 -06:00
|
|
|
has_one :local_counterpart, -> { where(domain: nil) }, class_name: 'CustomEmoji', primary_key: :shortcode, foreign_key: :shortcode
|
|
|
|
|
2017-10-05 16:41:47 -05:00
|
|
|
has_attached_file :image, styles: { static: { format: 'png', convert_options: '-coalesce -strip' } }
|
2017-09-18 19:42:40 -05:00
|
|
|
|
|
|
|
validates_attachment :image, content_type: { content_type: 'image/png' }, presence: true, size: { in: 0..50.kilobytes }
|
|
|
|
validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 }
|
|
|
|
|
2017-10-05 16:42:05 -05:00
|
|
|
scope :local, -> { where(domain: nil) }
|
|
|
|
scope :remote, -> { where.not(domain: nil) }
|
|
|
|
scope :alphabetic, -> { order(domain: :asc, shortcode: :asc) }
|
2017-09-22 18:57:23 -05:00
|
|
|
|
2017-09-18 19:42:40 -05:00
|
|
|
include Remotable
|
|
|
|
|
2017-10-05 16:42:05 -05:00
|
|
|
def local?
|
|
|
|
domain.nil?
|
|
|
|
end
|
|
|
|
|
2017-10-07 10:43:42 -05:00
|
|
|
def object_type
|
|
|
|
:emoji
|
|
|
|
end
|
|
|
|
|
2017-09-18 19:42:40 -05:00
|
|
|
class << self
|
|
|
|
def from_text(text, domain)
|
|
|
|
return [] if text.blank?
|
2017-09-26 21:14:03 -05:00
|
|
|
|
|
|
|
shortcodes = text.scan(SCAN_RE).map(&:first).uniq
|
|
|
|
|
|
|
|
return [] if shortcodes.empty?
|
|
|
|
|
2017-10-05 16:42:05 -05:00
|
|
|
where(shortcode: shortcodes, domain: domain, disabled: false)
|
2017-09-18 19:42:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|