block_service.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # frozen_string_literal: true
  2. class BlockService < BaseService
  3. def call(account, target_account)
  4. return if account.id == target_account.id
  5. UnfollowService.new.call(account, target_account) if account.following?(target_account)
  6. UnfollowService.new.call(target_account, account) if target_account.following?(account)
  7. account.block!(target_account)
  8. clear_timelines(account, target_account)
  9. clear_notifications(account, target_account)
  10. end
  11. private
  12. def clear_timelines(account, target_account)
  13. mentions_key = FeedManager.instance.key(:mentions, account.id)
  14. home_key = FeedManager.instance.key(:home, account.id)
  15. target_account.statuses.select('id').find_each do |status|
  16. redis.zrem(mentions_key, status.id)
  17. redis.zrem(home_key, status.id)
  18. end
  19. end
  20. def clear_notifications(account, target_account)
  21. Notification.where(account: account).joins(:follow).where(activity_type: 'Follow', follows: { account_id: target_account.id }).destroy_all
  22. Notification.where(account: account).joins(mention: :status).where(activity_type: 'Mention', statuses: { account_id: target_account.id }).destroy_all
  23. Notification.where(account: account).joins(:favourite).where(activity_type: 'Favourite', favourites: { account_id: target_account.id }).destroy_all
  24. Notification.where(account: account).joins(:status).where(activity_type: 'Status', statuses: { account_id: target_account.id }).destroy_all
  25. end
  26. def redis
  27. Redis.current
  28. end
  29. end