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.

110 lines
3.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class FanOutOnWriteService < BaseService
  3. # Push a status into home and mentions feeds
  4. # @param [Status] status
  5. def call(status)
  6. raise Mastodon::RaceConditionError if status.visibility.nil?
  7. deliver_to_self(status) if status.account.local?
  8. render_anonymous_payload(status)
  9. if status.direct_visibility?
  10. deliver_to_mentioned_followers(status)
  11. deliver_to_direct_timelines(status)
  12. deliver_to_own_conversation(status)
  13. else
  14. deliver_to_followers(status)
  15. deliver_to_lists(status)
  16. end
  17. return if status.account.silenced? || !status.public_visibility? || status.reblog?
  18. deliver_to_hashtags(status)
  19. return if status.reply? && status.in_reply_to_account_id != status.account_id
  20. deliver_to_public(status)
  21. deliver_to_media(status) if status.media_attachments.any?
  22. end
  23. private
  24. def deliver_to_self(status)
  25. Rails.logger.debug "Delivering status #{status.id} to author"
  26. FeedManager.instance.push_to_home(status.account, status)
  27. end
  28. def deliver_to_followers(status)
  29. Rails.logger.debug "Delivering status #{status.id} to followers"
  30. status.account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers|
  31. FeedInsertWorker.push_bulk(followers) do |follower|
  32. [status.id, follower.id, :home]
  33. end
  34. end
  35. end
  36. def deliver_to_lists(status)
  37. Rails.logger.debug "Delivering status #{status.id} to lists"
  38. status.account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists|
  39. FeedInsertWorker.push_bulk(lists) do |list|
  40. [status.id, list.id, :list]
  41. end
  42. end
  43. end
  44. def deliver_to_mentioned_followers(status)
  45. Rails.logger.debug "Delivering status #{status.id} to mentioned followers"
  46. status.mentions.includes(:account).each do |mention|
  47. mentioned_account = mention.account
  48. next if !mentioned_account.local? || !mentioned_account.following?(status.account) || FeedManager.instance.filter?(:home, status, mention.account_id)
  49. FeedManager.instance.push_to_home(mentioned_account, status)
  50. end
  51. end
  52. def render_anonymous_payload(status)
  53. @payload = InlineRenderer.render(status, nil, :status)
  54. @payload = Oj.dump(event: :update, payload: @payload)
  55. end
  56. def deliver_to_hashtags(status)
  57. Rails.logger.debug "Delivering status #{status.id} to hashtags"
  58. status.tags.pluck(:name).each do |hashtag|
  59. Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
  60. Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if status.local?
  61. end
  62. end
  63. def deliver_to_public(status)
  64. Rails.logger.debug "Delivering status #{status.id} to public timeline"
  65. Redis.current.publish('timeline:public', @payload)
  66. Redis.current.publish('timeline:public:local', @payload) if status.local?
  67. end
  68. def deliver_to_media(status)
  69. Rails.logger.debug "Delivering status #{status.id} to media timeline"
  70. Redis.current.publish('timeline:public:media', @payload)
  71. Redis.current.publish('timeline:public:local:media', @payload) if status.local?
  72. end
  73. def deliver_to_direct_timelines(status)
  74. Rails.logger.debug "Delivering status #{status.id} to direct timelines"
  75. status.mentions.includes(:account).each do |mention|
  76. Redis.current.publish("timeline:direct:#{mention.account.id}", @payload) if mention.account.local?
  77. end
  78. Redis.current.publish("timeline:direct:#{status.account.id}", @payload) if status.account.local?
  79. end
  80. def deliver_to_own_conversation(status)
  81. AccountConversation.add_status(status.account, status)
  82. end
  83. end