From e4c411ef1a6a4dfd5b2a2ac2bd582e0afc868c89 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 + .../preferences/appearance/show.html.haml | 16 +++---- ...0215074425_move_emoji_reaction_settings.rb | 48 +++++++++++++++++++ db/schema.rb | 2 +- lib/mastodon/version.rb | 2 +- 6 files changed, 73 insertions(+), 10 deletions(-) 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 0e9d4e1cd4..bc6af2d361 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 0be8c5fbce..43afe32add 100644 --- a/app/models/user_settings.rb +++ b/app/models/user_settings.rb @@ -18,6 +18,7 @@ class UserSettings setting :default_privacy, default: nil 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/app/views/settings/preferences/appearance/show.html.haml b/app/views/settings/preferences/appearance/show.html.haml index 09ca7eb411..b83ee3432d 100644 --- a/app/views/settings/preferences/appearance/show.html.haml +++ b/app/views/settings/preferences/appearance/show.html.haml @@ -40,7 +40,7 @@ = ff.input :'web.crop_images', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_crop_images') .fields-group.fields-row__column.fields-row__column-6 - = f.input :setting_visible_reactions, wrapper: :with_label, input_html: { type: 'number', min: '0', data: { default: '6' } }, hint: false + = ff.input :'visible_reactions', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_visible_reactions'), input_html: { type: 'number', min: '0', data: { default: '6' } }, hint: false %h4= t 'appearance.discovery' @@ -55,16 +55,16 @@ = ff.input :'web.favourite_modal', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_favourite_modal'), glitch_only: true = ff.input :'web.delete_modal', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_delete_modal') - %h4= t 'appearance.sensitive_content' + %h4= t 'appearance.sensitive_content' - .fields-group - = ff.input :'web.display_media', collection: ['default', 'show_all', 'hide_all'],label_method: lambda { |item| t("simple_form.hints.defaults.setting_display_media_#{item}") }, hint: false, as: :radio_buttons, collection_wrapper_tag: 'ul', item_wrapper_tag: 'li', wrapper: :with_floating_label, label: I18n.t('simple_form.labels.defaults.setting_display_media') + .fields-group + = f.input :setting_display_media, collection: ['default', 'show_all', 'hide_all'], label_method: lambda { |item| t("simple_form.hints.defaults.setting_display_media_#{item}") }, hint: false, as: :radio_buttons, collection_wrapper_tag: 'ul', item_wrapper_tag: 'li', wrapper: :with_floating_label - .fields-group - = ff.input :'web.use_blurhash', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_use_blurhash'), hint: I18n.t('simple_form.hints.defaults.setting_use_blurhash') + .fields-group + = f.input :setting_use_blurhash, as: :boolean, wrapper: :with_label - .fields-group - = ff.input :'web.expand_content_warnings', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_expand_spoilers') + .fields-group + = f.input :setting_expand_spoilers, as: :boolean, wrapper: :with_label .actions = f.button :button, t('generic.save_changes'), type: :submit 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 0000000000..9b9a65e046 --- /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 diff --git a/db/schema.rb b/db/schema.rb index 3dfd5f045d..b0b267226e 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: 2023_02_15_074424) do +ActiveRecord::Schema.define(version: 2023_02_15_074425) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/lib/mastodon/version.rb b/lib/mastodon/version.rb index 22eb6315b1..bf1100798c 100644 --- a/lib/mastodon/version.rb +++ b/lib/mastodon/version.rb @@ -25,7 +25,7 @@ module Mastodon end def suffix_version - '+1.0.2' + '+1.0.3' end def to_a