The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

337 lines
13 KiB

  1. require 'rails_helper'
  2. RSpec.describe FeedManager do
  3. it 'tracks at least as many statuses as reblogs' do
  4. expect(FeedManager::REBLOG_FALLOFF).to be <= FeedManager::MAX_ITEMS
  5. end
  6. describe '#key' do
  7. subject { FeedManager.instance.key(:home, 1) }
  8. it 'returns a string' do
  9. expect(subject).to be_a String
  10. end
  11. end
  12. describe '#filter?' do
  13. let(:alice) { Fabricate(:account, username: 'alice') }
  14. let(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
  15. let(:jeff) { Fabricate(:account, username: 'jeff') }
  16. context 'for home feed' do
  17. it 'returns false for followee\'s status' do
  18. status = Fabricate(:status, text: 'Hello world', account: alice)
  19. bob.follow!(alice)
  20. expect(FeedManager.instance.filter?(:home, status, bob.id)).to be false
  21. end
  22. it 'returns false for reblog by followee' do
  23. status = Fabricate(:status, text: 'Hello world', account: jeff)
  24. reblog = Fabricate(:status, reblog: status, account: alice)
  25. bob.follow!(alice)
  26. expect(FeedManager.instance.filter?(:home, reblog, bob.id)).to be false
  27. end
  28. it 'returns true for reblog by followee of blocked account' do
  29. status = Fabricate(:status, text: 'Hello world', account: jeff)
  30. reblog = Fabricate(:status, reblog: status, account: alice)
  31. bob.follow!(alice)
  32. bob.block!(jeff)
  33. expect(FeedManager.instance.filter?(:home, reblog, bob.id)).to be true
  34. end
  35. it 'returns true for reblog by followee of muted account' do
  36. status = Fabricate(:status, text: 'Hello world', account: jeff)
  37. reblog = Fabricate(:status, reblog: status, account: alice)
  38. bob.follow!(alice)
  39. bob.mute!(jeff)
  40. expect(FeedManager.instance.filter?(:home, reblog, bob.id)).to be true
  41. end
  42. it 'returns true for reblog by followee of someone who is blocking recipient' do
  43. status = Fabricate(:status, text: 'Hello world', account: jeff)
  44. reblog = Fabricate(:status, reblog: status, account: alice)
  45. bob.follow!(alice)
  46. jeff.block!(bob)
  47. expect(FeedManager.instance.filter?(:home, reblog, bob.id)).to be true
  48. end
  49. it 'returns false for reply by followee to another followee' do
  50. status = Fabricate(:status, text: 'Hello world', account: jeff)
  51. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  52. bob.follow!(alice)
  53. bob.follow!(jeff)
  54. expect(FeedManager.instance.filter?(:home, reply, bob.id)).to be false
  55. end
  56. it 'returns false for reply by followee to recipient' do
  57. status = Fabricate(:status, text: 'Hello world', account: bob)
  58. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  59. bob.follow!(alice)
  60. expect(FeedManager.instance.filter?(:home, reply, bob.id)).to be false
  61. end
  62. it 'returns false for reply by followee to self' do
  63. status = Fabricate(:status, text: 'Hello world', account: alice)
  64. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  65. bob.follow!(alice)
  66. expect(FeedManager.instance.filter?(:home, reply, bob.id)).to be false
  67. end
  68. it 'returns true for reply by followee to non-followed account' do
  69. status = Fabricate(:status, text: 'Hello world', account: jeff)
  70. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  71. bob.follow!(alice)
  72. expect(FeedManager.instance.filter?(:home, reply, bob.id)).to be true
  73. end
  74. it 'returns true for the second reply by followee to a non-federated status' do
  75. reply = Fabricate(:status, text: 'Reply 1', reply: true, account: alice)
  76. second_reply = Fabricate(:status, text: 'Reply 2', thread: reply, account: alice)
  77. bob.follow!(alice)
  78. expect(FeedManager.instance.filter?(:home, second_reply, bob.id)).to be true
  79. end
  80. it 'returns false for status by followee mentioning another account' do
  81. bob.follow!(alice)
  82. status = PostStatusService.new.call(alice, 'Hey @jeff')
  83. expect(FeedManager.instance.filter?(:home, status, bob.id)).to be false
  84. end
  85. it 'returns true for status by followee mentioning blocked account' do
  86. bob.block!(jeff)
  87. bob.follow!(alice)
  88. status = PostStatusService.new.call(alice, 'Hey @jeff')
  89. expect(FeedManager.instance.filter?(:home, status, bob.id)).to be true
  90. end
  91. it 'returns true for reblog of a personally blocked domain' do
  92. alice.block_domain!('example.com')
  93. alice.follow!(jeff)
  94. status = Fabricate(:status, text: 'Hello world', account: bob)
  95. reblog = Fabricate(:status, reblog: status, account: jeff)
  96. expect(FeedManager.instance.filter?(:home, reblog, alice.id)).to be true
  97. end
  98. end
  99. context 'for mentions feed' do
  100. it 'returns true for status that mentions blocked account' do
  101. bob.block!(jeff)
  102. status = PostStatusService.new.call(alice, 'Hey @jeff')
  103. expect(FeedManager.instance.filter?(:mentions, status, bob.id)).to be true
  104. end
  105. it 'returns true for status that replies to a blocked account' do
  106. status = Fabricate(:status, text: 'Hello world', account: jeff)
  107. reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
  108. bob.block!(jeff)
  109. expect(FeedManager.instance.filter?(:mentions, reply, bob.id)).to be true
  110. end
  111. it 'returns true for status by silenced account who recipient is not following' do
  112. status = Fabricate(:status, text: 'Hello world', account: alice)
  113. alice.update(silenced: true)
  114. expect(FeedManager.instance.filter?(:mentions, status, bob.id)).to be true
  115. end
  116. it 'returns false for status by followed silenced account' do
  117. status = Fabricate(:status, text: 'Hello world', account: alice)
  118. alice.update(silenced: true)
  119. bob.follow!(alice)
  120. expect(FeedManager.instance.filter?(:mentions, status, bob.id)).to be false
  121. end
  122. end
  123. end
  124. describe '#push' do
  125. it 'trims timelines if they will have more than FeedManager::MAX_ITEMS' do
  126. account = Fabricate(:account)
  127. status = Fabricate(:status)
  128. members = FeedManager::MAX_ITEMS.times.map { |count| [count, count] }
  129. Redis.current.zadd("feed:home:#{account.id}", members)
  130. FeedManager.instance.push_to_home(account, status)
  131. expect(Redis.current.zcard("feed:home:#{account.id}")).to eq FeedManager::MAX_ITEMS
  132. end
  133. context 'reblogs' do
  134. it 'saves reblogs of unseen statuses' do
  135. account = Fabricate(:account)
  136. reblogged = Fabricate(:status)
  137. reblog = Fabricate(:status, reblog: reblogged)
  138. expect(FeedManager.instance.push_to_home(account, reblog)).to be true
  139. end
  140. it 'does not save a new reblog of a recent status' do
  141. account = Fabricate(:account)
  142. reblogged = Fabricate(:status)
  143. reblog = Fabricate(:status, reblog: reblogged)
  144. FeedManager.instance.push_to_home(account, reblogged)
  145. expect(FeedManager.instance.push_to_home(account, reblog)).to be false
  146. end
  147. it 'saves a new reblog of an old status' do
  148. account = Fabricate(:account)
  149. reblogged = Fabricate(:status)
  150. reblog = Fabricate(:status, reblog: reblogged)
  151. FeedManager.instance.push_to_home(account, reblogged)
  152. # Fill the feed with intervening statuses
  153. FeedManager::REBLOG_FALLOFF.times do
  154. FeedManager.instance.push_to_home(account, Fabricate(:status))
  155. end
  156. expect(FeedManager.instance.push_to_home(account, reblog)).to be true
  157. end
  158. it 'does not save a new reblog of a recently-reblogged status' do
  159. account = Fabricate(:account)
  160. reblogged = Fabricate(:status)
  161. reblogs = 2.times.map { Fabricate(:status, reblog: reblogged) }
  162. # The first reblog will be accepted
  163. FeedManager.instance.push_to_home(account, reblogs.first)
  164. # The second reblog should be ignored
  165. expect(FeedManager.instance.push_to_home(account, reblogs.last)).to be false
  166. end
  167. it 'does not save a new reblog of a multiply-reblogged-then-unreblogged status' do
  168. account = Fabricate(:account)
  169. reblogged = Fabricate(:status)
  170. reblogs = 3.times.map { Fabricate(:status, reblog: reblogged) }
  171. # Accept the reblogs
  172. FeedManager.instance.push_to_home(account, reblogs[0])
  173. FeedManager.instance.push_to_home(account, reblogs[1])
  174. # Unreblog the first one
  175. FeedManager.instance.unpush_from_home(account, reblogs[0])
  176. # The last reblog should still be ignored
  177. expect(FeedManager.instance.push_to_home(account, reblogs.last)).to be false
  178. end
  179. it 'saves a new reblog of a long-ago-reblogged status' do
  180. account = Fabricate(:account)
  181. reblogged = Fabricate(:status)
  182. reblogs = 2.times.map { Fabricate(:status, reblog: reblogged) }
  183. # The first reblog will be accepted
  184. FeedManager.instance.push_to_home(account, reblogs.first)
  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. # The second reblog should also be accepted
  190. expect(FeedManager.instance.push_to_home(account, reblogs.last)).to be true
  191. end
  192. end
  193. end
  194. describe '#trim' do
  195. let(:receiver) { Fabricate(:account) }
  196. it 'cleans up reblog tracking keys' do
  197. reblogged = Fabricate(:status)
  198. status = Fabricate(:status, reblog: reblogged)
  199. another_status = Fabricate(:status, reblog: reblogged)
  200. reblogs_key = FeedManager.instance.key('home', receiver.id, 'reblogs')
  201. reblog_set_key = FeedManager.instance.key('home', receiver.id, "reblogs:#{reblogged.id}")
  202. FeedManager.instance.push_to_home(receiver, status)
  203. FeedManager.instance.push_to_home(receiver, another_status)
  204. # We should have a tracking set and an entry in reblogs.
  205. expect(Redis.current.exists(reblog_set_key)).to be true
  206. expect(Redis.current.zrange(reblogs_key, 0, -1)).to eq [reblogged.id.to_s]
  207. # Push everything off the end of the feed.
  208. FeedManager::MAX_ITEMS.times do
  209. FeedManager.instance.push_to_home(receiver, Fabricate(:status))
  210. end
  211. # `trim` should be called automatically, but do it anyway, as
  212. # we're testing `trim`, not side effects of `push`.
  213. FeedManager.instance.trim('home', receiver.id)
  214. # We should not have any reblog tracking data.
  215. expect(Redis.current.exists(reblog_set_key)).to be false
  216. expect(Redis.current.zrange(reblogs_key, 0, -1)).to be_empty
  217. end
  218. end
  219. describe '#unpush' do
  220. let(:receiver) { Fabricate(:account) }
  221. it 'leaves a reblogged status if original was on feed' do
  222. reblogged = Fabricate(:status)
  223. status = Fabricate(:status, reblog: reblogged)
  224. FeedManager.instance.push_to_home(receiver, reblogged)
  225. FeedManager::REBLOG_FALLOFF.times { FeedManager.instance.push_to_home(receiver, Fabricate(:status)) }
  226. FeedManager.instance.push_to_home(receiver, status)
  227. # The reblogging status should show up under normal conditions.
  228. expect(Redis.current.zrange("feed:home:#{receiver.id}", 0, -1)).to include(status.id.to_s)
  229. FeedManager.instance.unpush_from_home(receiver, status)
  230. # Restore original status
  231. expect(Redis.current.zrange("feed:home:#{receiver.id}", 0, -1)).to_not include(status.id.to_s)
  232. expect(Redis.current.zrange("feed:home:#{receiver.id}", 0, -1)).to include(reblogged.id.to_s)
  233. end
  234. it 'removes a reblogged status if it was only reblogged once' do
  235. reblogged = Fabricate(:status)
  236. status = Fabricate(:status, reblog: reblogged)
  237. FeedManager.instance.push_to_home(receiver, status)
  238. # The reblogging status should show up under normal conditions.
  239. expect(Redis.current.zrange("feed:home:#{receiver.id}", 0, -1)).to eq [status.id.to_s]
  240. FeedManager.instance.unpush_from_home(receiver, status)
  241. expect(Redis.current.zrange("feed:home:#{receiver.id}", 0, -1)).to be_empty
  242. end
  243. it 'leaves a multiply-reblogged status if another reblog was in feed' do
  244. reblogged = Fabricate(:status)
  245. reblogs = 3.times.map { Fabricate(:status, reblog: reblogged) }
  246. reblogs.each do |reblog|
  247. FeedManager.instance.push_to_home(receiver, reblog)
  248. end
  249. # The reblogging status should show up under normal conditions.
  250. expect(Redis.current.zrange("feed:home:#{receiver.id}", 0, -1)).to eq [reblogs.first.id.to_s]
  251. reblogs[0...-1].each do |reblog|
  252. FeedManager.instance.unpush_from_home(receiver, reblog)
  253. end
  254. expect(Redis.current.zrange("feed:home:#{receiver.id}", 0, -1)).to eq [reblogs.last.id.to_s]
  255. end
  256. it 'sends push updates' do
  257. status = Fabricate(:status)
  258. FeedManager.instance.push_to_home(receiver, status)
  259. allow(Redis.current).to receive_messages(publish: nil)
  260. FeedManager.instance.unpush_from_home(receiver, status)
  261. deletion = Oj.dump(event: :delete, payload: status.id.to_s)
  262. expect(Redis.current).to have_received(:publish).with("timeline:#{receiver.id}", deletion)
  263. end
  264. end
  265. end