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