Skip to content

Commit

Permalink
Make custom emoji domains case insensitive mastodon#9351 (mastodon#9474)
Browse files Browse the repository at this point in the history
* Make custom emoji domains case sensitive mastodon#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.
  • Loading branch information
Esteth authored and hiyuki2578 committed Oct 2, 2019
1 parent 1a6dead commit d2693d0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/models/custom_emoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -73,4 +75,8 @@ def search(shortcode)
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
2 changes: 1 addition & 1 deletion app/models/custom_emoji_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def scope_for(key, value)
when 'remote'
CustomEmoji.remote
when 'by_domain'
CustomEmoji.where(domain: value)
CustomEmoji.where(domain: value.downcase)
when 'shortcode'
CustomEmoji.search(value)
else
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20181207011115_downcase_custom_emoji_domains.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions spec/models/custom_emoji_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@
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

0 comments on commit d2693d0

Please sign in to comment.