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.

98 lines
3.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. require 'sidekiq-bulk'
  3. class FanOutOnWriteService < BaseService
  4. # Push a status into home and mentions feeds
  5. # @param [Status] status
  6. def call(status)
  7. raise Mastodon::RaceConditionError if status.visibility.nil?
  8. deliver_to_self(status) if status.account.local?
  9. render_anonymous_payload(status)
  10. if status.direct_visibility?
  11. deliver_to_mentioned_followers(status)
  12. deliver_to_direct_timelines(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. end
  22. private
  23. def deliver_to_self(status)
  24. Rails.logger.debug "Delivering status #{status.id} to author"
  25. FeedManager.instance.push_to_home(status.account, status)
  26. end
  27. def deliver_to_followers(status)
  28. Rails.logger.debug "Delivering status #{status.id} to followers"
  29. status.account.followers.where(domain: nil).joins(:user).where('users.current_sign_in_at > ?', 14.days.ago).select(:id).reorder(nil).find_in_batches do |followers|
  30. FeedInsertWorker.push_bulk(followers) do |follower|
  31. [status.id, follower.id, :home]
  32. end
  33. end
  34. end
  35. def deliver_to_lists(status)
  36. Rails.logger.debug "Delivering status #{status.id} to lists"
  37. status.account.lists.joins(account: :user).where('users.current_sign_in_at > ?', 14.days.ago).select(:id).reorder(nil).find_in_batches do |lists|
  38. FeedInsertWorker.push_bulk(lists) do |list|
  39. [status.id, list.id, :list]
  40. end
  41. end
  42. end
  43. def deliver_to_mentioned_followers(status)
  44. Rails.logger.debug "Delivering status #{status.id} to mentioned followers"
  45. status.mentions.includes(:account).each do |mention|
  46. mentioned_account = mention.account
  47. next if !mentioned_account.local? || !mentioned_account.following?(status.account) || FeedManager.instance.filter?(:home, status, mention.account_id)
  48. FeedManager.instance.push_to_home(mentioned_account, status)
  49. end
  50. end
  51. def render_anonymous_payload(status)
  52. @payload = InlineRenderer.render(status, nil, :status)
  53. @payload = Oj.dump(event: :update, payload: @payload)
  54. end
  55. def deliver_to_hashtags(status)
  56. Rails.logger.debug "Delivering status #{status.id} to hashtags"
  57. status.tags.pluck(:name).each do |hashtag|
  58. Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
  59. Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if status.local?
  60. end
  61. end
  62. def deliver_to_public(status)
  63. Rails.logger.debug "Delivering status #{status.id} to public timeline"
  64. Redis.current.publish('timeline:public', @payload)
  65. Redis.current.publish('timeline:public:local', @payload) if status.local?
  66. end
  67. def deliver_to_direct_timelines(status)
  68. Rails.logger.debug "Delivering status #{status.id} to direct timelines"
  69. status.mentions.includes(:account).each do |mention|
  70. Redis.current.publish("timeline:direct:#{mention.account.id}", @payload) if mention.account.local?
  71. end
  72. Redis.current.publish("timeline:direct:#{status.account.id}", @payload) if status.account.local?
  73. end
  74. end