Skip to content

Commit

Permalink
Merge pull request #1387 from Kyutech-C3/fix/request_custom_emoji
Browse files Browse the repository at this point in the history
Fix/request custom emoji
  • Loading branch information
PigeonsHouse authored Aug 21, 2023
2 parents 2352c6f + a032051 commit 7828024
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def new
def create
@custom_emoji = RequestCustomEmoji.new(resource_params)
@custom_emoji.account_id = current_account.id
if CustomEmoji.find_by(shortcode: @custom_emoji.shortcode)
if CustomEmoji.find_by(shortcode: @custom_emoji.shortcode, domain: nil)
@custom_emoji.errors.add(:shortcode, I18n.t('settings.request_custom_emoji.errors.already_exists'))
render :new
return
Expand Down
20 changes: 5 additions & 15 deletions app/models/request_custom_emoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,16 @@
class RequestCustomEmoji < ApplicationRecord
include Attachmentable

LIMIT = 50.kilobytes

SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}'

SCAN_RE = /(?<=[^[:alnum:]:]|\n|^)
:(#{SHORTCODE_RE_FRAGMENT}):
(?=[^[:alnum:]:]|$)/x

IMAGE_MIME_TYPES = %w(image/png image/gif).freeze

belongs_to :account

has_attached_file :image, styles: { static: { format: 'png', convert_options: '-coalesce -strip' } }, validate_media_type: false
has_attached_file :image, styles: { static: { format: 'png', convert_options: '-coalesce +profile "!icc,*" +set modify-date +set create-date' } }, validate_media_type: false

validates_attachment :image, content_type: { content_type: IMAGE_MIME_TYPES }, presence: true, size: { less_than: LIMIT }
validates :shortcode, uniqueness: true, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 }
validates_attachment :image, content_type: { content_type: CustomEmoji::IMAGE_MIME_TYPES }, presence: true, size: { less_than: CustomEmoji::LIMIT }
validates :shortcode, uniqueness: true, format: { with: CustomEmoji::SHORTCODE_ONLY_RE }, length: { minimum: 2 }

scope :alphabetic, -> { order(shortcode: :asc) }

remotable_attachment :image, LIMIT
remotable_attachment :image, CustomEmoji::LIMIT

def object_type
:emoji
Expand All @@ -48,7 +38,7 @@ class << self
def from_text(text, domain = nil)
return [] if text.blank?

shortcodes = text.scan(SCAN_RE).map(&:first).uniq
shortcodes = text.scan(CustomEmoji::SCAN_RE).map(&:first).uniq

return [] if shortcodes.empty?

Expand Down
2 changes: 1 addition & 1 deletion app/views/settings/request_custom_emojis/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
.fields-group
= f.input :shortcode, wrapper: :with_label, label: t('admin.custom_emojis.shortcode'), hint: t('admin.custom_emojis.shortcode_hint')
.fields-group
= f.input :image, wrapper: :with_label, input_html: { accept: 'image/png' }, hint: t('admin.custom_emojis.image_hint', size: number_to_human_size(RequestCustomEmoji::LIMIT))
= f.input :image, wrapper: :with_label, input_html: { accept: CustomEmoji::IMAGE_MIME_TYPES.join(' ') }, hint: t('admin.custom_emojis.image_hint', size: number_to_human_size(CustomEmoji::LIMIT))

.actions
= f.button :button, t('settings.request_custom_emoji.upload'), type: :submit

0 comments on commit 7828024

Please sign in to comment.