| 1 | // qrlink.go is the follow-along code's link: POST /d/{sqid}/qr-link, |
| 2 | // which the editor calls when a follow-along code is added and when |
| 3 | // "Make an audience link" is pressed in its card. |
| 4 | // |
| 5 | // It makes a share link exactly as the share page's form does |
| 6 | // (createShareLink, share.go) and answers the link's follow address, |
| 7 | // which the browser writes into the box's props.url through its own |
| 8 | // commit — the change is the person's, one undo step, saved through |
| 9 | // the queue like any other (fixtures/qr-link.mjs). The box stores the |
| 10 | // whole address because the server keeps only the token's hash: once |
| 11 | // this answer is gone nothing can say what the token was. |
| 12 | // |
| 13 | // Making a new link leaves the old one working. The share page lists |
| 14 | // both, the code's named Audience, and revoking one there is how a code |
| 15 | // is turned off: what is drawn is decided at render (followCodes), and |
| 16 | // a revoked link draws no code anywhere. |
| 17 | package slides |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
| 22 | "net/http" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 | "time" |
| 26 | |
| 27 | "amadan.net/rastrillo/rastrillo" |
| 28 | |
| 29 | "amadan.net/oficina/slides/internal/slides/deck" |
| 30 | ) |
| 31 | |
| 32 | // qrLink serves POST /d/{sqid}/qr-link. The share page's audience, the |
| 33 | // owner's alone (mayShareDeck): a team member who edits the deck still |
| 34 | // cannot mint an unauthenticated capability for it, for the reason |
| 35 | // deckShare gives, and gets the same 404 as a stranger. |
| 36 | func (a *app) qrLink(w http.ResponseWriter, r *http.Request) { |
| 37 | u, ok := a.viewer(w, r) |
| 38 | if !ok { |
| 39 | return |
| 40 | } |
| 41 | d, ok := a.ownedDeckFor(w, r, u, a.mayShareDeck) |
| 42 | if !ok { |
| 43 | return |
| 44 | } |
| 45 | token, ok := a.createShareLink(w, r, u, d, true) |
| 46 | if !ok { |
| 47 | return |
| 48 | } |
| 49 | w.Header().Set("Content-Type", "application/json") |
| 50 | // The answer is a live capability: never kept by a cache. |
| 51 | w.Header().Set("Cache-Control", "no-store") |
| 52 | _ = json.NewEncoder(w).Encode(map[string]string{"url": a.followURL(token)}) |
| 53 | } |
| 54 | |
| 55 | // followURL is a share link's follow address: the link with ?follow=1, |
| 56 | // which follows the talk while one is live and is the deck to read |
| 57 | // when none is (share.go's sharedDeck). |
| 58 | func (a *app) followURL(token string) string { |
| 59 | return a.shareURL(token) + "?follow=1" |
| 60 | } |
| 61 | |
| 62 | // qrAlt is a follow-along code's alternative text for this request's |
| 63 | // language, {address} left for the renderer to fill |
| 64 | // (render.Options.QRAlt). Every surface that draws a slide passes it. |
| 65 | func qrAlt(r *http.Request) string { return rastrillo.T(r, "slides.qr.alt") } |
| 66 | |
| 67 | // followCodes is which follow-along codes a surface of d may draw, and |
| 68 | // with which address (render.Options.QR, layout.Options.QR): each qr |
| 69 | // block's stored url to the follow address its code is drawn for. |
| 70 | // |
| 71 | // The document takes any follow-shaped address (core can't know a |
| 72 | // deck's links), so this is the authority. A url is drawn only when it |
| 73 | // is this instance's own origin and its token is a live (unrevoked) |
| 74 | // share link of this very deck; anything else is left out, and draws |
| 75 | // no code for a reader and an empty frame in the editor. |
| 76 | // |
| 77 | // base is the surface. On a share link's surfaces ("/s/{token}": the |
| 78 | // page, its follow view and fragments, its thumbnails) the code is |
| 79 | // drawn with the reader's own link: the audience link it holds is a |
| 80 | // capability the reader was never given, and one that would outlive |
| 81 | // the revoking of theirs. Members ("/d/…": the editor, the viewer, the |
| 82 | // API, the PDF) and the presenting surfaces ("/p/…") get the stored |
| 83 | // address. |
| 84 | // |
| 85 | // A database error draws no codes: failing closed on a capability. |
| 86 | func (a *app) followCodes(ctx context.Context, d Deck, view *deck.Deck, base string) map[string]string { |
| 87 | if view == nil { |
| 88 | return nil |
| 89 | } |
| 90 | prefix := a.cfg.Origin + "/s/" |
| 91 | byHash := map[string]string{} |
| 92 | for _, s := range view.Slides { |
| 93 | for _, b := range s.Blocks { |
| 94 | if b.Type != "qr" { |
| 95 | continue |
| 96 | } |
| 97 | url, _ := b.Props["url"].(string) |
| 98 | if _, ok := deck.FollowAddress(url); !ok || !strings.HasPrefix(url, prefix) { |
| 99 | continue |
| 100 | } |
| 101 | token := strings.TrimSuffix(strings.TrimPrefix(url, prefix), "?follow=1") |
| 102 | byHash[hashShareToken(token)] = url |
| 103 | } |
| 104 | } |
| 105 | if len(byHash) == 0 { |
| 106 | return nil |
| 107 | } |
| 108 | hashes := make([]string, 0, len(byHash)) |
| 109 | for h := range byHash { |
| 110 | hashes = append(hashes, h) |
| 111 | } |
| 112 | var live []string |
| 113 | if err := a.db.G.WithContext(ctx).Model(&ShareLink{}). |
| 114 | Where("deck_id = ? AND revoked_at IS NULL AND token_hash IN ?", d.ID, hashes). |
| 115 | Pluck("token_hash", &live).Error; err != nil { |
| 116 | a.logger.Error("slides: resolve follow-along codes", "deck", d.Sqid, "err", err) |
| 117 | return nil |
| 118 | } |
| 119 | own, reader := strings.CutPrefix(base, "/s/") |
| 120 | out := make(map[string]string, len(live)) |
| 121 | for _, h := range live { |
| 122 | url := byHash[h] |
| 123 | if reader { |
| 124 | url = a.followURL(own) |
| 125 | } |
| 126 | out[byHash[h]] = url |
| 127 | } |
| 128 | return out |
| 129 | } |
| 130 | |
| 131 | // linksStamp is the part of a cached slide's tag that moves when one |
| 132 | // of d's share links is made or revoked: followCodes reads them, and |
| 133 | // neither changes the deck's seq. Rows are never deleted while the |
| 134 | // deck lives, so the two counts only grow. An error is a stamp that |
| 135 | // matches nothing cached. |
| 136 | func (a *app) linksStamp(ctx context.Context, d Deck) string { |
| 137 | var row struct{ Total, Revoked int64 } |
| 138 | if err := a.db.G.WithContext(ctx).Model(&ShareLink{}). |
| 139 | Select("COUNT(*) AS total, COUNT(revoked_at) AS revoked"). |
| 140 | Where("deck_id = ?", d.ID).Scan(&row).Error; err != nil { |
| 141 | return "-L?" + strconv.FormatInt(time.Now().UnixNano(), 36) |
| 142 | } |
| 143 | return "-L" + strconv.FormatInt(row.Total, 10) + "." + strconv.FormatInt(row.Revoked, 10) |
| 144 | } |
| 145 | |