20230215074423_move_user_settings.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # frozen_string_literal: true
  2. class MoveUserSettings < ActiveRecord::Migration[6.1]
  3. disable_ddl_transaction!
  4. class User < ApplicationRecord; end
  5. MAPPING = {
  6. default_privacy: 'default_privacy',
  7. default_sensitive: 'web.default_sensitive',
  8. default_language: 'default_language',
  9. noindex: 'noindex',
  10. theme: 'theme',
  11. trends: 'web.trends',
  12. unfollow_modal: 'web.unfollow_modal',
  13. boost_modal: 'web.reblog_modal',
  14. delete_modal: 'web.delete_modal',
  15. auto_play_gif: 'web.auto_play',
  16. display_media: 'web.display_media',
  17. expand_spoilers: 'web.expand_content_warnings',
  18. reduce_motion: 'web.reduce_motion',
  19. disable_swiping: 'web.disable_swiping',
  20. show_application: 'show_application',
  21. system_font_ui: 'web.use_system_font',
  22. aggregate_reblogs: 'aggregate_reblogs',
  23. advanced_layout: 'web.advanced_layout',
  24. use_blurhash: 'web.use_blurhash',
  25. use_pending_items: 'web.use_pending_items',
  26. crop_images: 'web.crop_images',
  27. notification_emails: {
  28. follow: 'notification_emails.follow',
  29. reblog: 'notification_emails.reblog',
  30. favourite: 'notification_emails.favourite',
  31. mention: 'notification_emails.mention',
  32. follow_request: 'notification_emails.follow_request',
  33. report: 'notification_emails.report',
  34. pending_account: 'notification_emails.pending_account',
  35. trending_tag: 'notification_emails.trends',
  36. appeal: 'notification_emails.appeal',
  37. }.freeze,
  38. always_send_emails: 'always_send_emails',
  39. interactions: {
  40. must_be_follower: 'interactions.must_be_follower',
  41. must_be_following: 'interactions.must_be_following',
  42. must_be_following_dm: 'interactions.must_be_following_dm',
  43. }.freeze,
  44. }.freeze
  45. class LegacySetting < ApplicationRecord
  46. self.table_name = 'settings'
  47. def var
  48. self[:var]&.to_sym
  49. end
  50. def value
  51. YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess, Symbol]) if self[:value].present?
  52. end
  53. end
  54. def up
  55. User.find_in_batches do |users|
  56. previous_settings_for_batch = LegacySetting.where(thing_type: 'User', thing_id: users.map(&:id)).group_by(&:thing_id)
  57. users.each do |user|
  58. previous_settings = previous_settings_for_batch[user.id]&.index_by(&:var) || {}
  59. user_settings = {}
  60. MAPPING.each do |legacy_key, new_key|
  61. value = previous_settings[legacy_key]&.value
  62. next if value.nil?
  63. if value.is_a?(Hash)
  64. value.each do |nested_key, nested_value|
  65. user_settings[MAPPING[legacy_key][nested_key.to_sym]] = nested_value
  66. end
  67. else
  68. user_settings[new_key] = value
  69. end
  70. end
  71. user.update_column('settings', Oj.dump(user_settings))
  72. end
  73. end
  74. end
  75. def down; end
  76. end