20240304090449_migrate_interaction_settings_to_policy.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # frozen_string_literal: true
  2. class MigrateInteractionSettingsToPolicy < ActiveRecord::Migration[7.1]
  3. disable_ddl_transaction!
  4. # Dummy classes, to make migration possible across version changes
  5. class User < ApplicationRecord
  6. belongs_to :notification_policy, foreign_key: 'account_id', primary_key: 'account_id', optional: true, inverse_of: false
  7. end
  8. class NotificationPolicy < ApplicationRecord; end
  9. def up
  10. User.includes(:notification_policy).in_batches do |users|
  11. NotificationPolicy.upsert_all(users.filter_map { |user| policy_for_user(user) })
  12. end
  13. end
  14. def down; end
  15. private
  16. def policy_for_user(user)
  17. deserialized_settings = Oj.load(user.attributes_before_type_cast['settings'])
  18. return if deserialized_settings.nil?
  19. requires_new_policy = false
  20. policy = {
  21. account_id: user.account_id,
  22. filter_not_followers: false,
  23. filter_not_following: false,
  24. filter_private_mentions: true,
  25. }
  26. if deserialized_settings['interactions.must_be_follower']
  27. policy[:filter_not_followers] = true
  28. requires_new_policy = true
  29. end
  30. if deserialized_settings['interactions.must_be_following']
  31. policy[:filter_not_following] = true
  32. requires_new_policy = true
  33. end
  34. unless deserialized_settings['interactions.must_be_following_dm']
  35. policy[:filter_private_mentions] = false
  36. requires_new_policy = true
  37. end
  38. policy if requires_new_policy
  39. end
  40. end