mirror of
https://github.com/mastodon/mastodon.git
synced 2024-12-24 09:57:27 +00:00
Use normalizes to prepare User
values (#28650)
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
00341c70ff
commit
543d7890fd
|
@ -118,14 +118,15 @@ class User < ApplicationRecord
|
||||||
scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) }
|
scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) }
|
||||||
scope :matches_ip, ->(value) { left_joins(:ips).where('user_ips.ip <<= ?', value).group('users.id') }
|
scope :matches_ip, ->(value) { left_joins(:ips).where('user_ips.ip <<= ?', value).group('users.id') }
|
||||||
|
|
||||||
before_validation :sanitize_languages
|
|
||||||
before_validation :sanitize_role
|
before_validation :sanitize_role
|
||||||
before_validation :sanitize_time_zone
|
|
||||||
before_validation :sanitize_locale
|
|
||||||
before_create :set_approved
|
before_create :set_approved
|
||||||
after_commit :send_pending_devise_notifications
|
after_commit :send_pending_devise_notifications
|
||||||
after_create_commit :trigger_webhooks
|
after_create_commit :trigger_webhooks
|
||||||
|
|
||||||
|
normalizes :locale, with: ->(locale) { I18n.available_locales.exclude?(locale.to_sym) ? nil : locale }
|
||||||
|
normalizes :time_zone, with: ->(time_zone) { ActiveSupport::TimeZone[time_zone].nil? ? nil : time_zone }
|
||||||
|
normalizes :chosen_languages, with: ->(chosen_languages) { chosen_languages.compact_blank.presence }
|
||||||
|
|
||||||
# This avoids a deprecation warning from Rails 5.1
|
# This avoids a deprecation warning from Rails 5.1
|
||||||
# It seems possible that a future release of devise-two-factor will
|
# It seems possible that a future release of devise-two-factor will
|
||||||
# handle this itself, and this can be removed from our User class.
|
# handle this itself, and this can be removed from our User class.
|
||||||
|
@ -447,25 +448,10 @@ class User < ApplicationRecord
|
||||||
@bypass_invite_request_check
|
@bypass_invite_request_check
|
||||||
end
|
end
|
||||||
|
|
||||||
def sanitize_languages
|
|
||||||
return if chosen_languages.nil?
|
|
||||||
|
|
||||||
chosen_languages.compact_blank!
|
|
||||||
self.chosen_languages = nil if chosen_languages.empty?
|
|
||||||
end
|
|
||||||
|
|
||||||
def sanitize_role
|
def sanitize_role
|
||||||
self.role = nil if role.present? && role.everyone?
|
self.role = nil if role.present? && role.everyone?
|
||||||
end
|
end
|
||||||
|
|
||||||
def sanitize_time_zone
|
|
||||||
self.time_zone = nil if time_zone.present? && ActiveSupport::TimeZone[time_zone].nil?
|
|
||||||
end
|
|
||||||
|
|
||||||
def sanitize_locale
|
|
||||||
self.locale = nil if locale.present? && I18n.available_locales.exclude?(locale.to_sym)
|
|
||||||
end
|
|
||||||
|
|
||||||
def prepare_new_user!
|
def prepare_new_user!
|
||||||
BootstrapTimelineWorker.perform_async(account_id)
|
BootstrapTimelineWorker.perform_async(account_id)
|
||||||
ActivityTracker.increment('activity:accounts:local')
|
ActivityTracker.increment('activity:accounts:local')
|
||||||
|
|
|
@ -38,23 +38,49 @@ RSpec.describe User do
|
||||||
user.save(validate: false)
|
user.save(validate: false)
|
||||||
expect(user.valid?).to be true
|
expect(user.valid?).to be true
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it 'cleans out invalid locale' do
|
describe 'Normalizations' do
|
||||||
user = Fabricate.build(:user, locale: 'toto')
|
describe 'locale' do
|
||||||
expect(user.valid?).to be true
|
it 'preserves valid locale' do
|
||||||
expect(user.locale).to be_nil
|
user = Fabricate.build(:user, locale: 'en')
|
||||||
|
|
||||||
|
expect(user.locale).to eq('en')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'cleans out invalid locale' do
|
||||||
|
user = Fabricate.build(:user, locale: 'toto')
|
||||||
|
|
||||||
|
expect(user.locale).to be_nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'cleans out invalid timezone' do
|
describe 'time_zone' do
|
||||||
user = Fabricate.build(:user, time_zone: 'toto')
|
it 'preserves valid timezone' do
|
||||||
expect(user.valid?).to be true
|
user = Fabricate.build(:user, time_zone: 'UTC')
|
||||||
expect(user.time_zone).to be_nil
|
|
||||||
|
expect(user.time_zone).to eq('UTC')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'cleans out invalid timezone' do
|
||||||
|
user = Fabricate.build(:user, time_zone: 'toto')
|
||||||
|
|
||||||
|
expect(user.time_zone).to be_nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'cleans out empty string from languages' do
|
describe 'languages' do
|
||||||
user = Fabricate.build(:user, chosen_languages: [''])
|
it 'preserves valid options for languages' do
|
||||||
user.valid?
|
user = Fabricate.build(:user, chosen_languages: ['en', 'fr', ''])
|
||||||
expect(user.chosen_languages).to be_nil
|
|
||||||
|
expect(user.chosen_languages).to eq(['en', 'fr'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'cleans out empty string from languages' do
|
||||||
|
user = Fabricate.build(:user, chosen_languages: [''])
|
||||||
|
|
||||||
|
expect(user.chosen_languages).to be_nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue