Migrate emoji reactions

This commit is contained in:
Jeremy Kescher 2023-04-03 22:16:35 +02:00
parent 08264918bf
commit c6d04be51a
No known key found for this signature in database
GPG Key ID: 80A419A7A613DFA4
3 changed files with 63 additions and 0 deletions

View File

@ -127,6 +127,10 @@ module HasUserSettings
settings['hide_followers_count']
end
def setting_visible_reactions
integer_cast_setting('visible_reactions', 0)
end
def allows_report_emails?
settings['notification_emails.report']
end
@ -170,4 +174,14 @@ module HasUserSettings
def hide_all_media?
settings['web.display_media'] == 'hide_all'
end
def integer_cast_setting(key, min = nil, max = nil)
i = ActiveModel::Type::Integer.new.cast(settings[key])
# the cast above doesn't return a number if passed the string "e"
i = 0 unless i.is_a? Numeric
return min if !min.nil? && i < min
return max if !max.nil? && i > max
i
end
end

View File

@ -18,6 +18,7 @@ class UserSettings
setting :default_privacy, default: nil, in: %w(public unlisted private)
setting :default_content_type, default: 'text/plain'
setting :hide_followers_count, default: false
setting :visible_reactions, default: 6
namespace :web do
setting :crop_images, default: true

View File

@ -0,0 +1,48 @@
# frozen_string_literal: true
class MoveEmojiReactionSettings < ActiveRecord::Migration[6.1]
class User < ApplicationRecord; end
MAPPING = {
setting_visible_reactions: 'visible_reactions',
}.freeze
class LegacySetting < ApplicationRecord
self.table_name = 'settings'
def var
self[:var]&.to_sym
end
def value
YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess]) if self[:value].present?
end
end
def up
User.find_each do |user|
previous_settings = LegacySetting.where(thing_type: 'User', thing_id: user.id).index_by(&:var)
user_settings = Oj.load(user.settings || '{}')
user_settings.delete('theme')
MAPPING.each do |legacy_key, new_key|
value = previous_settings[legacy_key]&.value
next if value.blank?
if value.is_a?(Hash)
value.each do |nested_key, nested_value|
user_settings[MAPPING[legacy_key][nested_key.to_sym]] = nested_value
end
else
user_settings[new_key] = value
end
end
user.update_column('settings', Oj.dump(user_settings)) # rubocop:disable Rails/SkipsModelValidations
end
end
def down; end
end