feed_manager_spec.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. require 'rails_helper'
  2. RSpec.describe FeedManager do
  3. describe '#key' do
  4. subject { FeedManager.instance.key(:home, 1) }
  5. it 'returns a string' do
  6. expect(subject).to be_a String
  7. end
  8. end
  9. describe '#filter?' do
  10. let(:alice) { Fabricate(:account, username: 'alice') }
  11. let(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
  12. let(:jeff) { Fabricate(:account, username: 'jeff') }
  13. context 'for home feed' do
  14. it 'returns false for followee\'s status' do
  15. status = Fabricate(:status, text: 'Hello world', account: alice)
  16. bob.follow!(alice)
  17. expect(FeedManager.instance.filter?(:home, status, bob.id)).to be false
  18. end
  19. it 'returns false for reblog by followee' do
  20. status = Fabricate(:status, text: 'Hello world', account: jeff)
  21. reblog = Fabricate(:status, reblog: status, account: alice)
  22. bob.follow!(alice)
  23. expect(FeedManager.instance.filter?(:home, reblog, bob.id)).to be false
  24. end
  25. it 'returns true for reblog by followee of blocked account' do
  26. status = Fabricate(:status, text: 'Hello world', account: jeff)
  27. reblog = Fabricate(:status, reblog: status, account: alice)
  28. bob.follow!(alice)
  29. bob.block!(jeff)
  30. expect(FeedManager.instance.filter?(:home, reblog, bob.id)).to be true
  31. end
  32. it 'returns true for reblog by followee of muted account' do
  33. status = Fabricate(:status, text: 'Hello world', account: jeff)
  34. reblog = Fabricate(:status, reblog: status, account: alice)
  35. bob.follow!(alice)
  36. bob.mute!(jeff)
  37. expect(FeedManager.instance.filter?(:home, reblog, bob.id)).to be true
  38. end
  39. it 'returns true for reblog by followee of someone who is blocking recipient' do
  40. status = Fabricate(:status, text: 'Hello world', account: jeff)
  41. reblog = Fabricate(:status, reblog: status, account: alice)
  42. bob.follow!(alice)
  43. jeff.block!(bob)
  44. expect(FeedManager.instance.filter?(:home, reblog, bob.id)).to be true
  45. end
  46. it 'returns false for reply by followee to another followee' do
  47. status = Fabricate(:status, text: 'Hello world', account: jeff)
  48. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  49. bob.follow!(alice)
  50. bob.follow!(jeff)
  51. expect(FeedManager.instance.filter?(:home, reply, bob.id)).to be false
  52. end
  53. it 'returns false for reply by followee to recipient' do
  54. status = Fabricate(:status, text: 'Hello world', account: bob)
  55. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  56. bob.follow!(alice)
  57. expect(FeedManager.instance.filter?(:home, reply, bob.id)).to be false
  58. end
  59. it 'returns false for reply by followee to self' do
  60. status = Fabricate(:status, text: 'Hello world', account: alice)
  61. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  62. bob.follow!(alice)
  63. expect(FeedManager.instance.filter?(:home, reply, bob.id)).to be false
  64. end
  65. it 'returns true for reply by followee to non-followed account' do
  66. status = Fabricate(:status, text: 'Hello world', account: jeff)
  67. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  68. bob.follow!(alice)
  69. expect(FeedManager.instance.filter?(:home, reply, bob.id)).to be true
  70. end
  71. it 'returns true for the second reply by followee to a non-federated status' do
  72. reply = Fabricate(:status, text: 'Reply 1', reply: true, account: alice)
  73. second_reply = Fabricate(:status, text: 'Reply 2', thread: reply, account: alice)
  74. bob.follow!(alice)
  75. expect(FeedManager.instance.filter?(:home, second_reply, bob.id)).to be true
  76. end
  77. it 'returns false for status by followee mentioning another account' do
  78. bob.follow!(alice)
  79. status = PostStatusService.new.call(alice, 'Hey @jeff')
  80. expect(FeedManager.instance.filter?(:home, status, bob.id)).to be false
  81. end
  82. it 'returns true for status by followee mentioning blocked account' do
  83. bob.block!(jeff)
  84. bob.follow!(alice)
  85. status = PostStatusService.new.call(alice, 'Hey @jeff')
  86. expect(FeedManager.instance.filter?(:home, status, bob.id)).to be true
  87. end
  88. it 'returns true for reblog of a personally blocked domain' do
  89. alice.block_domain!('example.com')
  90. alice.follow!(jeff)
  91. status = Fabricate(:status, text: 'Hello world', account: bob)
  92. reblog = Fabricate(:status, reblog: status, account: jeff)
  93. expect(FeedManager.instance.filter?(:home, reblog, alice.id)).to be true
  94. end
  95. end
  96. context 'for mentions feed' do
  97. it 'returns true for status that mentions blocked account' do
  98. bob.block!(jeff)
  99. status = PostStatusService.new.call(alice, 'Hey @jeff')
  100. expect(FeedManager.instance.filter?(:mentions, status, bob.id)).to be true
  101. end
  102. it 'returns true for status that replies to a blocked account' do
  103. status = Fabricate(:status, text: 'Hello world', account: jeff)
  104. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  105. bob.block!(jeff)
  106. expect(FeedManager.instance.filter?(:mentions, reply, bob.id)).to be true
  107. end
  108. it 'returns true for status by silenced account who recipient is not following' do
  109. status = Fabricate(:status, text: 'Hello world', account: alice)
  110. alice.update(silenced: true)
  111. expect(FeedManager.instance.filter?(:mentions, status, bob.id)).to be true
  112. end
  113. it 'returns false for status by followed silenced account' do
  114. status = Fabricate(:status, text: 'Hello world', account: alice)
  115. alice.update(silenced: true)
  116. bob.follow!(alice)
  117. expect(FeedManager.instance.filter?(:mentions, status, bob.id)).to be false
  118. end
  119. end
  120. end
  121. describe '#push' do
  122. it 'trims timelines if they will have more than FeedManager::MAX_ITEMS' do
  123. account = Fabricate(:account)
  124. status = Fabricate(:status)
  125. members = FeedManager::MAX_ITEMS.times.map { |count| [count, count] }
  126. Redis.current.zadd("feed:type:#{account.id}", members)
  127. FeedManager.instance.push('type', account, status)
  128. expect(Redis.current.zcard("feed:type:#{account.id}")).to eq FeedManager::MAX_ITEMS
  129. end
  130. end
  131. end