logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

fan_out_on_write_service.rb (2881B)


  1. # frozen_string_literal: true
  2. require 'sidekiq-bulk'
  3. class FanOutOnWriteService < BaseService
  4. # Push a status into home and mentions feeds
  5. # @param [Status] status
  6. def call(status)
  7. raise Mastodon::RaceConditionError if status.visibility.nil?
  8. deliver_to_self(status) if status.account.local?
  9. if status.direct_visibility?
  10. deliver_to_mentioned_followers(status)
  11. else
  12. deliver_to_followers(status)
  13. deliver_to_lists(status)
  14. end
  15. return if status.account.silenced? || !status.public_visibility? || status.reblog?
  16. render_anonymous_payload(status)
  17. deliver_to_hashtags(status)
  18. return if status.reply? && status.in_reply_to_account_id != status.account_id
  19. deliver_to_public(status)
  20. end
  21. private
  22. def deliver_to_self(status)
  23. Rails.logger.debug "Delivering status #{status.id} to author"
  24. FeedManager.instance.push_to_home(status.account, status)
  25. end
  26. def deliver_to_followers(status)
  27. Rails.logger.debug "Delivering status #{status.id} to followers"
  28. status.account.followers.where(domain: nil).joins(:user).where('users.current_sign_in_at > ?', 14.days.ago).select(:id).reorder(nil).find_in_batches do |followers|
  29. FeedInsertWorker.push_bulk(followers) do |follower|
  30. [status.id, follower.id, :home]
  31. end
  32. end
  33. end
  34. def deliver_to_lists(status)
  35. Rails.logger.debug "Delivering status #{status.id} to lists"
  36. status.account.lists.joins(account: :user).where('users.current_sign_in_at > ?', 14.days.ago).select(:id).reorder(nil).find_in_batches do |lists|
  37. FeedInsertWorker.push_bulk(lists) do |list|
  38. [status.id, list.id, :list]
  39. end
  40. end
  41. end
  42. def deliver_to_mentioned_followers(status)
  43. Rails.logger.debug "Delivering status #{status.id} to mentioned followers"
  44. status.mentions.includes(:account).each do |mention|
  45. mentioned_account = mention.account
  46. next if !mentioned_account.local? || !mentioned_account.following?(status.account) || FeedManager.instance.filter?(:home, status, mention.account_id)
  47. FeedManager.instance.push_to_home(mentioned_account, status)
  48. end
  49. end
  50. def render_anonymous_payload(status)
  51. @payload = InlineRenderer.render(status, nil, :status)
  52. @payload = Oj.dump(event: :update, payload: @payload)
  53. end
  54. def deliver_to_hashtags(status)
  55. Rails.logger.debug "Delivering status #{status.id} to hashtags"
  56. status.tags.pluck(:name).each do |hashtag|
  57. Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
  58. Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if status.local?
  59. end
  60. end
  61. def deliver_to_public(status)
  62. Rails.logger.debug "Delivering status #{status.id} to public timeline"
  63. Redis.current.publish('timeline:public', @payload)
  64. Redis.current.publish('timeline:public:local', @payload) if status.local?
  65. end
  66. end