feed_manager_spec.rb 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. require 'rails_helper'
  2. RSpec.describe FeedManager do
  3. before do |example|
  4. unless example.metadata[:skip_stub]
  5. stub_const 'FeedManager::MAX_ITEMS', 10
  6. stub_const 'FeedManager::REBLOG_FALLOFF', 4
  7. end
  8. end
  9. it 'tracks at least as many statuses as reblogs', skip_stub: true do
  10. expect(FeedManager::REBLOG_FALLOFF).to be <= FeedManager::MAX_ITEMS
  11. end
  12. describe '#key' do
  13. subject { FeedManager.instance.key(:home, 1) }
  14. it 'returns a string' do
  15. expect(subject).to be_a String
  16. end
  17. end
  18. describe '#filter?' do
  19. let(:alice) { Fabricate(:account, username: 'alice') }
  20. let(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
  21. let(:jeff) { Fabricate(:account, username: 'jeff') }
  22. context 'for home feed' do
  23. it 'returns false for followee\'s status' do
  24. status = Fabricate(:status, text: 'Hello world', account: alice)
  25. bob.follow!(alice)
  26. expect(FeedManager.instance.filter?(:home, status, bob)).to be false
  27. end
  28. it 'returns false for reblog by followee' do
  29. status = Fabricate(:status, text: 'Hello world', account: jeff)
  30. reblog = Fabricate(:status, reblog: status, account: alice)
  31. bob.follow!(alice)
  32. expect(FeedManager.instance.filter?(:home, reblog, bob)).to be false
  33. end
  34. it 'returns true for post from account who blocked me' do
  35. status = Fabricate(:status, text: 'Hello, World', account: alice)
  36. alice.block!(bob)
  37. expect(FeedManager.instance.filter?(:home, status, bob)).to be true
  38. end
  39. it 'returns true for post from blocked account' do
  40. status = Fabricate(:status, text: 'Hello, World', account: alice)
  41. bob.block!(alice)
  42. expect(FeedManager.instance.filter?(:home, status, bob)).to be true
  43. end
  44. it 'returns true for reblog by followee of blocked account' do
  45. status = Fabricate(:status, text: 'Hello world', account: jeff)
  46. reblog = Fabricate(:status, reblog: status, account: alice)
  47. bob.follow!(alice)
  48. bob.block!(jeff)
  49. expect(FeedManager.instance.filter?(:home, reblog, bob)).to be true
  50. end
  51. it 'returns true for reblog by followee of muted account' do
  52. status = Fabricate(:status, text: 'Hello world', account: jeff)
  53. reblog = Fabricate(:status, reblog: status, account: alice)
  54. bob.follow!(alice)
  55. bob.mute!(jeff)
  56. expect(FeedManager.instance.filter?(:home, reblog, bob)).to be true
  57. end
  58. it 'returns true for reblog by followee of someone who is blocking recipient' do
  59. status = Fabricate(:status, text: 'Hello world', account: jeff)
  60. reblog = Fabricate(:status, reblog: status, account: alice)
  61. bob.follow!(alice)
  62. jeff.block!(bob)
  63. expect(FeedManager.instance.filter?(:home, reblog, bob)).to be true
  64. end
  65. it 'returns true for reblog from account with reblogs disabled' do
  66. status = Fabricate(:status, text: 'Hello world', account: jeff)
  67. reblog = Fabricate(:status, reblog: status, account: alice)
  68. bob.follow!(alice, reblogs: false)
  69. expect(FeedManager.instance.filter?(:home, reblog, bob)).to be true
  70. end
  71. it 'returns false for reply by followee to another followee' do
  72. status = Fabricate(:status, text: 'Hello world', account: jeff)
  73. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  74. bob.follow!(alice)
  75. bob.follow!(jeff)
  76. expect(FeedManager.instance.filter?(:home, reply, bob)).to be false
  77. end
  78. it 'returns false for reply by followee to recipient' do
  79. status = Fabricate(:status, text: 'Hello world', account: bob)
  80. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  81. bob.follow!(alice)
  82. expect(FeedManager.instance.filter?(:home, reply, bob)).to be false
  83. end
  84. it 'returns false for reply by followee to self' do
  85. status = Fabricate(:status, text: 'Hello world', account: alice)
  86. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  87. bob.follow!(alice)
  88. expect(FeedManager.instance.filter?(:home, reply, bob)).to be false
  89. end
  90. it 'returns true for reply by followee to non-followed account' do
  91. status = Fabricate(:status, text: 'Hello world', account: jeff)
  92. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  93. bob.follow!(alice)
  94. expect(FeedManager.instance.filter?(:home, reply, bob)).to be true
  95. end
  96. it 'returns true for the second reply by followee to a non-federated status' do
  97. reply = Fabricate(:status, text: 'Reply 1', reply: true, account: alice)
  98. second_reply = Fabricate(:status, text: 'Reply 2', thread: reply, account: alice)
  99. bob.follow!(alice)
  100. expect(FeedManager.instance.filter?(:home, second_reply, bob)).to be true
  101. end
  102. it 'returns false for status by followee mentioning another account' do
  103. bob.follow!(alice)
  104. jeff.follow!(alice)
  105. status = PostStatusService.new.call(alice, text: 'Hey @jeff')
  106. expect(FeedManager.instance.filter?(:home, status, bob)).to be false
  107. end
  108. it 'returns true for status by followee mentioning blocked account' do
  109. bob.block!(jeff)
  110. bob.follow!(alice)
  111. status = PostStatusService.new.call(alice, text: 'Hey @jeff')
  112. expect(FeedManager.instance.filter?(:home, status, bob)).to be true
  113. end
  114. it 'returns true for reblog of a personally blocked domain' do
  115. alice.block_domain!('example.com')
  116. alice.follow!(jeff)
  117. status = Fabricate(:status, text: 'Hello world', account: bob)
  118. reblog = Fabricate(:status, reblog: status, account: jeff)
  119. expect(FeedManager.instance.filter?(:home, reblog, alice)).to be true
  120. end
  121. it 'returns true for German post when follow is set to English only' do
  122. alice.follow!(bob, languages: %w(en))
  123. status = Fabricate(:status, text: 'Hallo Welt', account: bob, language: 'de')
  124. expect(FeedManager.instance.filter?(:home, status, alice)).to be true
  125. end
  126. it 'returns false for German post when follow is set to German' do
  127. alice.follow!(bob, languages: %w(de))
  128. status = Fabricate(:status, text: 'Hallo Welt', account: bob, language: 'de')
  129. expect(FeedManager.instance.filter?(:home, status, alice)).to be false
  130. end
  131. end
  132. context 'for mentions feed' do
  133. it 'returns true for status that mentions blocked account' do
  134. bob.block!(jeff)
  135. status = PostStatusService.new.call(alice, text: 'Hey @jeff')
  136. expect(FeedManager.instance.filter?(:mentions, status, bob)).to be true
  137. end
  138. it 'returns true for status that replies to a blocked account' do
  139. status = Fabricate(:status, text: 'Hello world', account: jeff)
  140. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  141. bob.block!(jeff)
  142. expect(FeedManager.instance.filter?(:mentions, reply, bob)).to be true
  143. end
  144. it 'returns true for status by silenced account who recipient is not following' do
  145. status = Fabricate(:status, text: 'Hello world', account: alice)
  146. alice.silence!
  147. expect(FeedManager.instance.filter?(:mentions, status, bob)).to be true
  148. end
  149. it 'returns false for status by followed silenced account' do
  150. status = Fabricate(:status, text: 'Hello world', account: alice)
  151. alice.silence!
  152. bob.follow!(alice)
  153. expect(FeedManager.instance.filter?(:mentions, status, bob)).to be false
  154. end
  155. end
  156. end
  157. describe '#push_to_home' do
  158. it 'trims timelines if they will have more than FeedManager::MAX_ITEMS' do
  159. account = Fabricate(:account)
  160. status = Fabricate(:status)
  161. members = FeedManager::MAX_ITEMS.times.map { |count| [count, count] }
  162. redis.zadd("feed:home:#{account.id}", members)
  163. FeedManager.instance.push_to_home(account, status)
  164. expect(redis.zcard("feed:home:#{account.id}")).to eq FeedManager::MAX_ITEMS
  165. end
  166. context 'reblogs' do
  167. it 'saves reblogs of unseen statuses' do
  168. account = Fabricate(:account)
  169. reblogged = Fabricate(:status)
  170. reblog = Fabricate(:status, reblog: reblogged)
  171. expect(FeedManager.instance.push_to_home(account, reblog)).to be true
  172. end
  173. it 'does not save a new reblog of a recent status' do
  174. account = Fabricate(:account)
  175. reblogged = Fabricate(:status)
  176. reblog = Fabricate(:status, reblog: reblogged)
  177. FeedManager.instance.push_to_home(account, reblogged)
  178. expect(FeedManager.instance.push_to_home(account, reblog)).to be false
  179. end
  180. it 'saves a new reblog of an old status' do
  181. account = Fabricate(:account)
  182. reblogged = Fabricate(:status)
  183. reblog = Fabricate(:status, reblog: reblogged)
  184. FeedManager.instance.push_to_home(account, reblogged)
  185. # Fill the feed with intervening statuses
  186. FeedManager::REBLOG_FALLOFF.times do
  187. FeedManager.instance.push_to_home(account, Fabricate(:status))
  188. end
  189. expect(FeedManager.instance.push_to_home(account, reblog)).to be true
  190. end
  191. it 'does not save a new reblog of a recently-reblogged status' do
  192. account = Fabricate(:account)
  193. reblogged = Fabricate(:status)
  194. reblogs = 2.times.map { Fabricate(:status, reblog: reblogged) }
  195. # The first reblog will be accepted
  196. FeedManager.instance.push_to_home(account, reblogs.first)
  197. # The second reblog should be ignored
  198. expect(FeedManager.instance.push_to_home(account, reblogs.last)).to be false
  199. end
  200. it 'saves a new reblog of a recently-reblogged status when previous reblog has been deleted' do
  201. account = Fabricate(:account)
  202. reblogged = Fabricate(:status)
  203. old_reblog = Fabricate(:status, reblog: reblogged)
  204. # The first reblog should be accepted
  205. expect(FeedManager.instance.push_to_home(account, old_reblog)).to be true
  206. # The first reblog should be successfully removed
  207. expect(FeedManager.instance.unpush_from_home(account, old_reblog)).to be true
  208. reblog = Fabricate(:status, reblog: reblogged)
  209. # The second reblog should be accepted
  210. expect(FeedManager.instance.push_to_home(account, reblog)).to be true
  211. end
  212. it 'does not save a new reblog of a multiply-reblogged-then-unreblogged status' do
  213. account = Fabricate(:account)
  214. reblogged = Fabricate(:status)
  215. reblogs = 3.times.map { Fabricate(:status, reblog: reblogged) }
  216. # Accept the reblogs
  217. FeedManager.instance.push_to_home(account, reblogs[0])
  218. FeedManager.instance.push_to_home(account, reblogs[1])
  219. # Unreblog the first one
  220. FeedManager.instance.unpush_from_home(account, reblogs[0])
  221. # The last reblog should still be ignored
  222. expect(FeedManager.instance.push_to_home(account, reblogs.last)).to be false
  223. end
  224. it 'saves a new reblog of a long-ago-reblogged status' do
  225. account = Fabricate(:account)
  226. reblogged = Fabricate(:status)
  227. reblogs = 2.times.map { Fabricate(:status, reblog: reblogged) }
  228. # The first reblog will be accepted
  229. FeedManager.instance.push_to_home(account, reblogs.first)
  230. # Fill the feed with intervening statuses
  231. FeedManager::REBLOG_FALLOFF.times do
  232. FeedManager.instance.push_to_home(account, Fabricate(:status))
  233. end
  234. # The second reblog should also be accepted
  235. expect(FeedManager.instance.push_to_home(account, reblogs.last)).to be true
  236. end
  237. end
  238. it "does not push when the given status's reblog is already inserted" do
  239. account = Fabricate(:account)
  240. reblog = Fabricate(:status)
  241. status = Fabricate(:status, reblog: reblog)
  242. FeedManager.instance.push_to_home(account, status)
  243. expect(FeedManager.instance.push_to_home(account, reblog)).to eq false
  244. end
  245. end
  246. describe '#push_to_list' do
  247. let(:owner) { Fabricate(:account, username: 'owner') }
  248. let(:alice) { Fabricate(:account, username: 'alice') }
  249. let(:bob) { Fabricate(:account, username: 'bob') }
  250. let(:eve) { Fabricate(:account, username: 'eve') }
  251. let(:list) { Fabricate(:list, account: owner) }
  252. before do
  253. owner.follow!(alice)
  254. owner.follow!(bob)
  255. owner.follow!(eve)
  256. list.accounts << alice
  257. list.accounts << bob
  258. end
  259. it "does not push when the given status's reblog is already inserted" do
  260. reblog = Fabricate(:status)
  261. status = Fabricate(:status, reblog: reblog)
  262. FeedManager.instance.push_to_list(list, status)
  263. expect(FeedManager.instance.push_to_list(list, reblog)).to eq false
  264. end
  265. context 'when replies policy is set to no replies' do
  266. before do
  267. list.replies_policy = :none
  268. end
  269. it 'pushes statuses that are not replies' do
  270. status = Fabricate(:status, text: 'Hello world', account: bob)
  271. expect(FeedManager.instance.push_to_list(list, status)).to eq true
  272. end
  273. it 'pushes statuses that are replies to list owner' do
  274. status = Fabricate(:status, text: 'Hello world', account: owner)
  275. reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
  276. expect(FeedManager.instance.push_to_list(list, reply)).to eq true
  277. end
  278. it 'does not push replies to another member of the list' do
  279. status = Fabricate(:status, text: 'Hello world', account: alice)
  280. reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
  281. expect(FeedManager.instance.push_to_list(list, reply)).to eq false
  282. end
  283. end
  284. context 'when replies policy is set to list-only replies' do
  285. before do
  286. list.replies_policy = :list
  287. end
  288. it 'pushes statuses that are not replies' do
  289. status = Fabricate(:status, text: 'Hello world', account: bob)
  290. expect(FeedManager.instance.push_to_list(list, status)).to eq true
  291. end
  292. it 'pushes statuses that are replies to list owner' do
  293. status = Fabricate(:status, text: 'Hello world', account: owner)
  294. reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
  295. expect(FeedManager.instance.push_to_list(list, reply)).to eq true
  296. end
  297. it 'pushes replies to another member of the list' do
  298. status = Fabricate(:status, text: 'Hello world', account: alice)
  299. reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
  300. expect(FeedManager.instance.push_to_list(list, reply)).to eq true
  301. end
  302. it 'does not push replies to someone not a member of the list' do
  303. status = Fabricate(:status, text: 'Hello world', account: eve)
  304. reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
  305. expect(FeedManager.instance.push_to_list(list, reply)).to eq false
  306. end
  307. end
  308. context 'when replies policy is set to any reply' do
  309. before do
  310. list.replies_policy = :followed
  311. end
  312. it 'pushes statuses that are not replies' do
  313. status = Fabricate(:status, text: 'Hello world', account: bob)
  314. expect(FeedManager.instance.push_to_list(list, status)).to eq true
  315. end
  316. it 'pushes statuses that are replies to list owner' do
  317. status = Fabricate(:status, text: 'Hello world', account: owner)
  318. reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
  319. expect(FeedManager.instance.push_to_list(list, reply)).to eq true
  320. end
  321. it 'pushes replies to another member of the list' do
  322. status = Fabricate(:status, text: 'Hello world', account: alice)
  323. reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
  324. expect(FeedManager.instance.push_to_list(list, reply)).to eq true
  325. end
  326. it 'pushes replies to someone not a member of the list' do
  327. status = Fabricate(:status, text: 'Hello world', account: eve)
  328. reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
  329. expect(FeedManager.instance.push_to_list(list, reply)).to eq true
  330. end
  331. end
  332. end
  333. describe '#merge_into_home' do
  334. it "does not push source account's statuses whose reblogs are already inserted" do
  335. account = Fabricate(:account, id: 0)
  336. reblog = Fabricate(:status)
  337. status = Fabricate(:status, reblog: reblog)
  338. FeedManager.instance.push_to_home(account, status)
  339. FeedManager.instance.merge_into_home(account, reblog.account)
  340. expect(redis.zscore("feed:home:0", reblog.id)).to eq nil
  341. end
  342. end
  343. describe '#unpush_from_home' do
  344. let(:receiver) { Fabricate(:account) }
  345. it 'leaves a reblogged status if original was on feed' do
  346. reblogged = Fabricate(:status)
  347. status = Fabricate(:status, reblog: reblogged)
  348. FeedManager.instance.push_to_home(receiver, reblogged)
  349. FeedManager::REBLOG_FALLOFF.times { FeedManager.instance.push_to_home(receiver, Fabricate(:status)) }
  350. FeedManager.instance.push_to_home(receiver, status)
  351. # The reblogging status should show up under normal conditions.
  352. expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to include(status.id.to_s)
  353. FeedManager.instance.unpush_from_home(receiver, status)
  354. # Restore original status
  355. expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to_not include(status.id.to_s)
  356. expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to include(reblogged.id.to_s)
  357. end
  358. it 'removes a reblogged status if it was only reblogged once' do
  359. reblogged = Fabricate(:status)
  360. status = Fabricate(:status, reblog: reblogged)
  361. FeedManager.instance.push_to_home(receiver, status)
  362. # The reblogging status should show up under normal conditions.
  363. expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to eq [status.id.to_s]
  364. FeedManager.instance.unpush_from_home(receiver, status)
  365. expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to be_empty
  366. end
  367. it 'leaves a multiply-reblogged status if another reblog was in feed' do
  368. reblogged = Fabricate(:status)
  369. reblogs = 3.times.map { Fabricate(:status, reblog: reblogged) }
  370. reblogs.each do |reblog|
  371. FeedManager.instance.push_to_home(receiver, reblog)
  372. end
  373. # The reblogging status should show up under normal conditions.
  374. expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to eq [reblogs.first.id.to_s]
  375. reblogs[0...-1].each do |reblog|
  376. FeedManager.instance.unpush_from_home(receiver, reblog)
  377. end
  378. expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to eq [reblogs.last.id.to_s]
  379. end
  380. it 'sends push updates' do
  381. status = Fabricate(:status)
  382. FeedManager.instance.push_to_home(receiver, status)
  383. allow(redis).to receive_messages(publish: nil)
  384. FeedManager.instance.unpush_from_home(receiver, status)
  385. deletion = Oj.dump(event: :delete, payload: status.id.to_s)
  386. expect(redis).to have_received(:publish).with("timeline:#{receiver.id}", deletion)
  387. end
  388. end
  389. describe '#clear_from_home' do
  390. let(:account) { Fabricate(:account) }
  391. let(:followed_account) { Fabricate(:account) }
  392. let(:target_account) { Fabricate(:account) }
  393. let(:status_1) { Fabricate(:status, account: followed_account) }
  394. let(:status_2) { Fabricate(:status, account: target_account) }
  395. let(:status_3) { Fabricate(:status, account: followed_account, mentions: [Fabricate(:mention, account: target_account)]) }
  396. let(:status_4) { Fabricate(:status, mentions: [Fabricate(:mention, account: target_account)]) }
  397. let(:status_5) { Fabricate(:status, account: followed_account, reblog: status_4) }
  398. let(:status_6) { Fabricate(:status, account: followed_account, reblog: status_2) }
  399. let(:status_7) { Fabricate(:status, account: followed_account) }
  400. before do
  401. [status_1, status_3, status_5, status_6, status_7].each do |status|
  402. redis.zadd("feed:home:#{account.id}", status.id, status.id)
  403. end
  404. end
  405. it 'correctly cleans the home timeline' do
  406. FeedManager.instance.clear_from_home(account, target_account)
  407. expect(redis.zrange("feed:home:#{account.id}", 0, -1)).to eq [status_1.id.to_s, status_7.id.to_s]
  408. end
  409. end
  410. end