闭社主体 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.

86 lines
2.3 KiB

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