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

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