You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.6 KiB

  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. block = account.block!(target_account)
  8. clear_timelines(account, target_account)
  9. clear_notifications(account, target_account)
  10. NotificationWorker.perform_async(block.stream_entry.id, target_account.id) unless target_account.local?
  11. end
  12. private
  13. def clear_timelines(account, target_account)
  14. mentions_key = FeedManager.instance.key(:mentions, account.id)
  15. home_key = FeedManager.instance.key(:home, account.id)
  16. target_account.statuses.select('id').find_each do |status|
  17. redis.zrem(mentions_key, status.id)
  18. redis.zrem(home_key, status.id)
  19. end
  20. end
  21. def clear_notifications(account, target_account)
  22. Notification.where(account: account).joins(:follow).where(activity_type: 'Follow', follows: { account_id: target_account.id }).destroy_all
  23. Notification.where(account: account).joins(mention: :status).where(activity_type: 'Mention', statuses: { account_id: target_account.id }).destroy_all
  24. Notification.where(account: account).joins(:favourite).where(activity_type: 'Favourite', favourites: { account_id: target_account.id }).destroy_all
  25. Notification.where(account: account).joins(:status).where(activity_type: 'Status', statuses: { account_id: target_account.id }).destroy_all
  26. end
  27. def redis
  28. Redis.current
  29. end
  30. end