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 "html/template"
6 "net/http"
7 "strconv"
8
9 "github.com/go-chi/chi/v5"
10
11 "amadan.net/oficina/mapa/internal/mapa/markup"
12)
13
14// Private pages' screens: scratch pages, "Make team page", and the time
15// zone setting.
16
17func (app *app) privateRoutes(router chi.Router) {
18 router.Get("/scratch", app.listScratch)
19 router.Get("/scratch/new", app.newScratch)
20 router.Post("/scratch", app.createScratch)
21 router.Get("/p/{id}/share", app.shareForm)
22 router.Post("/p/{id}/share", countedHere(app.share))
23 router.Get("/settings", app.settings)
24 router.Post("/settings", app.saveSettings)
25 router.Post("/settings/locale", app.saveLocale)
26}
27
28func (app *app) listScratch(writer http.ResponseWriter,
29 request *http.Request) {
30 viewer, err := app.viewer(request)
31 if err != nil {
32 app.fail(writer, request, err)
33 return
34 }
35 pages, err := app.index.OwnPages(request.Context(), ScopeOf(viewer),
36 KindScratch)
37 if err != nil {
38 app.fail(writer, request, err)
39 return
40 }
41 app.show(writer, request, http.StatusOK, "scratch", view{
42 Title: "Scratch", Nav: nav("/scratch"), Viewer: viewer,
43 Data: map[string]any{"Pages": pages}})
44}
45
46// newScratchMarkdown is New scratch page in the Markdown textarea: the
47// floor without script (level-up §6.10), and View → Edit as Markdown.
48const newScratchMarkdown = "/scratch/new?markdown=1"
49
50func (app *app) newScratch(writer http.ResponseWriter,
51 request *http.Request) {
52 viewer, err := app.viewer(request)
53 var island template.JS
54 if err == nil && request.URL.Query().Get("markdown") != "1" {
55 island, err = app.islandFor(request.Context(), viewer, PageView{},
56 nil, "/scratch")
57 }
58 if err != nil {
59 app.fail(writer, request, err)
60 return
61 }
62 app.showForm(writer, request, http.StatusOK, "scratch_new",
63 pageForm{Action: "/scratch", Label: "New scratch page",
64 Page: PageView{Kind: KindScratch, Label: "New scratch page",
65 HTML: markup.EmptyParagraph}, Island: island})
66}
67
68func (app *app) createScratch(writer http.ResponseWriter,
69 request *http.Request) {
70 if isUpload(request) {
71 app.uploadOnScratch(writer, request)
72 return
73 }
74 if token := request.PostFormValue("first"); token != "" {
75 app.firstScratch(writer, request, token)
76 return
77 }
78 address, _ := app.identity(request)
79 body := request.PostFormValue("body")
80 page, err := app.store.CreateScratch(request.Context(), address, body)
81 if errors.Is(err, ErrEmpty) {
82 app.showForm(writer, request, http.StatusUnprocessableEntity,
83 "scratch_new", pageForm{Action: "/scratch", Error: err.Error(),
84 Label: "New scratch page", Page: PageView{Kind: KindScratch}})
85 return
86 }
87 if err != nil {
88 app.fail(writer, request, err)
89 return
90 }
91 seeOther(writer, request, PagePath(page))
92}
93
94// shareData is templates/share.html's data.
95type shareData struct {
96 Page PageView
97 Blocks []Context
98 Chosen map[int]bool
99 Title, Error string
100 Taken, Action string
101}
102
103func (app *app) shareForm(writer http.ResponseWriter,
104 request *http.Request) {
105 app.showShare(writer, request, http.StatusOK, shareData{})
106}
107
108func (app *app) showShare(writer http.ResponseWriter,
109 request *http.Request, status int, data shareData) {
110 page, ok := app.visible(writer, request)
111 if !ok {
112 return
113 }
114 viewer, err := app.viewer(request)
115 if err == nil && page.Rung != RungPrivate {
116 err = ErrNotFound
117 }
118 if err == nil {
119 data.Blocks, err = app.index.Blocks(request.Context(),
120 ScopeOf(viewer), page.ID)
121 }
122 if err != nil {
123 app.refuse(writer, request, err)
124 return
125 }
126 data.Page, data.Action = page, request.URL.Path
127 app.show(writer, request, status, "share", view{Title: "Make team page",
128 Nav: nav(navFor(page.Kind)), Viewer: viewer, Data: data})
129}
130
131func (app *app) share(writer http.ResponseWriter, request *http.Request) {
132 ref, ok := app.pathPage(request, "id")
133 if !ok || request.ParseForm() != nil {
134 notFound(writer, request)
135 return
136 }
137 data := shareData{Title: request.PostFormValue("title"),
138 Chosen: map[int]bool{}}
139 var chosen []int
140 for _, raw := range request.PostForm["block"] {
141 if number, err := strconv.Atoi(raw); err == nil {
142 chosen, data.Chosen[number] = append(chosen, number), true
143 }
144 }
145 address, _ := app.identity(request)
146 shared, err := app.store.MakeTeamPage(request.Context(), address,
147 ref.ID, formInt(request, "revision"), chosen, data.Title)
148 if sayable(err) || errors.Is(err, ErrNothingChosen) ||
149 errors.Is(err, ErrNoteChanged) || errors.Is(err, ErrCutsLayout) {
150 data.Error = err.Error()
151 data.Taken = app.index.TitleHref(request.Context(), data.Title)
152 app.refuseShare(writer, request, data)
153 return
154 }
155 if err != nil {
156 app.refuse(writer, request, err)
157 return
158 }
159 if wantsJSON(request) {
160 app.sharedJSON(writer, request, ref.ID, shared)
161 return
162 }
163 seeOther(writer, request, PagePath(shared))
164}
165
166// refuseShare asks for another title or another choice: the form again,
167// or, for the editor, the sentence as JSON (level-up §6.7).
168func (app *app) refuseShare(writer http.ResponseWriter,
169 request *http.Request, data shareData) {
170 if !wantsJSON(request) {
171 app.showShare(writer, request, http.StatusUnprocessableEntity, data)
172 return
173 }
174 refusedJSON(writer, data.Error, data.Taken)
175}
176
177// sharedJSON answers the editor: the new team page, and the note as it
178// now stands, with the link in the blocks' place, so the editor takes
179// that text and revision as its own.
180func (app *app) sharedJSON(writer http.ResponseWriter,
181 request *http.Request, noteID int64, shared Page) {
182 viewer, err := app.viewer(request)
183 note := PageView{}
184 if err == nil {
185 note, err = app.index.View(request.Context(), ScopeOf(viewer),
186 noteID)
187 }
188 if err != nil {
189 app.refuse(writer, request, err)
190 return
191 }
192 answer(writer, map[string]any{"href": PagePath(shared),
193 "revision": idText(note.Revision), "body": note.Body})
194}
195