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
6 "amadan.net/oficina/mapa/internal/mapa/notionmd"
7 "amadan.net/oficina/notion.git/notion"
8)
9
10// A Notion page's look (columns and icons spec §6.1): its icon and its
11// cover, prepared with its images, before the page's transaction, under
12// the same rules (import spec §12): a fresh URL from the page object,
13// the lease checked just before each add, a failure the page's own
14// count and never its failure. notion_images keeps where each went, in
15// a print domain of its own (lookPrints), so the file's key decides
16// whether it changed, as for an image block.
17
18// The look's pseudo-blocks in notion_images.
19const (
20 iconPlace = "icon:"
21 coverPlace = "cover:"
22)
23
24// The look's outcomes, in words for the report. lookImported counts;
25// the rest are why not.
26const (
27 lookImported = "imported"
28 lookEmoji = "not in Mapa's emoji list"
29 lookIconSet = "from Notion's icon set or another website"
30 lookElsewhere = "from another website"
31 lookIconLarge = "too large for an icon (over 128 KB or 512 pixels " +
32 "on a side)"
33)
34
35// pageLook is a page's prepared icon and banner, the images it added,
36// where each went, and the page object read again for fresh URLs, once.
37type pageLook struct {
38 icon *Icon
39 banner *Banner
40 added []AddedImage
41 placed []placedImage
42 fresh *notion.Page
43}
44
45// lookOutcome is one icon's or cover's: the image's public id, or why
46// not, and whether that is for good.
47type lookOutcome struct {
48 public, reason string
49 permanent bool
50}
51
52// prepareLook prepares a page's icon and cover, counting each.
53func (job *importJob) prepareLook(ctx context.Context, entry *importEntry,
54 counts *imageCounts) (*pageLook, error) {
55 look := &pageLook{}
56 if err := job.prepareIcon(ctx, entry, look, counts); err != nil {
57 return nil, err
58 }
59 return look, job.prepareCover(ctx, entry, look, counts)
60}
61
62func (job *importJob) prepareIcon(ctx context.Context, entry *importEntry,
63 look *pageLook, counts *imageCounts) error {
64 icon := entry.staged.Icon
65 switch {
66 case icon == nil:
67 return nil
68 case icon.Type == notion.IconEmoji:
69 made, err := EmojiIcon(icon.Emoji)
70 if err != nil {
71 counts.Icon = lookEmoji
72 return nil
73 }
74 look.icon, counts.Icon = made, lookImported
75 return nil
76 case !iconFetched(icon.Type):
77 // Notion's own icon set is SVG, which Mapa refuses, and any
78 // other address is never fetched (import D13).
79 counts.Icon = lookIconSet
80 return nil
81 }
82 got, err := job.lookImage(ctx, entry, iconPlace, icon, look)
83 if err != nil {
84 return err
85 }
86 switch {
87 case got.reason == "":
88 look.icon, counts.Icon = &Icon{Image: got.public}, lookImported
89 case !got.permanent:
90 // Tried again next run: the page keeps the icon it has, so a
91 // passing failure is not two revisions (Fable on phase 4, L7).
92 look.icon, counts.Icon = entry.current.Icon, got.reason
93 default:
94 counts.Icon = got.reason
95 }
96 return nil
97}
98
99// iconFetched is an icon type whose picture is downloaded.
100func iconFetched(kind string) bool {
101 return kind == notion.IconCustomEmoji || kind == notion.FileHosted
102}
103
104// coverFetched is a cover downloaded: Notion-hosted, or from Notion's
105// own gallery, the one external address the module pins for it (Q5).
106func (job *importJob) coverFetched(cover *notionmd.Look) bool {
107 return cover.Type == notion.FileHosted ||
108 (cover.Type == notion.FileExternal && job.files.Gallery(cover.URL))
109}
110
111func (job *importJob) prepareCover(ctx context.Context, entry *importEntry,
112 look *pageLook, counts *imageCounts) error {
113 cover := entry.staged.Cover
114 if cover == nil {
115 return nil
116 }
117 if !job.coverFetched(cover) {
118 counts.Cover = lookElsewhere
119 return nil
120 }
121 got, err := job.lookImage(ctx, entry, coverPlace, cover, look)
122 if err != nil {
123 return err
124 }
125 switch {
126 case got.reason == "":
127 // The API gives no position: centred.
128 look.banner = &Banner{Image: got.public, Y: 50}
129 counts.Cover = lookImported
130 case !got.permanent:
131 look.banner, counts.Cover = entry.current.Banner, got.reason
132 default:
133 counts.Cover = got.reason
134 }
135 return nil
136}
137
138// lookImage is an icon's or a cover's image: its last place reused when
139// Notion still shows the same file and the page still holds the image,
140// a permanent refusal remembered, or a fresh download.
141func (job *importJob) lookImage(ctx context.Context, entry *importEntry,
142 pseudo string, given *notionmd.Look, look *pageLook) (lookOutcome,
143 error) {
144 placed := placedImage{page: entry.staged.ID, block: pseudo, look: true,
145 key: notion.FileKey(given.URL)}
146 prior, found, err := job.priorLook(ctx, entry.staged.ID, pseudo)
147 if err != nil {
148 return lookOutcome{}, err
149 }
150 if found && sameFile(prior.Key, placed.key) {
151 if prior.Reason != "" {
152 placed.reason, placed.permanent = prior.Reason, true
153 look.placed = append(look.placed, placed)
154 return lookOutcome{reason: prior.Reason, permanent: true}, nil
155 }
156 if entry.place.PageID != 0 &&
157 job.images.Held(ctx, prior.PublicID, entry.place.PageID) {
158 placed.publicID = prior.PublicID
159 look.placed = append(look.placed, placed)
160 return lookOutcome{public: prior.PublicID}, nil
161 }
162 }
163 added, key, why, err := job.addLook(ctx, entry, pseudo, look)
164 if err != nil {
165 return lookOutcome{}, err
166 }
167 if key != "" {
168 placed.key = key
169 }
170 if why.text != "" {
171 look.placed = append(look.placed, refused(placed, why, prior))
172 return lookOutcome{reason: why.text, permanent: why.permanent}, nil
173 }
174 placed.publicID = added.PublicID()
175 look.added = append(look.added, added)
176 look.placed = append(look.placed, placed)
177 return lookOutcome{public: placed.publicID}, nil
178}
179
180// refused is a refused look's place. A passing failure leaves the page
181// its look, so the row stays as it was: a Notion that goes back to that
182// file reuses it (Fable's re-check of phase 4, low 2).
183func refused(placed placedImage, why refusal, prior imagePlace) placedImage {
184 if !why.permanent && prior.PublicID != "" {
185 return placedImage{page: placed.page, block: placed.block,
186 look: true, key: prior.Key, publicID: prior.PublicID}
187 }
188 placed.reason, placed.permanent = why.text, why.permanent
189 return placed
190}
191
192// addLook downloads an icon or a cover and adds it, and answers the key
193// of the file it downloaded. An icon over 512 pixels a side is taken
194// away again at once: Mapa never decodes pixels, so it cannot scale one
195// down (§6.1).
196func (job *importJob) addLook(ctx context.Context, entry *importEntry,
197 pseudo string, look *pageLook) (AddedImage, string, refusal, error) {
198 if job.imagesStopped == "" {
199 if err := job.images.Ready(ctx); err != nil {
200 job.imagesStopped = errNoImageStore.Error()
201 }
202 }
203 if job.imagesStopped != "" {
204 return nil, "", refusal{text: job.imagesStopped}, nil
205 }
206 if err := job.store.bookkeep(ctx, job.lease, noChange); err != nil {
207 return nil, "", refusal{}, err
208 }
209 data, key, why, err := job.downloadLook(ctx, entry.staged.ID, pseudo,
210 look)
211 if err != nil || why.text != "" {
212 return nil, key, why, err
213 }
214 if err := job.store.bookkeep(ctx, job.lease, noChange); err != nil {
215 return nil, "", refusal{}, err
216 }
217 added, err := job.images.Add(ctx, job.who, data)
218 if why = job.storeRefusal(err); why.text != "" {
219 return nil, key, why, nil
220 }
221 if pseudo == iconPlace && added.Side() > MaxIconSide {
222 if err := job.images.Discard(ctx, job.who,
223 []AddedImage{added}); err != nil {
224 return nil, "", refusal{}, err
225 }
226 return nil, key, refusal{text: lookIconLarge, permanent: true}, nil
227 }
228 return added, key, refusal{}, nil
229}
230
231// downloadLook fetches the icon's or the cover's file, from the page
232// object read again for a fresh URL (once a page), through the pinned
233// transport. What the fresh object holds is checked as Survey's was: an
234// icon or cover changed since to one Mapa does not fetch gives no file
235// (Fable on phase 4, L3).
236func (job *importJob) downloadLook(ctx context.Context, pageID,
237 pseudo string, look *pageLook) ([]byte, string, refusal, error) {
238 if look.fresh == nil {
239 page, err := job.client.Page(ctx, pageID)
240 if err != nil {
241 return nil, "", refusal{text: fetchReason(err)}, finalOnly(err)
242 }
243 look.fresh = &page
244 }
245 address, limit := job.freshAddress(*look.fresh, pseudo)
246 if address == "" {
247 return nil, "", refusal{text: fetchReason(nil)}, nil
248 }
249 key := notion.FileKey(address)
250 data, err := job.files.Fetch(ctx, address, limit)
251 if err != nil {
252 large := notion.CodeOf(err) == notion.CodeTooLarge
253 text := fetchReason(err)
254 if large && pseudo == iconPlace {
255 text = lookIconLarge
256 }
257 return nil, key, refusal{text: text, permanent: large}, nil
258 }
259 return data, key, refusal{}, nil
260}
261
262// freshAddress is the address to fetch from a fresh page object, and
263// its cap, or "" when it holds no picture Mapa fetches.
264func (job *importJob) freshAddress(page notion.Page, pseudo string) (string,
265 int64) {
266 switch {
267 case pseudo == iconPlace && page.Icon != nil &&
268 iconFetched(page.Icon.Type):
269 return page.Icon.URL, MaxIconBytes
270 case pseudo == coverPlace && page.Cover != nil &&
271 job.coverFetched(&notionmd.Look{Type: page.Cover.Type,
272 URL: page.Cover.URL}):
273 return page.Cover.URL, imageCap
274 }
275 return "", 0
276}
277