The code powering m.abunchtell.com https://m.abunchtell.com
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

  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. end
  41. def deliver_to_hashtags(status)
  42. Rails.logger.debug "Delivering status #{status.id} to hashtags"
  43. status.tags.pluck(:name).each do |hashtag|
  44. Redis.current.publish("hashtag:#{hashtag}", Oj.dump(event: :update, payload: @payload))
  45. Redis.current.publish("hashtag:#{hashtag}:local", Oj.dump(event: :update, payload: @payload)) if status.account.local?
  46. end
  47. end
  48. def deliver_to_public(status)
  49. Rails.logger.debug "Delivering status #{status.id} to public timeline"
  50. Redis.current.publish('public', Oj.dump(event: 'update', payload: @payload))
  51. Redis.current.publish('public:local', Oj.dump(event: 'update', payload: @payload)) if status.account.local?
  52. end
  53. end