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 "net/http"
6 "strings"
7
8 "github.com/go-chi/chi/v5"
9
10 "amadan.net/oficina/notion.git/notion"
11)
12
13// The import's screens (Notion import spec §8.4). Only the people in
14// MAPA_IMPORTERS reach them; everyone else gets a 404 on every route,
15// GET and POST, and with MAPA_IMPORTERS unset the routes do not exist
16// (spec §7, gate I13). Every action is a form button and works with
17// JavaScript off; Disconnect, Finish and Forget have confirm pages.
18
19const importPath = "/import/notion"
20
21// importRoutes mounts the import, only when someone may use it.
22func (app *app) importRoutes(router chi.Router) {
23 if len(app.config.Importers) == 0 {
24 return
25 }
26 router.Route(importPath, func(guarded chi.Router) {
27 guarded.Use(app.importerOnly)
28 guarded.Get("/", app.importPage)
29 guarded.Post("/connect", app.importConnect)
30 guarded.Post("/start", app.importStart)
31 guarded.Post("/startover", app.importStartOver)
32 guarded.Post("/resume", app.importResume)
33 guarded.Post("/stop", app.importStop)
34 for _, act := range []string{"disconnect", "finish", "forget"} {
35 guarded.Get("/"+act, app.importConfirm)
36 guarded.Post("/"+act, app.importAct)
37 }
38 })
39}
40
41// importerOnly answers 404 to anyone not in MAPA_IMPORTERS.
42func (app *app) importerOnly(next http.Handler) http.Handler {
43 return http.HandlerFunc(func(writer http.ResponseWriter,
44 request *http.Request) {
45 address, _ := app.identity(request)
46 if !app.config.Importer(address) {
47 notFound(writer, request)
48 return
49 }
50 next.ServeHTTP(writer, request)
51 })
52}
53
54func (app *app) importPage(writer http.ResponseWriter,
55 request *http.Request) {
56 app.showImport(writer, request, http.StatusOK, "")
57}
58
59func (app *app) showImport(writer http.ResponseWriter,
60 request *http.Request, status int, problem string) {
61 viewer, err := app.viewer(request)
62 if err != nil {
63 app.fail(writer, request, err)
64 return
65 }
66 data, err := app.importData(request)
67 if err != nil {
68 app.fail(writer, request, err)
69 return
70 }
71 data.Error = problem
72 app.show(writer, request, status, "import", view{
73 Title: "Import from Notion", Nav: nav(""), Viewer: viewer,
74 Data: data})
75}
76
77// importConnect verifies a pasted token with one call and saves it.
78// The token is never echoed back, not even on a refusal.
79func (app *app) importConnect(writer http.ResponseWriter,
80 request *http.Request) {
81 token := strings.TrimSpace(request.PostFormValue("token"))
82 if token == "" {
83 app.showImport(writer, request, http.StatusUnprocessableEntity,
84 "Paste the integration's secret to connect.")
85 return
86 }
87 workspace, err := app.notion.Verify(request.Context(), token)
88 if err != nil {
89 app.showImport(writer, request, http.StatusUnprocessableEntity,
90 verifyProblem(err))
91 return
92 }
93 address, _ := app.identity(request)
94 err = app.store.Connect(request.Context(), address, token, workspace)
95 app.importDone(writer, request, err, "connected")
96}
97
98// verifyProblem is a sentence for a token Notion did not accept. Never
99// Notion's own message, which quotes ids and names.
100func verifyProblem(err error) string {
101 if notion.CodeOf(err) == notion.CodeUnauthorized {
102 return "Notion didn't accept that secret. Check you copied the " +
103 "whole of it, from the integration's Configuration tab."
104 }
105 return "Mapa couldn't reach Notion to check that secret. Try again " +
106 "in a minute."
107}
108
109func (app *app) importStart(writer http.ResponseWriter,
110 request *http.Request) {
111 app.startRun(writer, request, false)
112}
113
114func (app *app) importStartOver(writer http.ResponseWriter,
115 request *http.Request) {
116 app.startRun(writer, request, true)
117}
118
119func (app *app) startRun(writer http.ResponseWriter,
120 request *http.Request, over bool) {
121 address, _ := app.identity(request)
122 lease, err := app.store.StartRun(request.Context(), address, over)
123 if err == nil {
124 err = app.begin(lease, address)
125 }
126 app.importDone(writer, request, err, "")
127}
128
129func (app *app) importResume(writer http.ResponseWriter,
130 request *http.Request) {
131 address, _ := app.identity(request)
132 lease, err := app.store.ResumeRun(request.Context(), address)
133 if err == nil {
134 err = app.begin(lease, address)
135 }
136 app.importDone(writer, request, err, "")
137}
138
139func (app *app) importStop(writer http.ResponseWriter,
140 request *http.Request) {
141 address, _ := app.identity(request)
142 run, err := app.store.StopRun(request.Context(), address)
143 if err == nil {
144 app.cancel(run)
145 }
146 app.importDone(writer, request, err, "")
147}
148
149// importDone goes back to the import's page, or shows a refusal there.
150func (app *app) importDone(writer http.ResponseWriter,
151 request *http.Request, err error, done string) {
152 switch {
153 case err == nil && done != "":
154 seeOther(writer, request, importPath+"?done="+done)
155 case err == nil:
156 seeOther(writer, request, importPath)
157 case errors.Is(err, ErrRunActive), errors.Is(err, ErrOtherWorkspace),
158 errors.Is(err, ErrNotConnected), errors.Is(err, ErrNoRun):
159 app.showImport(writer, request, http.StatusConflict,
160 importRefusal(err))
161 default:
162 app.fail(writer, request, err)
163 }
164}
165
166// refusalOf is the first sentence-bearing error in err.
167func importRefusal(err error) string {
168 for _, known := range []error{ErrRunActive, ErrOtherWorkspace,
169 ErrNotConnected, ErrNoRun} {
170 if errors.Is(err, known) {
171 return known.Error()
172 }
173 }
174 return err.Error()
175}
176