2017-09-19 09:42:40 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: custom_emojis
|
|
|
|
#
|
2020-04-27 06:29:08 +09:00
|
|
|
# id :bigint(8) 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
|
|
|
|
# disabled :boolean default(FALSE), not null
|
|
|
|
# uri :string
|
|
|
|
# image_remote_url :string
|
|
|
|
# visible_in_picker :boolean default(TRUE), not null
|
|
|
|
# category_id :bigint(8)
|
|
|
|
# image_storage_schema_version :integer
|
2017-09-19 09:42:40 +09:00
|
|
|
#
|
|
|
|
|
|
|
|
class CustomEmoji < ApplicationRecord
|
2021-09-30 06:52:36 +09:00
|
|
|
include Attachmentable
|
|
|
|
|
2022-07-18 05:07:20 +09:00
|
|
|
LOCAL_LIMIT = (ENV['MAX_EMOJI_SIZE'] || 256.kilobytes).to_i
|
|
|
|
LIMIT = [LOCAL_LIMIT, (ENV['MAX_REMOTE_EMOJI_SIZE'] || 256.kilobytes).to_i].max
|
2018-03-26 21:02:10 +09:00
|
|
|
|
2017-09-19 09:42:40 +09:00
|
|
|
SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}'
|
|
|
|
|
|
|
|
SCAN_RE = /(?<=[^[:alnum:]:]|\n|^)
|
|
|
|
:(#{SHORTCODE_RE_FRAGMENT}):
|
|
|
|
(?=[^[:alnum:]:]|$)/x
|
2022-11-10 13:49:30 +09:00
|
|
|
SHORTCODE_ONLY_RE = /\A#{SHORTCODE_RE_FRAGMENT}\z/
|
2017-09-19 09:42:40 +09:00
|
|
|
|
2022-05-28 03:06:40 +09:00
|
|
|
IMAGE_MIME_TYPES = %w(image/png image/gif image/webp).freeze
|
2019-08-09 06:03:09 +09:00
|
|
|
|
2019-06-28 22:54:10 +09:00
|
|
|
belongs_to :category, class_name: 'CustomEmojiCategory', optional: true
|
2017-11-07 22:49:32 +09:00
|
|
|
has_one :local_counterpart, -> { where(domain: nil) }, class_name: 'CustomEmoji', primary_key: :shortcode, foreign_key: :shortcode
|
|
|
|
|
2022-11-11 17:20:10 +09:00
|
|
|
has_attached_file :image, styles: { static: { format: 'png', convert_options: '-coalesce +profile "!icc,*" +set modify-date +set create-date' } }, validate_media_type: false
|
2017-09-19 09:42:40 +09:00
|
|
|
|
2018-12-11 13:30:57 +09:00
|
|
|
before_validation :downcase_domain
|
|
|
|
|
2021-04-25 19:02:41 +09:00
|
|
|
validates_attachment :image, content_type: { content_type: IMAGE_MIME_TYPES }, presence: true
|
|
|
|
validates_attachment_size :image, less_than: LIMIT, unless: :local?
|
|
|
|
validates_attachment_size :image, less_than: LOCAL_LIMIT, if: :local?
|
2022-11-10 13:49:30 +09:00
|
|
|
validates :shortcode, uniqueness: { scope: :domain }, format: { with: SHORTCODE_ONLY_RE }, length: { minimum: 2 }
|
2017-09-19 09:42:40 +09:00
|
|
|
|
2019-09-13 23:01:09 +09:00
|
|
|
scope :local, -> { where(domain: nil) }
|
|
|
|
scope :remote, -> { where.not(domain: nil) }
|
2017-10-06 06:42:05 +09:00
|
|
|
scope :alphabetic, -> { order(domain: :asc, shortcode: :asc) }
|
2022-08-26 03:39:40 +09:00
|
|
|
scope :by_domain_and_subdomains, ->(domain) { where(domain: domain).or(where(arel_table[:domain].matches("%.#{domain}"))) }
|
2019-09-13 23:01:09 +09:00
|
|
|
scope :listed, -> { local.where(disabled: false).where(visible_in_picker: true) }
|
2017-09-23 08:57:23 +09:00
|
|
|
|
2018-03-26 21:02:10 +09:00
|
|
|
remotable_attachment :image, LIMIT
|
2017-09-19 09:42:40 +09:00
|
|
|
|
2018-04-27 08:38:10 +09:00
|
|
|
after_commit :remove_entity_cache
|
|
|
|
|
2017-10-06 06:42:05 +09:00
|
|
|
def local?
|
|
|
|
domain.nil?
|
|
|
|
end
|
|
|
|
|
2017-10-08 00:43:42 +09:00
|
|
|
def object_type
|
|
|
|
:emoji
|
|
|
|
end
|
|
|
|
|
2019-09-10 05:44:17 +09:00
|
|
|
def copy!
|
|
|
|
copy = self.class.find_or_initialize_by(domain: nil, shortcode: shortcode)
|
|
|
|
copy.image = image
|
2019-09-18 06:20:48 +09:00
|
|
|
copy.tap(&:save!)
|
2019-09-10 05:44:17 +09:00
|
|
|
end
|
|
|
|
|
2022-08-26 03:39:40 +09:00
|
|
|
def to_log_human_identifier
|
|
|
|
shortcode
|
|
|
|
end
|
|
|
|
|
2017-09-19 09:42:40 +09:00
|
|
|
class << self
|
2020-01-24 06:00:13 +09:00
|
|
|
def from_text(text, domain = nil)
|
2017-09-19 09:42:40 +09:00
|
|
|
return [] if text.blank?
|
2017-09-27 11:14:03 +09:00
|
|
|
|
|
|
|
shortcodes = text.scan(SCAN_RE).map(&:first).uniq
|
|
|
|
|
|
|
|
return [] if shortcodes.empty?
|
|
|
|
|
2018-04-27 08:38:10 +09:00
|
|
|
EntityCache.instance.emoji(shortcodes, domain)
|
2017-09-19 09:42:40 +09:00
|
|
|
end
|
2018-04-10 22:46:27 +09:00
|
|
|
|
|
|
|
def search(shortcode)
|
|
|
|
where('"custom_emojis"."shortcode" ILIKE ?', "%#{shortcode}%")
|
|
|
|
end
|
2017-09-19 09:42:40 +09:00
|
|
|
end
|
2018-04-27 08:38:10 +09:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def remove_entity_cache
|
|
|
|
Rails.cache.delete(EntityCache.instance.to_key(:emoji, shortcode, domain))
|
|
|
|
end
|
2018-12-11 13:30:57 +09:00
|
|
|
|
|
|
|
def downcase_domain
|
|
|
|
self.domain = domain.downcase unless domain.nil?
|
|
|
|
end
|
2017-09-19 09:42:40 +09:00
|
|
|
end
|