fan_out_on_write_service_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe FanOutOnWriteService, type: :service do
  4. subject { described_class.new }
  5. let(:last_active_at) { Time.now.utc }
  6. let(:status) { Fabricate(:status, account: alice, visibility: visibility, text: 'Hello @bob @eve #hoge') }
  7. let!(:alice) { Fabricate(:user, current_sign_in_at: last_active_at).account }
  8. let!(:bob) { Fabricate(:user, current_sign_in_at: last_active_at, account_attributes: { username: 'bob' }).account }
  9. let!(:tom) { Fabricate(:user, current_sign_in_at: last_active_at).account }
  10. let!(:eve) { Fabricate(:user, current_sign_in_at: last_active_at, account_attributes: { username: 'eve' }).account }
  11. before do
  12. bob.follow!(alice)
  13. tom.follow!(alice)
  14. ProcessMentionsService.new.call(status)
  15. ProcessHashtagsService.new.call(status)
  16. Fabricate(:media_attachment, status: status, account: alice)
  17. allow(redis).to receive(:publish)
  18. subject.call(status)
  19. end
  20. def home_feed_of(account)
  21. HomeFeed.new(account).get(10).map(&:id)
  22. end
  23. context 'when status is public' do
  24. let(:visibility) { 'public' }
  25. it 'is added to the home feed of its author' do
  26. expect(home_feed_of(alice)).to include status.id
  27. end
  28. it 'is added to the home feed of a follower', :sidekiq_inline do
  29. expect(home_feed_of(bob)).to include status.id
  30. expect(home_feed_of(tom)).to include status.id
  31. end
  32. it 'is broadcast to the hashtag stream' do
  33. expect(redis).to have_received(:publish).with('timeline:hashtag:hoge', anything)
  34. expect(redis).to have_received(:publish).with('timeline:hashtag:hoge:local', anything)
  35. end
  36. it 'is broadcast to the public stream' do
  37. expect(redis).to have_received(:publish).with('timeline:public', anything)
  38. expect(redis).to have_received(:publish).with('timeline:public:local', anything)
  39. expect(redis).to have_received(:publish).with('timeline:public:media', anything)
  40. end
  41. end
  42. context 'when status is limited' do
  43. let(:visibility) { 'limited' }
  44. it 'is added to the home feed of its author' do
  45. expect(home_feed_of(alice)).to include status.id
  46. end
  47. it 'is added to the home feed of the mentioned follower', :sidekiq_inline do
  48. expect(home_feed_of(bob)).to include status.id
  49. end
  50. it 'is not added to the home feed of the other follower' do
  51. expect(home_feed_of(tom)).to_not include status.id
  52. end
  53. it 'is not broadcast publicly' do
  54. expect(redis).to_not have_received(:publish).with('timeline:hashtag:hoge', anything)
  55. expect(redis).to_not have_received(:publish).with('timeline:public', anything)
  56. end
  57. end
  58. context 'when status is private' do
  59. let(:visibility) { 'private' }
  60. it 'is added to the home feed of its author' do
  61. expect(home_feed_of(alice)).to include status.id
  62. end
  63. it 'is added to the home feed of a follower', :sidekiq_inline do
  64. expect(home_feed_of(bob)).to include status.id
  65. expect(home_feed_of(tom)).to include status.id
  66. end
  67. it 'is not broadcast publicly' do
  68. expect(redis).to_not have_received(:publish).with('timeline:hashtag:hoge', anything)
  69. expect(redis).to_not have_received(:publish).with('timeline:public', anything)
  70. end
  71. end
  72. context 'when status is direct' do
  73. let(:visibility) { 'direct' }
  74. it 'is added to the home feed of its author' do
  75. expect(home_feed_of(alice)).to include status.id
  76. end
  77. it 'is added to the home feed of the mentioned follower', :sidekiq_inline do
  78. expect(home_feed_of(bob)).to include status.id
  79. end
  80. it 'is not added to the home feed of the other follower' do
  81. expect(home_feed_of(tom)).to_not include status.id
  82. end
  83. it 'is not broadcast publicly' do
  84. expect(redis).to_not have_received(:publish).with('timeline:hashtag:hoge', anything)
  85. expect(redis).to_not have_received(:publish).with('timeline:public', anything)
  86. end
  87. context 'when handling status updates' do
  88. before do
  89. subject.call(status)
  90. status.snapshot!(at_time: status.created_at, rate_limit: false)
  91. status.update!(text: 'Hello @bob @eve #hoge (edited)')
  92. status.snapshot!(account_id: status.account_id)
  93. redis.set("subscribed:timeline:#{eve.id}:notifications", '1')
  94. end
  95. it 'pushes the update to mentioned users through the notifications streaming channel' do
  96. subject.call(status, update: true)
  97. expect(PushUpdateWorker).to have_enqueued_sidekiq_job(anything, status.id, "timeline:#{eve.id}:notifications", { 'update' => true })
  98. end
  99. end
  100. end
  101. end