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 "context"
5 "errors"
6
7 "amadan.net/oficina/mapa/internal/mapa/markup"
8)
9
10// Copies a verb makes before its write (§2.4, §3.2 items 3 and 5).
11// Make team page copies the images attached to its source that the
12// moved blocks name; a deleted page's draft, saved as a scratch page,
13// copies the images attached to that deleted page. Each copy is a new
14// pending image owned by the person, made before the transaction, and
15// attached by the write's own commit. When the write fails, the verb
16// removes its copies at once (dropCopies), so nothing holds quota for
17// seven days and no object is orphaned.
18
19// copied is the copies one verb made: each old public id's new one.
20type copied struct {
21 ids map[string]string
22}
23
24// rewrite is body with each copied image pointed at its copy.
25func (made copied) rewrite(body string) string {
26 if len(made.ids) == 0 {
27 return body
28 }
29 return markup.ReplaceImageIDs(body, made.ids)
30}
31
32// copyAttached copies each image body names that is attached to
33// pageID, or (moved, for a deleted page's draft) was moved from it by a
34// Restore. The caller has ruled that the person may read that page.
35func (store *Store) copyAttached(ctx context.Context, who person,
36 pageID int64, body string, moved bool) (copied, error) {
37 made := copied{ids: map[string]string{}}
38 named := sortedKeys(markup.ImageIDs(body))
39 if len(named) == 0 {
40 return made, nil
41 }
42 var rows []ImageRow
43 query := store.database.WithContext(ctx)
44 if moved {
45 query = query.Where("(page_id = ? OR moved_from = ?)", pageID,
46 pageID)
47 } else {
48 query = query.Where("page_id = ?", pageID)
49 }
50 err := query.Where("public_id IN ?", named).Order("id").
51 Find(&rows).Error
52 if err != nil {
53 return made, err
54 }
55 for _, row := range rows {
56 image, err := store.copyRow(ctx, who, row, uploadWait)
57 if err != nil {
58 store.dropCopies(ctx, who, made)
59 return copied{}, err
60 }
61 made.ids[row.PublicID] = image.PublicID
62 }
63 return made, nil
64}
65
66// teamCopies is Make team page's copies: the images attached to the
67// person's own private note that the chosen blocks of revision name.
68// A note that is not theirs, or not at that revision, copies nothing:
69// the write refuses it.
70func (store *Store) teamCopies(ctx context.Context, who string, sourceID,
71 revision int64, chosen []int, title string) (copied, error) {
72 member, err := store.known(ctx, byAddress(who))
73 if err != nil || member == 0 {
74 return copied{}, err
75 }
76 var source Page
77 err = store.database.WithContext(ctx).Where("id = ? AND gone = ? "+
78 "AND rung = ? AND owner_id = ? AND revision = ?", sourceID, false,
79 RungPrivate, member, revision).Limit(1).Find(&source).Error
80 if err != nil || source.ID == 0 {
81 return copied{}, err
82 }
83 record, err := store.OpenPage(source)
84 if err != nil {
85 return copied{}, err
86 }
87 _, moved, err := splitBlocks(record.Body, chosen, title)
88 if err != nil {
89 return copied{}, nil
90 }
91 return store.copyAttached(ctx, byAddress(who), source.ID, moved, false)
92}
93
94// scratchCopies is draftToScratch's copies, the one named exception to
95// the read rule (§3.2 item 5): the images attached to the draft's own
96// deleted page, when the person passes that page's rule (a team page,
97// or a private page they own). An image attached anywhere else is left
98// as typed.
99func (store *Store) scratchCopies(ctx context.Context, viewer Viewer,
100 pageID int64, body string) (copied, error) {
101 var page Page
102 err := store.database.WithContext(ctx).Where("id = ? AND "+
103 "(rung = ? OR owner_id = ?)", pageID, RungTeam, viewer.MemberID).
104 Limit(1).Find(&page).Error
105 if err != nil || page.ID == 0 || viewer.MemberID == 0 {
106 return copied{}, err
107 }
108 return store.copyAttached(ctx, byAddress(viewer.Address), page.ID,
109 body, true)
110}
111
112// dropCopies removes a failed verb's copies, as a removal by the person
113// (§3.2 item 3): one transaction deletes the rows, queues their keys
114// and logs "removed"; the objects go after it.
115func (store *Store) dropCopies(ctx context.Context, who person,
116 made copied) {
117 if len(made.ids) == 0 {
118 return
119 }
120 var fresh []string
121 for _, id := range made.ids {
122 fresh = append(fresh, id)
123 }
124 var keys []string
125 err := store.write(context.WithoutCancel(ctx), func(tx *Tx) error {
126 actor, err := who.in(tx)
127 if err != nil {
128 return err
129 }
130 var rows []ImageRow
131 err = tx.db.Where("public_id IN ? AND owner_id = ? AND "+
132 "page_id IS NULL", fresh, actor).Find(&rows).Error
133 if err != nil {
134 return err
135 }
136 keys, err = tx.removeImages(actor, rows)
137 return err
138 })
139 if err != nil && !errors.Is(err, ErrUnlogged) {
140 store.logger().Error("mapa: removing a failed write's copies",
141 "err", err)
142 }
143 store.emptyTrash(ctx, keys)
144}
145