20180608213548_reject_following_blocked_users.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # frozen_string_literal: true
  2. class RejectFollowingBlockedUsers < ActiveRecord::Migration[5.2]
  3. disable_ddl_transaction!
  4. def up
  5. blocked_follows = Follow.find_by_sql(<<-SQL.squish)
  6. select f.* from follows f
  7. inner join blocks b on
  8. f.account_id = b.target_account_id and
  9. f.target_account_id = b.account_id
  10. SQL
  11. domain_blocked_follows = Follow.find_by_sql(<<-SQL.squish)
  12. select f.* from follows f
  13. inner join accounts following on f.account_id = following.id
  14. inner join account_domain_blocks b on
  15. lower(b.domain) = lower(following.domain) and
  16. f.target_account_id = b.account_id
  17. SQL
  18. follows = (blocked_follows + domain_blocked_follows).uniq
  19. say "Destroying #{follows.size} blocked follow relationships..."
  20. follows.each do |follow|
  21. blocked_account = follow.account
  22. followed_account = follow.target_account
  23. next follow.destroy! if blocked_account.local?
  24. reject_follow_json = Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(follow, serializer: ActivityPub::RejectFollowSerializer, adapter: ActivityPub::Adapter).as_json).sign!(followed_account))
  25. ActivityPub::DeliveryWorker.perform_async(reject_follow_json, followed_account, blocked_account.inbox_url)
  26. follow.destroy!
  27. end
  28. end
  29. def down
  30. raise ActiveRecord::IrreversibleMigration
  31. end
  32. end