about summary refs log tree commit diff
path: root/app/models/custom_emoji.rb
diff options
context:
space:
mode:
authorOndřej Hruška <ondra@ondrovo.com>2017-09-28 09:12:17 +0200
committerOndřej Hruška <ondra@ondrovo.com>2017-09-28 09:12:17 +0200
commit9330ea1f4d34b6ef4ce6e841f1aa931a7f10f749 (patch)
tree81e346b3d9ca532b3d252135d82ce20f61780a36 /app/models/custom_emoji.rb
parent06e299cef591b63bd96f320eadc873b047cd2664 (diff)
parent4aea3f88a6d30f102a79c2da7fcfac96465ba1a8 (diff)
Merge commit '4aea3f88a6d30f102a79c2da7fcfac96465ba1a8' into merging-upstream
Diffstat (limited to 'app/models/custom_emoji.rb')
-rw-r--r--app/models/custom_emoji.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb
new file mode 100644
index 000000000..f4d3b16a0
--- /dev/null
+++ b/app/models/custom_emoji.rb
@@ -0,0 +1,38 @@
+# 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
+#
+
+class CustomEmoji < ApplicationRecord
+  SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}'
+
+  SCAN_RE = /(?<=[^[:alnum:]:]|\n|^)
+    :(#{SHORTCODE_RE_FRAGMENT}):
+    (?=[^[:alnum:]:]|$)/x
+
+  has_attached_file :image
+
+  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 }
+
+  include Remotable
+
+  class << self
+    def from_text(text, domain)
+      return [] if text.blank?
+      shortcodes = text.scan(SCAN_RE).map(&:first)
+      where(shortcode: shortcodes, domain: domain)
+    end
+  end
+end