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.9 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. end
  16. return if status.account.silenced? || !status.public_visibility? || status.reblog?
  17. deliver_to_hashtags(status)
  18. return if status.reply? && status.in_reply_to_account_id != status.account_id
  19. deliver_to_public(status)
  20. end
  21. private
  22. def deliver_to_self(status)
  23. Rails.logger.debug "Delivering status #{status.id} to author"
  24. FeedManager.instance.push(: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.where(domain: nil).joins(:user).where('users.current_sign_in_at > ?', 14.days.ago).select(:id).reorder(nil).find_in_batches do |followers|
  29. FeedInsertWorker.push_bulk(followers) do |follower|
  30. [status.id, follower.id]
  31. end
  32. end
  33. end
  34. def deliver_to_mentioned_followers(status)
  35. Rails.logger.debug "Delivering status #{status.id} to mentioned followers"
  36. status.mentions.includes(:account).each do |mention|
  37. mentioned_account = mention.account
  38. next if !mentioned_account.local? || !mentioned_account.following?(status.account) || FeedManager.instance.filter?(:home, status, mention.account_id)
  39. FeedManager.instance.push(:home, mentioned_account, status)
  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_direct_timelines(status)
  59. Rails.logger.debug "Delivering status #{status.id} to direct timelines"
  60. status.mentions.includes(:account).each do |mention|
  61. Redis.current.publish("timeline:direct:#{mention.account.id}", @payload) if mention.account.local?
  62. end
  63. Redis.current.publish("timeline:direct:#{status.account.id}", @payload) if status.account.local?
  64. end
  65. end