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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
require 'rails_helper'
RSpec.describe AccountStatusesCleanupPolicy, type: :model do
let(:account) { Fabricate(:account, username: 'alice', domain: nil) }
describe 'validation' do
it 'disallow remote accounts' do
account.update(domain: 'example.com')
account_statuses_cleanup_policy = Fabricate.build(:account_statuses_cleanup_policy, account: account)
account_statuses_cleanup_policy.valid?
expect(account_statuses_cleanup_policy).to model_have_error_on_field(:account)
end
end
describe 'save hooks' do
context 'when widening a policy' do
let!(:account_statuses_cleanup_policy) do
Fabricate(:account_statuses_cleanup_policy,
account: account,
keep_direct: true,
keep_pinned: true,
keep_polls: true,
keep_media: true,
keep_self_fav: true,
keep_self_bookmark: true,
min_favs: 1,
min_reblogs: 1)
end
before do
account_statuses_cleanup_policy.record_last_inspected(42)
end
it 'invalidates last_inspected when widened because of keep_direct' do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
it 'invalidates last_inspected when widened because of keep_pinned' do
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
it 'invalidates last_inspected when widened because of keep_polls' do
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
it 'invalidates last_inspected when widened because of keep_media' do
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
it 'invalidates last_inspected when widened because of keep_self_fav' do
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
it 'invalidates last_inspected when widened because of keep_self_bookmark' do
account_statuses_cleanup_policy.keep_self_bookmark = false
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
it 'invalidates last_inspected when widened because of higher min_favs' do
account_statuses_cleanup_policy.min_favs = 5
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
it 'invalidates last_inspected when widened because of disabled min_favs' do
account_statuses_cleanup_policy.min_favs = nil
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
it 'invalidates last_inspected when widened because of higher min_reblogs' do
account_statuses_cleanup_policy.min_reblogs = 5
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
it 'invalidates last_inspected when widened because of disable min_reblogs' do
account_statuses_cleanup_policy.min_reblogs = nil
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to be_nil
end
end
context 'when narrowing a policy' do
let!(:account_statuses_cleanup_policy) do
Fabricate(:account_statuses_cleanup_policy,
account: account,
keep_direct: false,
keep_pinned: false,
keep_polls: false,
keep_media: false,
keep_self_fav: false,
keep_self_bookmark: false,
min_favs: nil,
min_reblogs: nil)
end
it 'does not unnecessarily invalidate last_inspected' do
account_statuses_cleanup_policy.record_last_inspected(42)
account_statuses_cleanup_policy.keep_direct = true
account_statuses_cleanup_policy.keep_pinned = true
account_statuses_cleanup_policy.keep_polls = true
account_statuses_cleanup_policy.keep_media = true
account_statuses_cleanup_policy.keep_self_fav = true
account_statuses_cleanup_policy.keep_self_bookmark = true
account_statuses_cleanup_policy.min_favs = 5
account_statuses_cleanup_policy.min_reblogs = 5
account_statuses_cleanup_policy.save
expect(account_statuses_cleanup_policy.last_inspected).to eq 42
end
end
end
describe '#record_last_inspected' do
let(:account_statuses_cleanup_policy) { Fabricate(:account_statuses_cleanup_policy, account: account) }
it 'records the given id' do
account_statuses_cleanup_policy.record_last_inspected(42)
expect(account_statuses_cleanup_policy.last_inspected).to eq 42
end
end
describe '#invalidate_last_inspected' do
subject { account_statuses_cleanup_policy.invalidate_last_inspected(status, action) }
let(:account_statuses_cleanup_policy) { Fabricate(:account_statuses_cleanup_policy, account: account) }
let(:status) { Fabricate(:status, id: 10, account: account) }
before do
account_statuses_cleanup_policy.record_last_inspected(42)
end
context 'when the action is :unbookmark' do
let(:action) { :unbookmark }
context 'when the policy is not to keep self-bookmarked toots' do
before do
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'does not change the recorded id' do
subject
expect(account_statuses_cleanup_policy.last_inspected).to eq 42
end
end
context 'when the policy is to keep self-bookmarked toots' do
before do
account_statuses_cleanup_policy.keep_self_bookmark = true
end
it 'records the older id' do
subject
expect(account_statuses_cleanup_policy.last_inspected).to eq 10
end
end
end
context 'when the action is :unfav' do
let(:action) { :unfav }
context 'when the policy is not to keep self-favourited toots' do
before do
account_statuses_cleanup_policy.keep_self_fav = false
end
it 'does not change the recorded id' do
subject
expect(account_statuses_cleanup_policy.last_inspected).to eq 42
end
end
context 'when the policy is to keep self-favourited toots' do
before do
account_statuses_cleanup_policy.keep_self_fav = true
end
it 'records the older id' do
subject
expect(account_statuses_cleanup_policy.last_inspected).to eq 10
end
end
end
context 'when the action is :unpin' do
let(:action) { :unpin }
context 'when the policy is not to keep pinned toots' do
before do
account_statuses_cleanup_policy.keep_pinned = false
end
it 'does not change the recorded id' do
subject
expect(account_statuses_cleanup_policy.last_inspected).to eq 42
end
end
context 'when the policy is to keep pinned toots' do
before do
account_statuses_cleanup_policy.keep_pinned = true
end
it 'records the older id' do
subject
expect(account_statuses_cleanup_policy.last_inspected).to eq 10
end
end
end
context 'when the status is more recent than the recorded inspected id' do
let(:action) { :unfav }
let(:status) { Fabricate(:status, account: account) }
it 'does not change the recorded id' do
subject
expect(account_statuses_cleanup_policy.last_inspected).to eq 42
end
end
end
describe '#compute_cutoff_id' do
subject { account_statuses_cleanup_policy.compute_cutoff_id }
let!(:unrelated_status) { Fabricate(:status, created_at: 3.years.ago) }
let(:account_statuses_cleanup_policy) { Fabricate(:account_statuses_cleanup_policy, account: account) }
context 'when the account has posted multiple toots' do
let!(:very_old_status) { Fabricate(:status, created_at: 3.years.ago, account: account) }
let!(:old_status) { Fabricate(:status, created_at: 3.weeks.ago, account: account) }
let!(:recent_status) { Fabricate(:status, created_at: 2.days.ago, account: account) }
it 'returns the most recent id that is still below policy age' do
expect(subject).to eq old_status.id
end
end
context 'when the account has not posted anything' do
it 'returns nil' do
expect(subject).to be_nil
end
end
end
describe '#statuses_to_delete' do
subject { account_statuses_cleanup_policy.statuses_to_delete }
let!(:unrelated_status) { Fabricate(:status, created_at: 3.years.ago) }
let!(:very_old_status) { Fabricate(:status, created_at: 3.years.ago, account: account) }
let!(:pinned_status) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:direct_message) { Fabricate(:status, created_at: 1.year.ago, account: account, visibility: :direct) }
let!(:self_faved) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:self_bookmarked) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:status_with_poll) { Fabricate(:status, created_at: 1.year.ago, account: account, poll_attributes: { account: account, voters_count: 0, options: %w(a b), expires_in: 2.days }) }
let!(:status_with_media) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:faved4) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:faved5) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:reblogged4) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:reblogged5) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:recent_status) { Fabricate(:status, created_at: 2.days.ago, account: account) }
let!(:media_attachment) { Fabricate(:media_attachment, account: account, status: status_with_media) }
let!(:status_pin) { Fabricate(:status_pin, account: account, status: pinned_status) }
let!(:favourite) { Fabricate(:favourite, account: account, status: self_faved) }
let!(:bookmark) { Fabricate(:bookmark, account: account, status: self_bookmarked) }
let(:account_statuses_cleanup_policy) { Fabricate(:account_statuses_cleanup_policy, account: account) }
before do
4.times { faved4.increment_count!(:favourites_count) }
5.times { faved5.increment_count!(:favourites_count) }
4.times { reblogged4.increment_count!(:reblogs_count) }
5.times { reblogged5.increment_count!(:reblogs_count) }
end
context 'when passed a max_id' do
subject { account_statuses_cleanup_policy.statuses_to_delete(50, old_status.id).pluck(:id) }
let!(:old_status) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:slightly_less_old_status) { Fabricate(:status, created_at: 6.months.ago, account: account) }
it 'returns statuses including max_id' do
expect(subject).to include(old_status.id)
end
it 'returns statuses including older than max_id' do
expect(subject).to include(very_old_status.id)
end
it 'does not return statuses newer than max_id' do
expect(subject).to_not include(slightly_less_old_status.id)
end
end
context 'when passed a min_id' do
subject { account_statuses_cleanup_policy.statuses_to_delete(50, recent_status.id, old_status.id).pluck(:id) }
let!(:old_status) { Fabricate(:status, created_at: 1.year.ago, account: account) }
let!(:slightly_less_old_status) { Fabricate(:status, created_at: 6.months.ago, account: account) }
it 'returns statuses including min_id' do
expect(subject).to include(old_status.id)
end
it 'returns statuses including newer than max_id' do
expect(subject).to include(slightly_less_old_status.id)
end
it 'does not return statuses older than min_id' do
expect(subject).to_not include(very_old_status.id)
end
end
context 'when passed a low limit' do
it 'only returns the limited number of items' do
expect(account_statuses_cleanup_policy.statuses_to_delete(1).count).to eq 1
end
end
context 'when policy is set to keep statuses more recent than 2 years' do
before do
account_statuses_cleanup_policy.min_status_age = 2.years.seconds
end
it 'does not return unrelated old status' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end
it 'returns only oldest status for deletion' do
expect(subject.pluck(:id)).to eq [very_old_status.id]
end
end
context 'when policy is set to keep DMs and reject everything else' do
before do
account_statuses_cleanup_policy.keep_direct = true
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'does not return the old direct message for deletion' do
expect(subject.pluck(:id)).to_not include(direct_message.id)
end
it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved4.id, faved5.id, reblogged4.id, reblogged5.id)
end
end
context 'when policy is set to keep self-bookmarked toots and reject everything else' do
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = true
end
it 'does not return the old self-bookmarked message for deletion' do
expect(subject.pluck(:id)).to_not include(self_bookmarked.id)
end
it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, status_with_poll.id, status_with_media.id, faved4.id, faved5.id, reblogged4.id, reblogged5.id)
end
end
context 'when policy is set to keep self-faved toots and reject everything else' do
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = true
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'does not return the old self-bookmarked message for deletion' do
expect(subject.pluck(:id)).to_not include(self_faved.id)
end
it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved4.id, faved5.id, reblogged4.id, reblogged5.id)
end
end
context 'when policy is set to keep toots with media and reject everything else' do
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = true
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'does not return the old message with media for deletion' do
expect(subject.pluck(:id)).to_not include(status_with_media.id)
end
it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, faved4.id, faved5.id, reblogged4.id, reblogged5.id)
end
end
context 'when policy is set to keep toots with polls and reject everything else' do
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = true
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'does not return the old poll message for deletion' do
expect(subject.pluck(:id)).to_not include(status_with_poll.id)
end
it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_media.id, faved4.id, faved5.id, reblogged4.id, reblogged5.id)
end
end
context 'when policy is set to keep pinned toots and reject everything else' do
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = true
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'does not return the old pinned message for deletion' do
expect(subject.pluck(:id)).to_not include(pinned_status.id)
end
it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved4.id, faved5.id, reblogged4.id, reblogged5.id)
end
end
context 'when policy is to not keep any special messages' do
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'does not return the recent toot' do
expect(subject.pluck(:id)).to_not include(recent_status.id)
end
it 'does not return the unrelated toot' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end
it 'returns every other old status for deletion' do
expect(subject.pluck(:id)).to include(direct_message.id, very_old_status.id, pinned_status.id, self_faved.id, self_bookmarked.id, status_with_poll.id, status_with_media.id, faved4.id, faved5.id, reblogged4.id, reblogged5.id)
end
end
context 'when policy is set to keep every category of toots' do
before do
account_statuses_cleanup_policy.keep_direct = true
account_statuses_cleanup_policy.keep_pinned = true
account_statuses_cleanup_policy.keep_polls = true
account_statuses_cleanup_policy.keep_media = true
account_statuses_cleanup_policy.keep_self_fav = true
account_statuses_cleanup_policy.keep_self_bookmark = true
end
it 'does not return unrelated old status' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end
it 'returns only normal statuses for deletion' do
expect(subject.pluck(:id)).to match_array([very_old_status.id, faved4.id, faved5.id, reblogged4.id, reblogged5.id])
end
end
context 'when policy is to keep statuses with at least 5 boosts' do
before do
account_statuses_cleanup_policy.min_reblogs = 5
end
it 'does not return the recent toot' do
expect(subject.pluck(:id)).to_not include(recent_status.id)
end
it 'does not return the toot reblogged 5 times' do
expect(subject.pluck(:id)).to_not include(reblogged5.id)
end
it 'does not return the unrelated toot' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end
it 'returns old statuses not reblogged as much' do
expect(subject.pluck(:id)).to include(very_old_status.id, faved4.id, faved5.id, reblogged4.id)
end
end
context 'when policy is to keep statuses with at least 5 favs' do
before do
account_statuses_cleanup_policy.min_favs = 5
end
it 'does not return the recent toot' do
expect(subject.pluck(:id)).to_not include(recent_status.id)
end
it 'does not return the toot faved 5 times' do
expect(subject.pluck(:id)).to_not include(faved5.id)
end
it 'does not return the unrelated toot' do
expect(subject.pluck(:id)).to_not include(unrelated_status.id)
end
it 'returns old statuses not faved as much' do
expect(subject.pluck(:id)).to include(very_old_status.id, faved4.id, reblogged4.id, reblogged5.id)
end
end
end
end
|