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.

87 lines
2.6 KiB

7 years ago
7 years ago
7 years ago
7 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. render_anonymous_payload(status)
  8. if status.direct_visibility?
  9. deliver_to_own_conversation(status)
  10. else
  11. deliver_to_self(status) if status.account.local?
  12. deliver_to_followers(status)
  13. deliver_to_lists(status)
  14. end
  15. return if status.account.silenced? || !status.public_visibility? || status.reblog?
  16. deliver_to_hashtags(status)
  17. return if status.reply? && status.in_reply_to_account_id != status.account_id
  18. deliver_to_public(status)
  19. deliver_to_media(status) if status.media_attachments.any?
  20. end
  21. private
  22. def deliver_to_self(status)
  23. Rails.logger.debug "Delivering status #{status.id} to author"
  24. FeedManager.instance.push_to_home(status.account, status)
  25. end
  26. def deliver_to_followers(status)
  27. Rails.logger.debug "Delivering status #{status.id} to followers"
  28. status.account.followers_for_local_distribution.select(:id).reorder(nil).find_in_batches do |followers|
  29. FeedInsertWorker.push_bulk(followers) do |follower|
  30. [status.id, follower.id, :home]
  31. end
  32. end
  33. end
  34. def deliver_to_lists(status)
  35. Rails.logger.debug "Delivering status #{status.id} to lists"
  36. status.account.lists_for_local_distribution.select(:id).reorder(nil).find_in_batches do |lists|
  37. FeedInsertWorker.push_bulk(lists) do |list|
  38. [status.id, list.id, :list]
  39. end
  40. end
  41. end
  42. def render_anonymous_payload(status)
  43. @payload = InlineRenderer.render(status, nil, :status)
  44. @payload = Oj.dump(event: :update, payload: @payload)
  45. end
  46. def deliver_to_hashtags(status)
  47. Rails.logger.debug "Delivering status #{status.id} to hashtags"
  48. status.tags.pluck(:name).each do |hashtag|
  49. Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
  50. Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if status.local?
  51. end
  52. end
  53. def deliver_to_public(status)
  54. Rails.logger.debug "Delivering status #{status.id} to public timeline"
  55. Redis.current.publish('timeline:public', @payload)
  56. Redis.current.publish('timeline:public:local', @payload) if status.local?
  57. end
  58. def deliver_to_media(status)
  59. Rails.logger.debug "Delivering status #{status.id} to media timeline"
  60. Redis.current.publish('timeline:public:media', @payload)
  61. Redis.current.publish('timeline:public:local:media', @payload) if status.local?
  62. end
  63. def deliver_to_own_conversation(status)
  64. AccountConversation.add_status(status.account, status)
  65. end
  66. end