闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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