has_attached_file :image, styles: { static: { format: 'png', convert_options: '-coalesce -strip' } }
+ before_validation :downcase_domain
+
validates_attachment :image, content_type: { content_type: 'image/png' }, presence: true, size: { less_than: LIMIT }
validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 }
def remove_entity_cache
Rails.cache.delete(EntityCache.instance.to_key(:emoji, shortcode, domain))
end
+
+ def downcase_domain
+ self.domain = domain.downcase unless domain.nil?
+ end
end
when 'remote'
CustomEmoji.remote
when 'by_domain'
- CustomEmoji.where(domain: value)
+ CustomEmoji.where(domain: value.downcase)
when 'shortcode'
CustomEmoji.search(value)
else
--- /dev/null
+class DowncaseCustomEmojiDomains < ActiveRecord::Migration[5.2]
+ disable_ddl_transaction!
+
+ def change
+ CustomEmoji.in_batches.update_all('domain = lower(domain)')
+ end
+end
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2018_12_04_215309) do
+ActiveRecord::Schema.define(version: 2018_12_07_011115) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
end
end
end
+
+ describe 'pre_validation' do
+ let(:custom_emoji) { Fabricate(:custom_emoji, domain: 'wWw.MaStOdOn.CoM') }
+
+ it 'should downcase' do
+ custom_emoji.valid?
+ expect(custom_emoji.domain).to eq('www.mastodon.com')
+ end
+ end
end