20180608213548_reject_following_blocked_users.rb 1.3 KB

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