oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "errors"
5 "sort"
6
7 "gorm.io/gorm"
8
9 "amadan.net/oficina/mapa/internal/mapa/seal"
10)
11
12// How an image gets its place (§3.2, §3.3). tx.commit writes every
13// revision; attaching is an explicit argument to it, and only the verbs
14// that carry the person's own text pass it: create, a draft's or a
15// form's save, Resolve, Make team page, draftToScratch and the
16// importer's writes. Revert, Restore and the bulk verbs attach nothing.
17// Every commit stamps unreferenced_at on the page's images its new body
18// no longer names (only where it is unset), and clears it on those it
19// names again.
20
21// attaching is tx.commit's choice: whether to attach, and the revision
22// the person's text started from (a draft's base, a form's revision),
23// whose images are not "introduced" either.
24type attaching struct {
25 on bool
26 base int64
27}
28
29// attachNothing is every verb that does not carry the person's own text.
30var attachNothing = attaching{}
31
32// attachOwn is a commit of the person's own text, started from base (0
33// when it started from the page's current revision, or from nothing).
34func attachOwn(base int64) attaching { return attaching{on: true, base: base} }
35
36// settleImages is tx.commit's image half, after the page row holds the
37// new revision. previous is the page's sealed record before it.
38func (tx *Tx) settleImages(page *Page, previous []byte, record Record,
39 actor int64, attach attaching) error {
40 named := record.images()
41 if err := tx.stampImages(page.ID, named); err != nil {
42 return err
43 }
44 if !attach.on || len(named) == 0 {
45 return nil
46 }
47 held, err := tx.heldImages(page.ID, previous, attach.base)
48 if err != nil {
49 return err
50 }
51 var introduced []string
52 for id := range named {
53 if !held[id] {
54 introduced = append(introduced, id)
55 }
56 }
57 if len(introduced) == 0 {
58 return nil
59 }
60 // The pending window is the sweep's alone (Fable M1): a row that
61 // exists is its owner's to attach.
62 return tx.db.Model(&ImageRow{}).Where("public_id IN ? AND "+
63 "page_id IS NULL AND owner_id = ?", introduced, actor).
64 Updates(map[string]any{"page_id": page.ID,
65 "unreferenced_at": nil}).Error
66}
67
68// stampImages marks the page's images its record no longer names, and
69// unmarks those it names again. A stamp is never moved, so an image
70// taken out of the text reaches its 90 days however often the page is
71// edited after (§3.3).
72func (tx *Tx) stampImages(pageID int64, named map[string]bool) error {
73 ids := sortedKeys(named)
74 stamp := tx.db.Model(&ImageRow{}).
75 Where("page_id = ? AND unreferenced_at IS NULL", pageID)
76 if len(ids) > 0 {
77 stamp = stamp.Where("public_id NOT IN ?", ids)
78 }
79 if err := stamp.UpdateColumn("unreferenced_at", tx.at).Error; err != nil {
80 return err
81 }
82 if len(ids) == 0 {
83 return nil
84 }
85 return tx.db.Model(&ImageRow{}).Where("page_id = ? AND "+
86 "unreferenced_at IS NOT NULL AND public_id IN ?", pageID, ids).
87 UpdateColumn("unreferenced_at", nil).Error
88}
89
90// heldImages is every image the page's previous record and the base
91// revision named: none of them is introduced by this commit.
92func (tx *Tx) heldImages(pageID int64, previous []byte,
93 base int64) (map[string]bool, error) {
94 before, err := tx.store.openRecord(seal.PurposeRecord, previous)
95 if err != nil {
96 return nil, err
97 }
98 held := before.images()
99 if base == 0 {
100 return held, nil
101 }
102 var row Revision
103 err = tx.db.Where("page_id = ? AND revision = ?", pageID, base).
104 First(&row).Error
105 if errors.Is(err, gorm.ErrRecordNotFound) {
106 return held, nil
107 }
108 if err != nil {
109 return nil, err
110 }
111 started, err := tx.store.openRecord(seal.PurposeRecord, row.SealedRecord)
112 for id := range started.images() {
113 held[id] = true
114 }
115 return held, err
116}
117
118func sortedKeys(set map[string]bool) []string {
119 out := make([]string, 0, len(set))
120 for key := range set {
121 out = append(out, key)
122 }
123 sort.Strings(out)
124 return out
125}
126
127// repointImages moves a restored page's images to its new id, in the
128// restore's transaction (§3.2 item 4), then settles their stamps
129// against the restored body.
130func (tx *Tx) repointImages(from int64, to Page, record Record) error {
131 err := tx.db.Model(&ImageRow{}).Where("page_id = ?", from).
132 UpdateColumns(map[string]any{"page_id": to.ID,
133 "moved_from": from}).Error
134 if err != nil {
135 return err
136 }
137 return tx.stampImages(to.ID, record.images())
138}
139