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 "strings"
9 "time"
10
11 "github.com/go-chi/chi/v5"
12
13 "amadan.net/oficina/mapa/internal/mapa/markup"
14)
15
16// pageRoutes are every signed-in page.
17func (app *app) pageRoutes(router chi.Router) {
18 router.Get("/", app.today)
19 router.Get("/d/{date}", app.day)
20 router.Get("/d/{date}/edit", app.day)
21 router.Post("/d/{date}", app.saveDay)
22 app.teamPageRoutes(router)
23 app.namingRoutes(router)
24 app.privateRoutes(router)
25 app.searchRoutes(router)
26 app.viewRoutes(router)
27 app.pinRoutes(router)
28 app.imageRoutes(router)
29 app.lookRoutes(router)
30}
31
32// viewer resolves the signed-in person without writing anything.
33func (app *app) viewer(request *http.Request) (Viewer, error) {
34 address, _ := app.identity(request)
35 viewer, err := app.store.Viewer(request.Context(), address,
36 app.config.TimeZone)
37 viewer.Locale = localeIn(request)
38 return viewer, err
39}
40
41// todayIn is the ISO date it is now in zone.
42func todayIn(now time.Time, zone *time.Location) string {
43 return now.In(zone).Format(time.DateOnly)
44}
45
46// today is the landing page: today's note, in the person's own time
47// zone (§6). It is a GET, so it writes nothing, and until something is
48// saved no row for the day exists (gate 10).
49func (app *app) today(writer http.ResponseWriter, request *http.Request) {
50 viewer, err := app.viewer(request)
51 if err != nil {
52 app.fail(writer, request, err)
53 return
54 }
55 date := todayIn(app.store.now(), viewer.TimeZone)
56 app.showDay(writer, request, viewer, date, http.StatusOK, "")
57}
58
59func (app *app) day(writer http.ResponseWriter, request *http.Request) {
60 date := chi.URLParam(request, "date")
61 viewer, err := app.viewer(request)
62 if !markup.IsDate(date) || err != nil {
63 app.refuse(writer, request, errors.Join(ErrNotFound, err))
64 return
65 }
66 app.showDay(writer, request, viewer, date, http.StatusOK, "")
67}
68
69// dayData is templates/day.html's data.
70type dayData struct {
71 Page PageView
72 Written, Editing bool
73 Today, Prev, Next string
74 Body, Error string
75 Notice string
76 // Images is "Images on this page" under the Markdown screen.
77 Images []ListedImage
78 // Revision is the editor's base and Version its draft's; Draft is
79 // a draft a closed tab left, offered as Save or Discard.
80 Revision, Version int64
81 Draft *DraftView
82 // Orphans and Elsewhere are the person's other drafts, offered on
83 // today's note (§5): on deleted pages, and on live ones.
84 Orphans, Elsewhere []Unsaved
85 // Drafts is where the editor autosaves; empty for none.
86 Drafts string
87 // Week and Pill are day navigation (level-up §4.4): the panel's
88 // strip, and ‹ Today › with its month.
89 Week *WeekView
90 Pill *dayPill
91 // Saved is ?saved=1, the committed text with the draft still
92 // offered; Island is the editor's data (level-up §6.8).
93 Saved bool
94 Island template.JS
95}
96
97// dayCalendar fills in the week strip and the month calendar, both read
98// from the viewer's own notes only. ?month= pages the calendar.
99func (app *app) dayCalendar(request *http.Request, viewer Viewer,
100 data *dayData) error {
101 ctx, scope, date := request.Context(), ScopeOf(viewer), data.Page.Date
102 week, err := app.index.Week(ctx, scope, date, data.Today)
103 if err != nil {
104 return err
105 }
106 month, err := app.index.Month(ctx, scope,
107 request.URL.Query().Get("month"), date, data.Today)
108 if err != nil {
109 return err
110 }
111 data.Week = &week
112 data.Pill = &dayPill{Date: date, Today: data.Today, Prev: data.Prev,
113 Next: data.Next, Month: &month,
114 Open: request.URL.Query().Get("month") != ""}
115 return nil
116}
117
118func (app *app) showDay(writer http.ResponseWriter, request *http.Request,
119 viewer Viewer, date string, status int, problem string) {
120 page, written, err := app.index.Day(request.Context(),
121 ScopeOf(viewer), date)
122 if err != nil {
123 app.fail(writer, request, err)
124 return
125 }
126 day, _ := time.Parse(time.DateOnly, date)
127 data := dayData{Page: page, Written: written, Error: problem,
128 Today: todayIn(app.store.now(), viewer.TimeZone),
129 Prev: day.AddDate(0, 0, -1).Format(time.DateOnly),
130 Next: day.AddDate(0, 0, 1).Format(time.DateOnly),
131 Body: page.Body, Notice: notice(request), Revision: page.Revision,
132 Editing: problem != "" ||
133 strings.HasSuffix(request.URL.Path, "/edit")}
134 // No autosave in the editor a stale Save is shown again in (review
135 // N4): only an explicit Save may replace the newer draft.
136 if problem != staleDay {
137 data.Drafts = "/d/" + date + "/draft"
138 }
139 // In order: the drafts, the editor, the Markdown screen, the week.
140 for _, fill := range []func(*http.Request, Viewer, *dayData) error{
141 app.dayDrafts, app.dayEditor, app.dayForm, app.dayCalendar} {
142 if err := fill(request, viewer, &data); err != nil {
143 app.fail(writer, request, err)
144 return
145 }
146 }
147 app.show(writer, request, status, "day", view{
148 Title: viewer.Locale.Long(date),
149 Nav: nav(navFor(KindDaily)), Viewer: viewer, Data: data,
150 Chrome: &pageChrome{Page: &data.Page, Editor: data.Editing,
151 Writable: data.Island != "" && !data.Editing,
152 Day: data.Pill, Links: true}})
153}
154
155// dayForm is the day's Markdown screen: the text a POST brought, an
156// upload's notice (images §4.4), and "Images on this page".
157func (app *app) dayForm(request *http.Request, viewer Viewer,
158 data *dayData) error {
159 if note, ok := uploadedOf(request); ok {
160 data.Editing = true
161 if note.added {
162 data.Notice = note.say
163 }
164 }
165 if request.Method == http.MethodPost {
166 data.Body = request.PostFormValue("body")
167 }
168 if !data.Editing {
169 return nil
170 }
171 var err error
172 data.Images, err = app.store.PageImages(request.Context(), viewer,
173 data.Page.ID, data.Body)
174 return err
175}
176
177// dayDrafts fills in the day's own draft, which the editor holds, and on
178// today's note the person's other drafts.
179func (app *app) dayDrafts(request *http.Request, viewer Viewer,
180 data *dayData) error {
181 key := DraftKey{Date: data.Page.Date}
182 if data.Written {
183 key = DraftKey{PageID: data.Page.ID}
184 }
185 draft, found, err := app.store.OwnDraft(request.Context(), viewer, key)
186 if err != nil {
187 return err
188 }
189 if found {
190 data.Draft, data.Body = &draft, draft.Body
191 data.Revision, data.Version = draft.BaseRevision, draft.Version
192 }
193 if data.Page.Date != data.Today {
194 return nil
195 }
196 others, err := app.store.UnsavedDrafts(request.Context(), viewer)
197 for _, other := range others {
198 switch {
199 case other.Gone:
200 data.Orphans = append(data.Orphans, other)
201 case other.PageID != data.Page.ID:
202 data.Elsewhere = append(data.Elsewhere, other)
203 }
204 }
205 return err
206}
207
208// saveDay is the scriptless Save. The first save that holds text creates
209// the note; an empty one creates nothing; a save over an older revision
210// merges by block (§5).
211func (app *app) saveDay(writer http.ResponseWriter, request *http.Request) {
212 date := chi.URLParam(request, "date")
213 viewer, err := app.viewer(request)
214 if !markup.IsDate(date) || err != nil {
215 app.refuse(writer, request, errors.Join(ErrNotFound, err))
216 return
217 }
218 if isUpload(request) {
219 app.uploadOnDay(writer, request, viewer, date)
220 return
221 }
222 base, err := strconv.ParseInt(
223 firstOf(request.PostFormValue("revision"), "0"), 10, 64)
224 if err != nil {
225 notFound(writer, request)
226 return
227 }
228 outcome, pageID, err := app.store.SaveDay(request.Context(), viewer,
229 date, base, formInt(request, "version"),
230 request.PostFormValue("body"))
231 switch {
232 case errors.Is(err, ErrEmpty):
233 app.showDay(writer, request, viewer, date,
234 http.StatusUnprocessableEntity, err.Error())
235 case errors.Is(err, ErrStaleTab):
236 app.showDay(writer, request, viewer, date, http.StatusConflict,
237 staleDay)
238 case err != nil:
239 app.fail(writer, request, err)
240 default:
241 app.afterDay(writer, request, date, pageID, outcome)
242 }
243}
244
245// afterDay sends a saved day to its note, or a conflict to its view.
246func (app *app) afterDay(writer http.ResponseWriter, request *http.Request,
247 date string, pageID int64, outcome Outcome) {
248 public, err := app.store.publicOf(request.Context(), pageID)
249 if err != nil {
250 app.fail(writer, request, err)
251 return
252 }
253 seeOther(writer, request, afterSave("/d/"+date, public, outcome))
254}
255
256// staleDay is the day editor's answer to a stale Save.
257const staleDay = "Another tab of yours has newer unsaved changes to " +
258 "this note. This tab's text is below; saving it replaces them."
259