| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| 7 | "gorm.io/gorm" |
| 8 | |
| 9 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 10 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 11 | ) |
| 12 | |
| 13 | // The images' part of the sweep (§3.3), run on wake with the rest of |
| 14 | // retention and exempt from gate 8 by name, for the same reasons: it |
| 15 | // removes only what no person can still reach, and an event would have |
| 16 | // to name Mapa as its actor. It chooses candidates with SQL alone: |
| 17 | // - attached images taken out of the text more than 90 days ago; |
| 18 | // - the images of pages deleted more than 90 days ago; |
| 19 | // - pending images more than seven days old. |
| 20 | // |
| 21 | // Then it unseals only the drafts that could keep one (drafts on the |
| 22 | // candidates' pages, and drafts of the pending candidates' owners), and |
| 23 | // keeps every candidate a draft names. The common wake unseals nothing. |
| 24 | // The rows go, their keys to image_trash in the same transaction; the |
| 25 | // bucket is emptied off the boot path (EmptyImageTrash). |
| 26 | |
| 27 | func (store *Store) sweepImages(ctx context.Context, now time.Time) error { |
| 28 | history, pending := now.Add(-HistoryWindow), now.Add(-PendingWindow) |
| 29 | var candidates []ImageRow |
| 30 | err := store.database.WithContext(ctx).Table("images"). |
| 31 | Select("images.*"). |
| 32 | Joins("LEFT JOIN pages ON pages.id = images.page_id"). |
| 33 | Where("(images.page_id IS NOT NULL AND images.unreferenced_at < ?)"+ |
| 34 | " OR (pages.gone = ? AND pages.updated_at < ?)"+ |
| 35 | " OR (images.page_id IS NULL AND images.created_at < ?)", |
| 36 | history, true, history, pending). |
| 37 | Find(&candidates).Error |
| 38 | if err != nil || len(candidates) == 0 { |
| 39 | return err |
| 40 | } |
| 41 | kept, err := store.draftedImages(ctx, candidates) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | var doomed []ImageRow |
| 46 | for _, row := range candidates { |
| 47 | if !kept[row.PublicID] { |
| 48 | doomed = append(doomed, row) |
| 49 | } |
| 50 | } |
| 51 | return store.database.WithContext(ctx).Transaction( |
| 52 | func(database *gorm.DB) error { |
| 53 | for _, row := range doomed { |
| 54 | store.small.drop(row.PublicID) |
| 55 | err := database.Delete(&ImageRow{}, row.ID).Error |
| 56 | if err == nil { |
| 57 | err = queueKey(database, row.ObjectKey, now) |
| 58 | } |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | } |
| 63 | return nil |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | // draftedImages is every image id named by a draft that could keep one |
| 68 | // of the candidates. |
| 69 | func (store *Store) draftedImages(ctx context.Context, |
| 70 | candidates []ImageRow) (map[string]bool, error) { |
| 71 | pages, owners := map[int64]bool{}, map[int64]bool{} |
| 72 | for _, row := range candidates { |
| 73 | if row.PageID != nil { |
| 74 | pages[*row.PageID] = true |
| 75 | } else { |
| 76 | owners[row.OwnerID] = true |
| 77 | } |
| 78 | } |
| 79 | var drafts []Draft |
| 80 | err := store.database.WithContext(ctx). |
| 81 | Where("page_id IN ? OR person_id IN ?", keysOf(pages), |
| 82 | keysOf(owners)).Find(&drafts).Error |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | kept := map[string]bool{} |
| 87 | for _, draft := range drafts { |
| 88 | store.sweepOpened.Add(1) |
| 89 | record, err := store.openRecord(seal.PurposeDraft, |
| 90 | draft.SealedRecord) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | for id := range markup.ImageIDs(record.Body) { |
| 95 | kept[id] = true |
| 96 | } |
| 97 | } |
| 98 | return kept, nil |
| 99 | } |
| 100 | |
| 101 | func keysOf(set map[int64]bool) []int64 { |
| 102 | out := []int64{-1} // never empty: IN () is not SQL everywhere |
| 103 | for key := range set { |
| 104 | out = append(out, key) |
| 105 | } |
| 106 | return out |
| 107 | } |
| 108 | |