闭社主体 forked from https://github.com/tootsuite/mastodon
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.

70 lines
2.4 KiB

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