From 7d00e4edbd0bef8791d8efee7665eb13bb256d7a Mon Sep 17 00:00:00 2001 From: Adam Copp Date: Tue, 11 Dec 2018 04:30:57 +0000 Subject: [PATCH] Make custom emoji domains case insensitive #9351 (#9474) * Make custom emoji domains case sensitive #9351 * Fixup style in downcase_domain to comply with codeclimate. * switch if! to unless * Don't use transactions, operate in batches. Also revert spurious schema change. --- app/models/custom_emoji.rb | 6 ++++++ app/models/custom_emoji_filter.rb | 2 +- .../20181207011115_downcase_custom_emoji_domains.rb | 7 +++++++ db/schema.rb | 2 +- spec/models/custom_emoji_spec.rb | 9 +++++++++ 5 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20181207011115_downcase_custom_emoji_domains.rb diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb index b99ed01f0..d3cc70504 100644 --- a/app/models/custom_emoji.rb +++ b/app/models/custom_emoji.rb @@ -31,6 +31,8 @@ class CustomEmoji < ApplicationRecord 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 } @@ -73,4 +75,8 @@ class CustomEmoji < ApplicationRecord 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 diff --git a/app/models/custom_emoji_filter.rb b/app/models/custom_emoji_filter.rb index c4bc310bb..7649055d2 100644 --- a/app/models/custom_emoji_filter.rb +++ b/app/models/custom_emoji_filter.rb @@ -26,7 +26,7 @@ class CustomEmojiFilter when 'remote' CustomEmoji.remote when 'by_domain' - CustomEmoji.where(domain: value) + CustomEmoji.where(domain: value.downcase) when 'shortcode' CustomEmoji.search(value) else diff --git a/db/migrate/20181207011115_downcase_custom_emoji_domains.rb b/db/migrate/20181207011115_downcase_custom_emoji_domains.rb new file mode 100644 index 000000000..c9db3800d --- /dev/null +++ b/db/migrate/20181207011115_downcase_custom_emoji_domains.rb @@ -0,0 +1,7 @@ +class DowncaseCustomEmojiDomains < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + CustomEmoji.in_batches.update_all('domain = lower(domain)') + end +end diff --git a/db/schema.rb b/db/schema.rb index 6d643c27c..51ac43e1d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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" diff --git a/spec/models/custom_emoji_spec.rb b/spec/models/custom_emoji_spec.rb index 320a258d3..9de218b4f 100644 --- a/spec/models/custom_emoji_spec.rb +++ b/spec/models/custom_emoji_spec.rb @@ -75,4 +75,13 @@ RSpec.describe CustomEmoji, type: :model do 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