2024-03-08 01:53:37 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MigrateInteractionSettingsToPolicy < ActiveRecord::Migration[7.1]
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
# Dummy classes, to make migration possible across version changes
|
|
|
|
class User < ApplicationRecord
|
2024-03-27 02:57:08 +11:00
|
|
|
belongs_to :notification_policy, foreign_key: 'account_id', primary_key: 'account_id', optional: true, inverse_of: false
|
2024-03-08 01:53:37 +11:00
|
|
|
end
|
|
|
|
|
2024-03-27 02:57:08 +11:00
|
|
|
class NotificationPolicy < ApplicationRecord; end
|
2024-03-08 01:53:37 +11:00
|
|
|
|
|
|
|
def up
|
2024-03-27 02:57:08 +11:00
|
|
|
User.includes(:notification_policy).in_batches do |users|
|
|
|
|
NotificationPolicy.upsert_all(users.filter_map { |user| policy_for_user(user) })
|
|
|
|
end
|
|
|
|
end
|
2024-03-08 20:10:07 +11:00
|
|
|
|
2024-03-27 02:57:08 +11:00
|
|
|
def down; end
|
2024-03-08 20:10:07 +11:00
|
|
|
|
2024-03-27 02:57:08 +11:00
|
|
|
private
|
2024-03-08 01:53:37 +11:00
|
|
|
|
2024-03-27 02:57:08 +11:00
|
|
|
def policy_for_user(user)
|
|
|
|
deserialized_settings = Oj.load(user.attributes_before_type_cast['settings'])
|
|
|
|
return if deserialized_settings.nil?
|
2024-03-08 01:53:37 +11:00
|
|
|
|
2024-03-27 02:57:08 +11:00
|
|
|
requires_new_policy = false
|
2024-03-08 01:53:37 +11:00
|
|
|
|
2024-03-27 02:57:08 +11:00
|
|
|
policy = {
|
|
|
|
account_id: user.account_id,
|
|
|
|
filter_not_followers: false,
|
|
|
|
filter_not_following: false,
|
|
|
|
filter_private_mentions: true,
|
|
|
|
}
|
2024-03-08 01:53:37 +11:00
|
|
|
|
2024-03-27 02:57:08 +11:00
|
|
|
if deserialized_settings['interactions.must_be_follower']
|
|
|
|
policy[:filter_not_followers] = true
|
|
|
|
requires_new_policy = true
|
2024-03-08 01:53:37 +11:00
|
|
|
end
|
|
|
|
|
2024-03-27 02:57:08 +11:00
|
|
|
if deserialized_settings['interactions.must_be_following']
|
|
|
|
policy[:filter_not_following] = true
|
|
|
|
requires_new_policy = true
|
|
|
|
end
|
|
|
|
|
|
|
|
unless deserialized_settings['interactions.must_be_following_dm']
|
|
|
|
policy[:filter_private_mentions] = false
|
|
|
|
requires_new_policy = true
|
|
|
|
end
|
|
|
|
|
|
|
|
policy if requires_new_policy
|
|
|
|
end
|
2024-03-08 01:53:37 +11:00
|
|
|
end
|