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.

84 lines
2.2 KiB

  1. # frozen_string_literal: true
  2. class RemoveStatusService < BaseService
  3. include StreamEntryRenderer
  4. def call(status)
  5. remove_from_self(status) if status.account.local?
  6. remove_from_followers(status)
  7. remove_from_mentioned(status)
  8. remove_reblogs(status)
  9. remove_from_hashtags(status)
  10. remove_from_public(status)
  11. status.destroy!
  12. return unless status.account.local?
  13. Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)
  14. end
  15. private
  16. def remove_from_self(status)
  17. unpush(:home, status.account, status)
  18. end
  19. def remove_from_followers(status)
  20. status.account.followers.each do |follower|
  21. next unless follower.local?
  22. unpush(:home, follower, status)
  23. end
  24. end
  25. def remove_from_mentioned(status)
  26. notified_domains = []
  27. status.mentions.each do |mention|
  28. mentioned_account = mention.account
  29. if mentioned_account.local?
  30. unpush(:mentions, mentioned_account, status)
  31. else
  32. next if notified_domains.include?(mentioned_account.domain)
  33. notified_domains << mentioned_account.domain
  34. send_delete_salmon(mentioned_account, status)
  35. end
  36. end
  37. end
  38. def send_delete_salmon(account, status)
  39. return unless status.local?
  40. NotificationWorker.perform_async(stream_entry_to_xml(status.stream_entry), status.account_id, account.id)
  41. end
  42. def remove_reblogs(status)
  43. status.reblogs.each do |reblog|
  44. RemoveStatusService.new.call(reblog)
  45. end
  46. end
  47. def unpush(type, receiver, status)
  48. if status.reblog? && !redis.zscore(FeedManager.instance.key(type, receiver.id), status.reblog_of_id).nil?
  49. redis.zadd(FeedManager.instance.key(type, receiver.id), status.reblog_of_id, status.reblog_of_id)
  50. else
  51. redis.zremrangebyscore(FeedManager.instance.key(type, receiver.id), status.id, status.id)
  52. end
  53. FeedManager.instance.broadcast(receiver.id, event: 'delete', payload: status.id)
  54. end
  55. def remove_from_hashtags(status)
  56. status.tags.each do |tag|
  57. FeedManager.instance.broadcast("hashtag:#{tag.name}", event: 'delete', payload: status.id)
  58. end
  59. end
  60. def remove_from_public(status)
  61. FeedManager.instance.broadcast(:public, event: 'delete', payload: status.id)
  62. end
  63. def redis
  64. Redis.current
  65. end
  66. end