Presenting: the whole talk as one payload, notes for the presenter only
GET /p/{session}/talk lists every slide of the full talk, rendered at
press 0, with its assets, thumbnail and (for whoever may drive) notes
split at the press lines. ETag from the deck seq, 304, no-store.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
6 files changed,
+356
−0
internal/slides/app.go+1 −0internal/slides/deck/presses.go+23 −0internal/slides/deck/presses_test.go+31 −0internal/slides/presenting_talk.go+104 −0internal/slides/render/render.go+32 −0internal/slidestest/present_talk_test.go+165 −0
diff --git a/internal/slides/app.go b/internal/slides/app.go| index 7afcabf..1b1038e 100644 |
| --- a/internal/slides/app.go |
| +++ b/internal/slides/app.go |
| @@ -152,6 +152,7 @@ func New(d *db.DB, cfg Config) (*http.ServeMux, *app, error) { |
| r.Get("/p/{session}", a.presentSurface) |
| r.Get("/p/{session}/notes", a.presenterNotes) |
| r.Get("/p/{session}/stream", a.presentStream) |
| + r.Get("/p/{session}/talk", a.presentTalk) |
| r.Get("/p/{session}/remote", a.remote) |
| r.Get("/p/{session}/assets/{ref}", a.presentAsset) |
| r.Get("/p/{session}/slides/{file}", a.presentSlideThumb) |
diff --git a/internal/slides/deck/presses.go b/internal/slides/deck/presses.go| index 2c5f988..483b32f 100644 |
| --- a/internal/slides/deck/presses.go |
| +++ b/internal/slides/deck/presses.go |
| @@ -229,3 +229,26 @@ func children(b Block) []string { |
| } |
| return nil |
| } |
| + |
| +// pressLine is the paragraph that ends one section of a slide's notes |
| +// and starts the next: exactly this, in plain text. |
| +const pressLine = "(Press.)" |
| + |
| +// NoteParts splits a slide's notes at its press lines, so each part is |
| +// what to say at one point of the slide: the first when the slide |
| +// arrives, the next after the first press, and so on. A paragraph is a |
| +// press line when its plain text, trimmed of surrounding white space, is |
| +// exactly "(Press.)"; marks inside it do not matter, case does. Press |
| +// lines are dropped. Notes with none are one part, and a leading or |
| +// trailing line leaves an empty part. |
| +func NoteParts(t Text) []Text { |
| + parts := []Text{nil} |
| + for _, para := range t { |
| + if strings.TrimSpace(Text{para}.Plain()) == pressLine { |
| + parts = append(parts, nil) |
| + continue |
| + } |
| + parts[len(parts)-1] = append(parts[len(parts)-1], para) |
| + } |
| + return parts |
| +} |
diff --git a/internal/slides/deck/presses_test.go b/internal/slides/deck/presses_test.go| index 77876a6..57e0ab5 100644 |
| --- a/internal/slides/deck/presses_test.go |
| +++ b/internal/slides/deck/presses_test.go |
| @@ -64,3 +64,34 @@ func TestArrivalOfNumbersEveryRevealByItsPress(t *testing.T) { |
| t.Errorf("ArrivalOf(no blocks) = %v, want empty", got) |
| } |
| } |
| + |
| +func TestNotePartsSplitAtThePressLine(t *testing.T) { |
| + para := func(runs ...deck.Run) []deck.Run { return runs } |
| + p := func(s string) []deck.Run { return para(deck.Run{T: s}) } |
| + plain := func(parts []deck.Text) []string { |
| + var out []string |
| + for _, part := range parts { |
| + out = append(out, part.Plain()) |
| + } |
| + return out |
| + } |
| + for name, tc := range map[string]struct { |
| + in deck.Text |
| + want []string |
| + }{ |
| + "no line": {deck.Text{p("a"), p("b")}, []string{"a\nb"}}, |
| + "empty": {nil, []string{""}}, |
| + "middle": {deck.Text{p("a"), p("(Press.)"), p("b")}, []string{"a", "b"}}, |
| + "leading": {deck.Text{p("(Press.)"), p("a")}, []string{"", "a"}}, |
| + "trailing": {deck.Text{p("a"), p("(Press.)")}, []string{"a", ""}}, |
| + "two": {deck.Text{p("a"), p("(Press.)"), p("b"), p("(Press.)"), p("c")}, []string{"a", "b", "c"}}, |
| + "marks inside": {deck.Text{p("a"), para(deck.Run{T: "(", B: true}, deck.Run{T: "Press.)"}), p("b")}, []string{"a", "b"}}, |
| + "surrounding space": {deck.Text{p("a"), p(" (Press.) "), p("b")}, []string{"a", "b"}}, |
| + "lowercase": {deck.Text{p("a"), p("(press.)"), p("b")}, []string{"a\n(press.)\nb"}}, |
| + "inside a line": {deck.Text{p("(Press.) now")}, []string{"(Press.) now"}}, |
| + } { |
| + if got := plain(deck.NoteParts(tc.in)); !reflect.DeepEqual(got, tc.want) { |
| + t.Errorf("%s: parts %q, want %q", name, got, tc.want) |
| + } |
| + } |
| +} |
diff --git a/internal/slides/presenting_talk.go b/internal/slides/presenting_talk.go| new file mode 100644 |
| index 0000000..2bf9736 |
| --- /dev/null |
| +++ b/internal/slides/presenting_talk.go |
| @@ -0,0 +1,104 @@ |
| +// presenting_talk.go serves the whole talk as one payload, so the |
| +// presenter and slide windows hold every slide in memory before the |
| +// first press and a press needs no network. |
| +package slides |
| + |
| +import ( |
| + "encoding/json" |
| + "net/http" |
| + "net/url" |
| + "strconv" |
| + |
| + "amadan.net/oficina/slides/internal/slides/deck" |
| + slidehtml "amadan.net/oficina/slides/internal/slides/render" |
| +) |
| + |
| +type talkSlide struct { |
| + ID string `json:"id"` |
| + Number int `json:"number"` |
| + Title string `json:"title"` |
| + Full bool `json:"full"` |
| + Presses int `json:"presses"` |
| + HTML string `json:"html"` // rendered with Reveal at 0 |
| + Assets []string `json:"assets"` |
| + Thumb string `json:"thumb"` |
| + Notes []string `json:"notes,omitempty"` // one HTML part per press section |
| +} |
| + |
| +type talkPayload struct { |
| + DeckSeq int64 `json:"deckSeq"` |
| + Arrive string `json:"arrive"` |
| + Slides []talkSlide `json:"slides"` |
| +} |
| + |
| +// presentTalk is GET /p/{session}/talk. Anyone the session lets in gets |
| +// the slides; the notes go only to who may drive (mayDrive), so the |
| +// slide window never holds them. |
| +func (a *app) presentTalk(w http.ResponseWriter, r *http.Request) { |
| + p, ok := a.presentationFor(w, r) |
| + if !ok { |
| + return |
| + } |
| + d, ok := a.deckOf(w, r, p) |
| + if !ok { |
| + return |
| + } |
| + withNotes := a.mayDrive(r, p, r.URL.Query().Get("t")) |
| + |
| + // The tag is the deck's seq, and says whether notes are in the body: |
| + // a body without them must not satisfy a request that may have them. |
| + etag := strconv.Quote(strconv.FormatInt(d.Seq, 10)) |
| + if withNotes { |
| + etag = strconv.Quote(strconv.FormatInt(d.Seq, 10) + "-n") |
| + } |
| + w.Header().Set("Cache-Control", "no-store") |
| + w.Header().Set("ETag", etag) |
| + if matchesETag(r.Header.Get("If-None-Match"), etag) { |
| + w.WriteHeader(http.StatusNotModified) |
| + return |
| + } |
| + |
| + h, view, err := a.openDeck(r.Context(), d) |
| + if err != nil { |
| + a.fail(w, r, "open a deck for the talk", err) |
| + return |
| + } |
| + defer h.Close(r.Context()) |
| + view = view.ForReaders() |
| + |
| + base := "/p/" + p.Sqid |
| + zero := 0 |
| + arrive := view.Meta.Arrive |
| + if arrive == "" { |
| + arrive = "appear" |
| + } |
| + out := talkPayload{DeckSeq: d.Seq, Arrive: arrive, Slides: []talkSlide{}} |
| + for n, i := range deck.TalkOrder(view, deck.ModeFull) { |
| + s := view.Slides[i] |
| + html, err := slidehtml.SlideWith(view, i, slidehtml.Desktop, |
| + slidehtml.Options{AssetBase: base + "/assets", Reveal: &zero}) |
| + if err != nil { |
| + a.fail(w, r, "render a slide for the talk", err) |
| + return |
| + } |
| + ts := talkSlide{ |
| + ID: s.ID, |
| + Number: n + 1, |
| + Title: slideLabel(r, s, i+1), |
| + Full: s.Talk == "full", |
| + Presses: len(deck.Presses(s)), |
| + HTML: string(html), |
| + Assets: slidehtml.AssetPaths(view, i, base+"/assets"), |
| + Thumb: base + "/slides/" + url.PathEscape(s.ID) + ".svg", |
| + } |
| + if withNotes && len(s.Notes) > 0 { |
| + for _, part := range deck.NoteParts(s.Notes) { |
| + ts.Notes = append(ts.Notes, string(slidehtml.Notes(part))) |
| + } |
| + } |
| + out.Slides = append(out.Slides, ts) |
| + } |
| + |
| + w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| + json.NewEncoder(w).Encode(out) |
| +} |
diff --git a/internal/slides/render/render.go b/internal/slides/render/render.go| index 525b5e5..411a56b 100644 |
| --- a/internal/slides/render/render.go |
| +++ b/internal/slides/render/render.go |
| @@ -1426,3 +1426,35 @@ func clamp(v, lo, hi float64) float64 { |
| } |
| return v |
| } |
| + |
| +// AssetPaths is every asset the slide at index draws (its background |
| +// and its image boxes), as the URLs render writes for them under |
| +// assetBase, each once, in block order. It reads the same ids the |
| +// renderer does, so a window can fetch the pictures before the slide |
| +// is shown. |
| +func AssetPaths(d *deck.Deck, index int, assetBase string) []string { |
| + out := []string{} |
| + if d == nil || index < 0 || index >= len(d.Slides) { |
| + return out |
| + } |
| + c := &renderer{deck: d, assetBase: strings.TrimSuffix(strings.TrimSpace(assetBase), "/")} |
| + s := d.Slides[index] |
| + seen := map[string]bool{} |
| + add := func(id string) { |
| + ref := c.assetRef(safeAssetID(id)) |
| + if ref == "" || seen[ref] { |
| + return |
| + } |
| + seen[ref] = true |
| + out = append(out, c.assetPath(ref)) |
| + } |
| + if s.Background.Kind == "image" { |
| + add(s.Background.Value) |
| + } |
| + for _, b := range s.Blocks { |
| + if b.Type == "image" { |
| + add(stringProp(b.Props, "asset")) |
| + } |
| + } |
| + return out |
| +} |
diff --git a/internal/slidestest/present_talk_test.go b/internal/slidestest/present_talk_test.go| new file mode 100644 |
| index 0000000..b20e9e0 |
| --- /dev/null |
| +++ b/internal/slidestest/present_talk_test.go |
| @@ -0,0 +1,165 @@ |
| +package slidestest |
| + |
| +import ( |
| + "encoding/json" |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +type talkSlideJSON struct { |
| + ID string `json:"id"` |
| + Number int `json:"number"` |
| + Title string `json:"title"` |
| + Full bool `json:"full"` |
| + Presses int `json:"presses"` |
| + HTML string `json:"html"` |
| + Assets []string `json:"assets"` |
| + Thumb string `json:"thumb"` |
| + Notes []string `json:"notes"` |
| +} |
| + |
| +type talkJSON struct { |
| + DeckSeq int64 `json:"deckSeq"` |
| + Arrive string `json:"arrive"` |
| + Slides []talkSlideJSON `json:"slides"` |
| +} |
| + |
| +// talkDeck is stepsDeck with notes on its first slide: one before the |
| +// press line, one after. |
| +func talkDeck(t *testing.T, a *testApp, cookies []*http.Cookie) string { |
| + t.Helper() |
| + path := stepsDeck(t, a, cookies) |
| + token := mintAgent(t, a, cookies, "talk-notes") |
| + apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+strings.TrimPrefix(path, "/d/")+"/ops", token, |
| + `{"ops":[{"op":"set_notes","id":"steps","markdown":"Open warmly\n\n(Press.)\n\nThen the point"}]}`, http.StatusOK) |
| + return path |
| +} |
| + |
| +func talkRequest(a *testApp, session, query, ifNoneMatch string, cookies []*http.Cookie) *httptest.ResponseRecorder { |
| + req := httptest.NewRequest(http.MethodGet, "/p/"+session+"/talk"+query, nil) |
| + if ifNoneMatch != "" { |
| + req.Header.Set("If-None-Match", ifNoneMatch) |
| + } |
| + for _, c := range cookies { |
| + req.AddCookie(c) |
| + } |
| + rec := httptest.NewRecorder() |
| + a.ServeHTTP(rec, req) |
| + return rec |
| +} |
| + |
| +func decodeTalk(t *testing.T, rec *httptest.ResponseRecorder) talkJSON { |
| + t.Helper() |
| + if rec.Code != http.StatusOK { |
| + t.Fatalf("talk: status %d, want 200\n%s", rec.Code, rec.Body) |
| + } |
| + var out talkJSON |
| + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { |
| + t.Fatalf("talk: %v\n%s", err, rec.Body) |
| + } |
| + return out |
| +} |
| + |
| +func TestTalkPayloadWithholdsNotesAndParked(t *testing.T) { |
| + a := newApp(t) |
| + cookies := signedIn(t, a, allowedAddress) |
| + session, token := startPresenting(t, a, talkDeck(t, a, cookies), cookies) |
| + |
| + for name, tc := range map[string]struct { |
| + query string |
| + cookies []*http.Cookie |
| + notes bool |
| + }{ |
| + "token": {"?t=" + token, nil, true}, |
| + "presenter": {"", cookies, true}, |
| + "slide": {"", nil, false}, |
| + "bad token": {"?t=nope", nil, false}, |
| + } { |
| + rec := talkRequest(a, session, tc.query, "", tc.cookies) |
| + got := decodeTalk(t, rec) |
| + if cc := rec.Header().Get("Cache-Control"); cc != "no-store" { |
| + t.Errorf("%s: Cache-Control %q, want no-store", name, cc) |
| + } |
| + var ids []string |
| + for _, s := range got.Slides { |
| + ids = append(ids, s.ID) |
| + } |
| + if want := "steps,plain,extra,last"; strings.Join(ids, ",") != want { |
| + t.Errorf("%s: slides %v, want %s (parked left out, full-only in)", name, ids, want) |
| + } |
| + first, extra := got.Slides[0], got.Slides[2] |
| + if first.Number != 1 || extra.Number != 3 || !extra.Full || first.Full { |
| + t.Errorf("%s: numbers/full wrong: %+v / %+v", name, first, extra) |
| + } |
| + if first.Presses != 4 || got.Slides[1].Presses != 0 { |
| + t.Errorf("%s: presses %d and %d, want 4 and 0", name, first.Presses, got.Slides[1].Presses) |
| + } |
| + if !strings.Contains(first.HTML, "data-press") || !strings.Contains(first.HTML, "data-waiting") { |
| + t.Errorf("%s: html not rendered at press 0: %s", name, first.HTML) |
| + } |
| + if first.Thumb != "/p/"+session+"/slides/steps.svg" { |
| + t.Errorf("%s: thumb %q", name, first.Thumb) |
| + } |
| + if got.Arrive != "appear" || got.DeckSeq == 0 { |
| + t.Errorf("%s: arrive %q, deckSeq %d", name, got.Arrive, got.DeckSeq) |
| + } |
| + if tc.notes { |
| + if len(first.Notes) != 2 || !strings.Contains(first.Notes[0], "Open warmly") || |
| + !strings.Contains(first.Notes[1], "Then the point") || strings.Contains(strings.Join(first.Notes, ""), "Press.") { |
| + t.Errorf("%s: notes %q, want two parts without the press line", name, first.Notes) |
| + } |
| + } else { |
| + for _, s := range got.Slides { |
| + if len(s.Notes) != 0 { |
| + t.Errorf("%s: %s carries notes %q", name, s.ID, s.Notes) |
| + } |
| + } |
| + if strings.Contains(rec.Body.String(), "Open warmly") { |
| + t.Errorf("%s: the body holds the notes", name) |
| + } |
| + } |
| + } |
| +} |
| + |
| +func TestTalkPayloadAnswersUnchanged(t *testing.T) { |
| + a := newApp(t) |
| + cookies := signedIn(t, a, allowedAddress) |
| + session, token := startPresenting(t, a, talkDeck(t, a, cookies), cookies) |
| + |
| + rec := talkRequest(a, session, "?t="+token, "", nil) |
| + etag := rec.Header().Get("ETag") |
| + if etag == "" { |
| + t.Fatal("no ETag") |
| + } |
| + again := talkRequest(a, session, "?t="+token, etag, nil) |
| + if again.Code != http.StatusNotModified || again.Body.Len() != 0 { |
| + t.Errorf("matching tag: status %d, body %d bytes, want 304 and none", again.Code, again.Body.Len()) |
| + } |
| + if cc := again.Header().Get("Cache-Control"); cc != "no-store" { |
| + t.Errorf("304 Cache-Control %q, want no-store", cc) |
| + } |
| + // The notes-free body is another representation: the tag does not carry over. |
| + if rec := talkRequest(a, session, "", etag, nil); rec.Code != http.StatusOK { |
| + t.Errorf("the slide window with the presenter's tag: status %d, want 200", rec.Code) |
| + } |
| + if rec := talkRequest(a, session, "?t="+token, `"0"`, nil); rec.Code != http.StatusOK { |
| + t.Errorf("a stale tag: status %d, want 200", rec.Code) |
| + } |
| +} |
| + |
| +func TestTalkPayloadUnknownAndEnded(t *testing.T) { |
| + a := newApp(t) |
| + cookies := signedIn(t, a, allowedAddress) |
| + session, _ := startPresenting(t, a, talkDeck(t, a, cookies), cookies) |
| + if rec := talkRequest(a, session+"zzzz", "", "", nil); rec.Code != http.StatusNotFound { |
| + t.Errorf("unknown session: status %d, want 404", rec.Code) |
| + } |
| + if res := a.db.G.Exec("UPDATE presentations SET ended_at = CURRENT_TIMESTAMP WHERE sqid = ?", session); res.Error != nil { |
| + t.Fatal(res.Error) |
| + } |
| + if rec := talkRequest(a, session, "", "", nil); rec.Code != http.StatusGone { |
| + t.Errorf("ended session: status %d, want 410", rec.Code) |
| + } |
| +} |