| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | |
| 7 | "gorm.io/gorm" |
| 8 | ) |
| 9 | |
| 10 | // Removing an image now (§3.3): any member, for an image on a team |
| 11 | // page; the owner, for a private or pending one. One write deletes the |
| 12 | // row, queues its object and logs "removed" with the actor; the object |
| 13 | // is deleted straight after, and from the queue if that fails. Every |
| 14 | // reference to the image then draws as not available, History too. |
| 15 | |
| 16 | // removableImage is the row of an image member may remove, or |
| 17 | // ErrNoImage: the same answer as an id that never existed. |
| 18 | func (tx *Tx) removableImage(member int64, public string) (ImageRow, |
| 19 | error) { |
| 20 | var row ImageRow |
| 21 | err := tx.db.Table("images").Select("images.*"). |
| 22 | Joins("LEFT JOIN pages ON pages.id = images.page_id"). |
| 23 | Where("images.public_id = ?", public). |
| 24 | Where(tx.db.Where("images.page_id IS NULL AND "+ |
| 25 | "images.owner_id = ?", member). |
| 26 | Or("pages.gone = ? AND (pages.rung = ? OR "+ |
| 27 | "(pages.rung = ? AND pages.owner_id = ?))", false, RungTeam, |
| 28 | RungPrivate, member)). |
| 29 | Take(&row).Error |
| 30 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 31 | return row, ErrNoImage |
| 32 | } |
| 33 | return row, err |
| 34 | } |
| 35 | |
| 36 | // RemoveImage is "Remove from Mapa" and the confirm page's POST. |
| 37 | func (store *Store) RemoveImage(ctx context.Context, who, |
| 38 | public string) error { |
| 39 | var keys []string |
| 40 | err := store.write(ctx, func(tx *Tx) error { |
| 41 | actor, err := tx.member(who) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | row, err := tx.removableImage(actor, public) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | if err := tx.dropFromLook(row, actor); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | keys, err = tx.removeImages(actor, []ImageRow{row}) |
| 53 | return err |
| 54 | }) |
| 55 | if err == nil { |
| 56 | // Again after the commit: a read between the drop inside the |
| 57 | // write and the commit may have put the bytes back. They were |
| 58 | // never served (the read rule ran first); this is tidiness. |
| 59 | store.small.drop(public) |
| 60 | store.emptyTrash(ctx, keys) |
| 61 | } |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | // removeImages deletes rows, queues their objects and logs one |
| 66 | // "removed" each, by actor, in tx. It answers the queued keys. |
| 67 | func (tx *Tx) removeImages(actor int64, rows []ImageRow) ([]string, |
| 68 | error) { |
| 69 | var keys []string |
| 70 | for _, row := range rows { |
| 71 | if err := tx.db.Delete(&ImageRow{}, row.ID).Error; err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | tx.store.small.drop(row.PublicID) |
| 75 | if err := tx.queue(row.ObjectKey); err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | if err := tx.log(actor, ObjectImage, row.ID, VerbRemoved, |
| 79 | 0); err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | keys = append(keys, row.ObjectKey) |
| 83 | } |
| 84 | return keys, nil |
| 85 | } |
| 86 | |
| 87 | // queue puts an object key in image_trash. A key is the hash of one |
| 88 | // sealing, so it is never queued twice; ignore it if it is. |
| 89 | func queueKey(database *gorm.DB, key string, at any) error { |
| 90 | return database.Exec("INSERT OR IGNORE INTO image_trash "+ |
| 91 | "(object_key, queued_at) VALUES (?, ?)", key, at).Error |
| 92 | } |
| 93 | |
| 94 | func (tx *Tx) queue(key string) error { return queueKey(tx.db, key, tx.at) } |
| 95 | |
| 96 | // emptyTrash deletes queued objects from the bucket, and each queue |
| 97 | // row whose delete succeeded. Housekeeping of the queue is retention's, |
| 98 | // exempt from gate 8 by name (§3.3): it removes what no person can |
| 99 | // reach. A failure leaves the key queued for the next wake. |
| 100 | func (store *Store) emptyTrash(ctx context.Context, keys []string) { |
| 101 | objects, err := store.objects() |
| 102 | if err != nil || len(keys) == 0 { |
| 103 | return |
| 104 | } |
| 105 | ctx = context.WithoutCancel(ctx) |
| 106 | for _, key := range keys { |
| 107 | if err := objects.Delete(ctx, key); err != nil { |
| 108 | store.logger().Warn("mapa: an image object stays queued", |
| 109 | "err", err) |
| 110 | continue |
| 111 | } |
| 112 | err := store.database.WithContext(ctx). |
| 113 | Where("object_key = ?", key).Delete(&ImageTrash{}).Error |
| 114 | if err != nil { |
| 115 | store.logger().Warn("mapa: emptying image_trash", "err", err) |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // EmptyImageTrash empties the whole queue: run off the boot path after |
| 121 | // the sweep (§3.3, step 4). It never returns an error to boot. |
| 122 | func (store *Store) EmptyImageTrash(ctx context.Context) { |
| 123 | var keys []string |
| 124 | err := store.database.WithContext(ctx).Model(&ImageTrash{}). |
| 125 | Order("queued_at").Pluck("object_key", &keys).Error |
| 126 | if err != nil { |
| 127 | store.logger().Warn("mapa: reading image_trash", "err", err) |
| 128 | return |
| 129 | } |
| 130 | store.emptyTrash(ctx, keys) |
| 131 | } |
| 132 | |
| 133 | // RemovableImage is the image viewer may remove, with its size, for the |
| 134 | // confirm page; ErrNoImage otherwise. It writes nothing. |
| 135 | func (store *Store) RemovableImage(ctx context.Context, viewer Viewer, |
| 136 | public string) (Image, error) { |
| 137 | var row ImageRow |
| 138 | err := store.database.WithContext(ctx).Transaction( |
| 139 | func(database *gorm.DB) error { |
| 140 | tx := &Tx{db: database, store: store, at: store.now().UTC()} |
| 141 | var err error |
| 142 | row, err = tx.removableImage(viewer.MemberID, public) |
| 143 | return err |
| 144 | }) |
| 145 | if err != nil { |
| 146 | return Image{}, err |
| 147 | } |
| 148 | record, err := store.openImageRecord(row) |
| 149 | return Image{PublicID: row.PublicID, Width: record.Width, |
| 150 | Height: record.Height}, err |
| 151 | } |
| 152 | |