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