| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | ) |
| 6 | |
| 7 | // Sweep applies the retention rules (§5; Paul, 2026-11-12): page history |
| 8 | // is kept 90 days and abandoned drafts 30. It runs when the app wakes, |
| 9 | // because the platform runs no timers (doctrine §13), and between wakes |
| 10 | // every read already ignores what has aged out. |
| 11 | // |
| 12 | // **Retention is exempt from gate 8 by name**, as draft writes are: it |
| 13 | // changes no page, no revision anyone can still reach, and nothing S1 |
| 14 | // reports. It removes: |
| 15 | // - drafts last written more than 30 days ago; |
| 16 | // - revisions older than 90 days, except each live page's current one |
| 17 | // (the page row holds it anyway, and a merge needs a base); |
| 18 | // - everything restorable of a page deleted more than 90 days ago: its |
| 19 | // revisions and its sealed record. The gone row stays, for S1; |
| 20 | // - the images no person can still reach (images_sweep.go). |
| 21 | func (store *Store) Sweep(ctx context.Context) error { |
| 22 | database := store.database.WithContext(ctx) |
| 23 | now := store.now().UTC() |
| 24 | drafts := now.Add(-DraftWindow) |
| 25 | history := now.Add(-HistoryWindow) |
| 26 | if err := database.Where("updated_at < ?", drafts). |
| 27 | Delete(&Draft{}).Error; err != nil { |
| 28 | return err |
| 29 | } |
| 30 | err := database.Exec("DELETE FROM revisions WHERE at < ? AND NOT "+ |
| 31 | "EXISTS (SELECT 1 FROM pages WHERE pages.id = revisions.page_id "+ |
| 32 | "AND pages.gone = ? AND pages.revision = revisions.revision)", |
| 33 | history, false).Error |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | err = database.Model(&Page{}).Where("gone = ? AND updated_at < ? "+ |
| 38 | "AND sealed_record IS NOT NULL", true, history). |
| 39 | Update("sealed_record", nil).Error |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | if err := store.sweepImages(ctx, now); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | return store.sweepIdleToken(ctx, now) |
| 47 | } |
| 48 | |