闭社主体 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.

69 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. deliver_to_self(status) if status.account.local?
  7. if status.direct_visibility?
  8. deliver_to_mentioned_followers(status)
  9. else
  10. deliver_to_followers(status)
  11. end
  12. return if status.account.silenced? || !status.public_visibility? || status.reblog?
  13. deliver_to_hashtags(status)
  14. return if status.reply? && status.in_reply_to_account_id != status.account_id
  15. deliver_to_public(status)
  16. end
  17. private
  18. def deliver_to_self(status)
  19. Rails.logger.debug "Delivering status #{status.id} to author"
  20. FeedManager.instance.push(:home, status.account, status)
  21. end
  22. def deliver_to_followers(status)
  23. Rails.logger.debug "Delivering status #{status.id} to followers"
  24. status.account.followers.where(domain: nil).joins(:user).where('users.current_sign_in_at > ?', 14.days.ago).find_each do |follower|
  25. next if FeedManager.instance.filter?(:home, status, follower)
  26. FeedManager.instance.push(:home, follower, status)
  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, mentioned_account)
  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.find_each do |tag|
  41. FeedManager.instance.broadcast("hashtag:#{tag.name}", event: 'update', payload: payload)
  42. FeedManager.instance.broadcast("hashtag:#{tag.name}: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