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.

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