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.

76 lines
2.4 KiB

8 years ago
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. if status.direct_visibility?
  10. deliver_to_mentioned_followers(status)
  11. else
  12. deliver_to_followers(status)
  13. end
  14. return if status.account.silenced? || !status.public_visibility? || status.reblog?
  15. render_anonymous_payload(status)
  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. end
  20. private
  21. def deliver_to_self(status)
  22. Rails.logger.debug "Delivering status #{status.id} to author"
  23. FeedManager.instance.push(:home, status.account, status)
  24. end
  25. def deliver_to_followers(status)
  26. Rails.logger.debug "Delivering status #{status.id} to followers"
  27. 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|
  28. FeedInsertWorker.push_bulk(followers) do |follower|
  29. [status.id, follower.id]
  30. end
  31. end
  32. end
  33. def deliver_to_mentioned_followers(status)
  34. Rails.logger.debug "Delivering status #{status.id} to mentioned followers"
  35. status.mentions.includes(:account).each do |mention|
  36. mentioned_account = mention.account
  37. next if !mentioned_account.local? || !mentioned_account.following?(status.account) || FeedManager.instance.filter?(:home, status, mention.account_id)
  38. FeedManager.instance.push(:home, mentioned_account, status)
  39. end
  40. end
  41. def render_anonymous_payload(status)
  42. @payload = InlineRenderer.render(status, nil, 'api/v1/statuses/show')
  43. @payload = Oj.dump(event: :update, payload: @payload)
  44. end
  45. def deliver_to_hashtags(status)
  46. Rails.logger.debug "Delivering status #{status.id} to hashtags"
  47. status.tags.pluck(:name).each do |hashtag|
  48. Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
  49. Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if status.local?
  50. end
  51. end
  52. def deliver_to_public(status)
  53. Rails.logger.debug "Delivering status #{status.id} to public timeline"
  54. Redis.current.publish('timeline:public', @payload)
  55. Redis.current.publish('timeline:public:local', @payload) if status.local?
  56. end
  57. end