fan_out_on_write_service_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe FanOutOnWriteService 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 'adds status to home feed of author and followers and broadcasts', :inline_jobs do
  26. expect(status.id)
  27. .to be_in(home_feed_of(alice))
  28. .and be_in(home_feed_of(bob))
  29. .and be_in(home_feed_of(tom))
  30. expect(redis).to have_received(:publish).with('timeline:hashtag:hoge', anything)
  31. expect(redis).to have_received(:publish).with('timeline:hashtag:hoge:local', anything)
  32. expect(redis).to have_received(:publish).with('timeline:public', anything)
  33. expect(redis).to have_received(:publish).with('timeline:public:local', anything)
  34. expect(redis).to have_received(:publish).with('timeline:public:media', anything)
  35. end
  36. end
  37. context 'when status is limited' do
  38. let(:visibility) { 'limited' }
  39. it 'adds status to home feed of author and mentioned followers and does not broadcast', :inline_jobs do
  40. expect(status.id)
  41. .to be_in(home_feed_of(alice))
  42. .and be_in(home_feed_of(bob))
  43. expect(status.id)
  44. .to_not be_in(home_feed_of(tom))
  45. expect_no_broadcasting
  46. end
  47. end
  48. context 'when status is private' do
  49. let(:visibility) { 'private' }
  50. it 'adds status to home feed of author and followers and does not broadcast', :inline_jobs do
  51. expect(status.id)
  52. .to be_in(home_feed_of(alice))
  53. .and be_in(home_feed_of(bob))
  54. .and be_in(home_feed_of(tom))
  55. expect_no_broadcasting
  56. end
  57. end
  58. context 'when status is direct' do
  59. let(:visibility) { 'direct' }
  60. it 'is added to the home feed of its author and mentioned followers and does not broadcast', :inline_jobs do
  61. expect(status.id)
  62. .to be_in(home_feed_of(alice))
  63. .and be_in(home_feed_of(bob))
  64. expect(status.id)
  65. .to_not be_in(home_feed_of(tom))
  66. expect_no_broadcasting
  67. end
  68. context 'when handling status updates' do
  69. before do
  70. subject.call(status)
  71. status.snapshot!(at_time: status.created_at, rate_limit: false)
  72. status.update!(text: 'Hello @bob @eve #hoge (edited)')
  73. status.snapshot!(account_id: status.account_id)
  74. redis.set("subscribed:timeline:#{eve.id}:notifications", '1')
  75. end
  76. it 'pushes the update to mentioned users through the notifications streaming channel' do
  77. subject.call(status, update: true)
  78. expect(PushUpdateWorker).to have_enqueued_sidekiq_job(anything, status.id, "timeline:#{eve.id}:notifications", { 'update' => true })
  79. end
  80. end
  81. end
  82. def expect_no_broadcasting
  83. expect(redis)
  84. .to_not have_received(:publish)
  85. .with('timeline:hashtag:hoge', anything)
  86. expect(redis)
  87. .to_not have_received(:publish)
  88. .with('timeline:public', anything)
  89. end
  90. end