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 "encoding/json"
5 "net/http"
6 "path"
7
8 "amadan.net/oficina/mapa/internal/mapa/seal"
9 "amadan.net/oficina/notion.git/notion"
10)
11
12// importData is templates/import.html's data. Nothing in it is the
13// token: not a mask, not a prefix (spec §6, gate I11).
14type importData struct {
15 Connected bool
16 // Workspace is the connected workspace's name, for display.
17 Workspace string
18 // Finished is a map set aside by Finish: Connect starts a new
19 // import, and says so.
20 Finished bool
21 Run *runView
22 Report *reportView
23 Error string
24 Notice string
25 // The buttons on offer.
26 CanStart, CanResume, CanStartOver, CanStop bool
27 // Images is both image allowances, when Mapa has images (spec §12):
28 // the run cannot total Notion's images before it starts. NoImages is
29 // that this Mapa has no object store bound: images come in as lines.
30 Images *allowances
31 NoImages bool
32}
33
34// allowances is what the team's and the importer's image allowances
35// hold, in words.
36type allowances struct{ Team, Own string }
37
38// runView is the current or last run, as the page words it.
39type runView struct {
40 State string
41 Surveyed, Total, Written int
42 Active, Refresh bool
43 Code string
44}
45
46func (app *app) importData(request *http.Request) (importData, error) {
47 database := app.store.database.WithContext(request.Context())
48 var data importData
49 row, found, err := connection(database)
50 if err != nil {
51 return data, err
52 }
53 data.Connected = found
54 if found {
55 data.Workspace = app.workspaceName(row)
56 data.Images = app.allowances(request)
57 _, err := app.store.objects()
58 data.NoImages = err != nil
59 }
60 var finished int64
61 database.Model(&NotionPage{}).Where("state = ?", mapFinished).
62 Count(&finished)
63 data.Finished = finished > 0 && !found
64 run, ran, err := latestRun(database)
65 if err != nil || !ran {
66 data.CanStart = found
67 data.Notice = importNotice(request)
68 return data, err
69 }
70 app.offer(&data, run)
71 data.Notice = importNotice(request)
72 if run.State != runDone && run.State != runFailed {
73 // A run that has not ended has no report yet: its counts are
74 // partial, and a partial count must never read as a final one
75 // (spec §8.5). The page shows its progress instead.
76 return data, nil
77 }
78 data.Report, err = app.store.report(request.Context(), run)
79 return data, err
80}
81
82// offer is the run's view and the buttons it allows.
83func (app *app) offer(data *importData, run NotionRun) {
84 now := app.store.now().UTC()
85 data.Run = &runView{State: run.State, Surveyed: run.Surveyed,
86 Total: run.Total, Written: run.Written, Code: run.ErrorCode,
87 Active: live(run, now)}
88 data.Run.Refresh = data.Run.Active
89 data.CanStop = run.State == runSurveying || run.State == runWriting
90 data.CanResume = data.Connected && resumable(run, now)
91 data.CanStartOver = data.CanResume
92 data.CanStart = data.Connected && !data.CanStop && !data.CanResume
93}
94
95func (app *app) workspaceName(row NotionConnection) string {
96 plain, err := app.store.sealer.Open(seal.PurposeNotionWorkspace,
97 row.SealedWorkspace)
98 var workspace notion.Workspace
99 if err != nil || json.Unmarshal(plain, &workspace) != nil {
100 return ""
101 }
102 return workspace.Name
103}
104
105func importNotice(request *http.Request) string {
106 switch request.URL.Query().Get("done") {
107 case "connected":
108 return "Connected. Nothing is imported until you start."
109 case "disconnected":
110 return "Disconnected: Mapa no longer holds the secret. Now " +
111 "remove the integration from Notion's connection settings."
112 case "finished":
113 return "Import ended: Mapa no longer holds the secret. Now " +
114 "remove the integration from Notion's connection settings."
115 case "forgotten":
116 return "Forgotten: Mapa no longer holds the secret, or knows " +
117 "which Notion page each imported page came from. The " +
118 "pages stay. Now remove the integration from Notion's " +
119 "connection settings."
120 }
121 return ""
122}
123
124// importConfirm is Disconnect's, Finish's and Forget's own page.
125func (app *app) importConfirm(writer http.ResponseWriter,
126 request *http.Request) {
127 viewer, err := app.viewer(request)
128 if err != nil {
129 app.fail(writer, request, err)
130 return
131 }
132 database := app.store.database.WithContext(request.Context())
133 _, active, err := activeRun(database)
134 if err != nil {
135 app.fail(writer, request, err)
136 return
137 }
138 act := path.Base(request.URL.Path)
139 app.show(writer, request, http.StatusOK, "import_confirm", view{
140 Title: "Import from Notion", Nav: nav(""), Viewer: viewer,
141 Data: map[string]any{"Act": act, "Active": active,
142 "Action": request.URL.Path}})
143}
144
145// importAct is Disconnect, Finish or Forget, confirmed.
146func (app *app) importAct(writer http.ResponseWriter,
147 request *http.Request) {
148 address, _ := app.identity(request)
149 ctx := request.Context()
150 var err error
151 act := path.Base(request.URL.Path)
152 switch act {
153 case "disconnect":
154 err = app.store.Disconnect(ctx, address)
155 case "finish":
156 err = app.store.Finish(ctx, address)
157 default:
158 err = app.store.Forget(ctx, address)
159 }
160 done := map[string]string{"disconnect": "disconnected",
161 "finish": "finished", "forget": "forgotten"}[act]
162 app.importDone(writer, request, err, done)
163}
164
165// allowances is both image allowances for the viewer, or nil when Mapa
166// has no images.
167func (app *app) allowances(request *http.Request) *allowances {
168 if _, err := app.store.objects(); err != nil {
169 return nil
170 }
171 viewer, err := app.viewer(request)
172 if err != nil {
173 return nil
174 }
175 own, team, err := app.store.imageUse(request.Context(), viewer.MemberID)
176 if err != nil {
177 return nil
178 }
179 setup := app.store.images.setup
180 dates := viewer.Locale
181 return &allowances{Team: byteSize(dates, team) + " of " +
182 byteSize(dates, setup.QuotaTeam), Own: byteSize(dates, own) + " of " +
183 byteSize(dates, setup.QuotaPerson)}
184}
185