From c6d04be51adf9472fb3aa032657a150b725aba67 Mon Sep 17 00:00:00 2001 From: Jeremy Kescher Date: Mon, 3 Apr 2023 22:16:35 +0200 Subject: [PATCH] Migrate emoji reactions --- app/models/concerns/has_user_settings.rb | 14 ++++++ app/models/user_settings.rb | 1 + ...0215074425_move_emoji_reaction_settings.rb | 48 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 db/migrate/20230215074425_move_emoji_reaction_settings.rb diff --git a/app/models/concerns/has_user_settings.rb b/app/models/concerns/has_user_settings.rb index 0e9d4e1cd..bc6af2d36 100644 --- a/app/models/concerns/has_user_settings.rb +++ b/app/models/concerns/has_user_settings.rb @@ -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 diff --git a/app/models/user_settings.rb b/app/models/user_settings.rb index 41b4f57f0..70b375e94 100644 --- a/app/models/user_settings.rb +++ b/app/models/user_settings.rb @@ -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 diff --git a/db/migrate/20230215074425_move_emoji_reaction_settings.rb b/db/migrate/20230215074425_move_emoji_reaction_settings.rb new file mode 100644 index 000000000..9b9a65e04 --- /dev/null +++ b/db/migrate/20230215074425_move_emoji_reaction_settings.rb @@ -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