fan_out_on_write_service_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 #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. before do
  11. bob.follow!(alice)
  12. tom.follow!(alice)
  13. ProcessMentionsService.new.call(status)
  14. ProcessHashtagsService.new.call(status)
  15. allow(redis).to receive(:publish)
  16. subject.call(status)
  17. end
  18. def home_feed_of(account)
  19. HomeFeed.new(account).get(10).map(&:id)
  20. end
  21. context 'when status is public' do
  22. let(:visibility) { 'public' }
  23. it 'is added to the home feed of its author' do
  24. expect(home_feed_of(alice)).to include status.id
  25. end
  26. it 'is added to the home feed of a follower' do
  27. expect(home_feed_of(bob)).to include status.id
  28. expect(home_feed_of(tom)).to include status.id
  29. end
  30. it 'is broadcast to the hashtag stream' do
  31. expect(redis).to have_received(:publish).with('timeline:hashtag:hoge', anything)
  32. expect(redis).to have_received(:publish).with('timeline:hashtag:hoge:local', anything)
  33. end
  34. it 'is broadcast to the public stream' do
  35. expect(redis).to have_received(:publish).with('timeline:public', anything)
  36. expect(redis).to have_received(:publish).with('timeline:public:local', anything)
  37. end
  38. end
  39. context 'when status is limited' do
  40. let(:visibility) { 'limited' }
  41. it 'is added to the home feed of its author' do
  42. expect(home_feed_of(alice)).to include status.id
  43. end
  44. it 'is added to the home feed of the mentioned follower' do
  45. expect(home_feed_of(bob)).to include status.id
  46. end
  47. it 'is not added to the home feed of the other follower' do
  48. expect(home_feed_of(tom)).to_not include status.id
  49. end
  50. it 'is not broadcast publicly' do
  51. expect(redis).to_not have_received(:publish).with('timeline:hashtag:hoge', anything)
  52. expect(redis).to_not have_received(:publish).with('timeline:public', anything)
  53. end
  54. end
  55. context 'when status is private' do
  56. let(:visibility) { 'private' }
  57. it 'is added to the home feed of its author' do
  58. expect(home_feed_of(alice)).to include status.id
  59. end
  60. it 'is added to the home feed of a follower' do
  61. expect(home_feed_of(bob)).to include status.id
  62. expect(home_feed_of(tom)).to include status.id
  63. end
  64. it 'is not broadcast publicly' do
  65. expect(redis).to_not have_received(:publish).with('timeline:hashtag:hoge', anything)
  66. expect(redis).to_not have_received(:publish).with('timeline:public', anything)
  67. end
  68. end
  69. context 'when status is direct' do
  70. let(:visibility) { 'direct' }
  71. it 'is added to the home feed of its author' do
  72. expect(home_feed_of(alice)).to include status.id
  73. end
  74. it 'is added to the home feed of the mentioned follower' do
  75. expect(home_feed_of(bob)).to include status.id
  76. end
  77. it 'is not added to the home feed of the other follower' do
  78. expect(home_feed_of(tom)).to_not include status.id
  79. end
  80. it 'is not broadcast publicly' do
  81. expect(redis).to_not have_received(:publish).with('timeline:hashtag:hoge', anything)
  82. expect(redis).to_not have_received(:publish).with('timeline:public', anything)
  83. end
  84. end
  85. end