| 1 | // presenting_position.go is the server's half of an absolute position |
| 2 | // (spec S13 and S18): a session that is Positional holds the talk as |
| 3 | // {slide id, step, mode, black}, and every press that reaches |
| 4 | // POST /p/{session}/command is applied to it in the order it arrives. |
| 5 | // |
| 6 | // Ordering is the point. A laptop and a phone that both press next from |
| 7 | // step 2 must reach step 4, so a press is never "go to step 3": it is a |
| 8 | // move, applied inside one transaction to whatever the row says now |
| 9 | // (deck.Advance). A window that did not hear its answer sends the same |
| 10 | // press again, so each carries an id and an id already applied answers |
| 11 | // the position without moving it. |
| 12 | // |
| 13 | // Sessions begun before this existed have Positional false and keep |
| 14 | // presentation.go's index path, untouched, for their remaining hours. |
| 15 | package slides |
| 16 | |
| 17 | import ( |
| 18 | "crypto/rand" |
| 19 | "errors" |
| 20 | "net/http" |
| 21 | "net/url" |
| 22 | "regexp" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 | "time" |
| 26 | |
| 27 | "amadan.net/oficina/slides/internal/slides/deck" |
| 28 | "amadan.net/rastrillo/rastrillo" |
| 29 | "gorm.io/gorm" |
| 30 | ) |
| 31 | |
| 32 | // pressIDPattern is what a press id may be: base64url, from 16 |
| 33 | // characters (12 random bytes) to 43 (32). Anything else is refused |
| 34 | // rather than stored, so the table holds nothing a caller made up in a |
| 35 | // shape nobody expected. |
| 36 | var pressIDPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{16,43}$`) |
| 37 | |
| 38 | // keptPresses is how many press ids a session remembers. A press is |
| 39 | // only ever resent by the window that sent it, within seconds, so the |
| 40 | // newest 64 cover any burst a clicker can make. |
| 41 | const keptPresses = 64 |
| 42 | |
| 43 | // pressCommand is one press as the command form carries it. Link and |
| 44 | // N are a presenting window's: its link's id and the command's number |
| 45 | // in it (PresentationLink). AtBlack is resume's black screen, nil when |
| 46 | // the form did not say. |
| 47 | type pressCommand struct { |
| 48 | Op, Slide, Press, AtSlide, AtMode string |
| 49 | AtStep int |
| 50 | AtBlack *bool |
| 51 | Link string |
| 52 | N int64 |
| 53 | } |
| 54 | |
| 55 | // positionAnswer is what a press answers when JSON was asked for: the |
| 56 | // position after it ("" for the slide of an empty talk), the session's |
| 57 | // sequence, and the deck's own (the same seq apiGetDeck serves), so a |
| 58 | // window learns from any answer that the deck changed under it; and |
| 59 | // when the talk's timer started (Unix ms, null until the first press), |
| 60 | // which every window shows whoever pressed. |
| 61 | type positionAnswer struct { |
| 62 | Slide string `json:"slide"` |
| 63 | Step int `json:"step"` |
| 64 | Mode string `json:"mode"` |
| 65 | Black bool `json:"black"` |
| 66 | Seq int64 `json:"seq"` |
| 67 | DeckSeq int64 `json:"deckSeq"` |
| 68 | TimerAt *int64 `json:"timerAt"` |
| 69 | // number is the slide's 1-based number in the full talk, which a |
| 70 | // scriptless press's redirect names (pressLanding). Not on the wire. |
| 71 | number int |
| 72 | } |
| 73 | |
| 74 | // mayDrive is who may move a session: whoever holds the remote token, |
| 75 | // or the presenter themselves, signed in and still admitted (S19; |
| 76 | // admittedPresenter). The cookie being enough is why the app-wide origin |
| 77 | // check (app.go's csrf.Protect) matters here: another site cannot make |
| 78 | // the presenter's browser press. A session begun before positions is |
| 79 | // driven the same way, by its index (presentCommand). |
| 80 | // |
| 81 | // It answers for the row the request resolved, and says the actor it |
| 82 | // let in by the cookie ("" when the token did it). applyPress asks |
| 83 | // again, of the row its transaction read (drivesRow), because the token |
| 84 | // can be replaced in between. answered is admittedPresenter's: the |
| 85 | // request has been answered, and the caller writes nothing more. |
| 86 | func (a *app) mayDrive(w http.ResponseWriter, r *http.Request, p Presentation, token string) (drives bool, actor string, answered bool) { |
| 87 | if remoteTokenOK(p, token) { |
| 88 | return true, "", false |
| 89 | } |
| 90 | if p.Actor == "" { |
| 91 | return false, "", false |
| 92 | } |
| 93 | actor, answered = a.admittedPresenter(w, r) |
| 94 | if answered || actor != p.Actor { |
| 95 | return false, "", answered |
| 96 | } |
| 97 | return true, actor, false |
| 98 | } |
| 99 | |
| 100 | // errNotDriver is a press whose credential no longer drives the session |
| 101 | // by the time its transaction reads the row: the token was replaced |
| 102 | // between the request being let in and the write. |
| 103 | var errNotDriver = errors.New("slides: not entitled to drive this presentation") |
| 104 | |
| 105 | // drivesRow is mayDrive against a row already read, with the signed-in |
| 106 | // actor already looked up: the check applyPress makes inside its |
| 107 | // transaction. |
| 108 | func drivesRow(p Presentation, token, actor string) bool { |
| 109 | return remoteTokenOK(p, token) || (actor != "" && actor == p.Actor) |
| 110 | } |
| 111 | |
| 112 | // pressToken is the remote token a press carries: the form's field, |
| 113 | // or a bearer for a caller that is not a browser. |
| 114 | func pressToken(r *http.Request) string { |
| 115 | if token := r.PostFormValue("t"); token != "" { |
| 116 | return token |
| 117 | } |
| 118 | return bearerToken(r) |
| 119 | } |
| 120 | |
| 121 | // newPressID is a press id for a rendered form: 26 base32 characters, |
| 122 | // inside pressIDPattern. |
| 123 | func newPressID() string { return rand.Text() } |
| 124 | |
| 125 | // withPressIDs gives each button a fresh press id. |
| 126 | func withPressIDs(buttons []commandButton) []commandButton { |
| 127 | for i := range buttons { |
| 128 | buttons[i].Press = newPressID() |
| 129 | } |
| 130 | return buttons |
| 131 | } |
| 132 | |
| 133 | // presentPress is presentCommand for a Positional session. |
| 134 | func (a *app) presentPress(w http.ResponseWriter, r *http.Request, p Presentation) { |
| 135 | token := pressToken(r) |
| 136 | drives, actor, answered := a.mayDrive(w, r, p, token) |
| 137 | if answered { |
| 138 | return |
| 139 | } |
| 140 | if !drives { |
| 141 | a.notFound(w, r) |
| 142 | return |
| 143 | } |
| 144 | cmd, err := pressCommandFrom(r) |
| 145 | if err != nil { |
| 146 | a.errorPage(w, r, http.StatusUnprocessableEntity, kindStatus, "") |
| 147 | return |
| 148 | } |
| 149 | |
| 150 | answer, err := a.applyPress(r, p, cmd, actor) |
| 151 | switch { |
| 152 | case err == nil: |
| 153 | case errors.Is(err, errNotDriver): |
| 154 | a.notFound(w, r) |
| 155 | return |
| 156 | case errors.Is(err, errPresentationEnded): |
| 157 | a.gone(w, r) |
| 158 | return |
| 159 | case errors.Is(err, errBadCommand): |
| 160 | a.errorPage(w, r, http.StatusUnprocessableEntity, kindStatus, "") |
| 161 | return |
| 162 | default: |
| 163 | a.fail(w, r, "apply a press", err) |
| 164 | return |
| 165 | } |
| 166 | |
| 167 | // The actor is what authenticated, as presentCommand says: the |
| 168 | // remote's capability, or the presenter's own session. |
| 169 | if actor == "" { |
| 170 | actor = remoteActor(p) |
| 171 | } |
| 172 | a.logger.Info("slides: presentation press", |
| 173 | "session", p.Sqid, "op", cmd.Op, "slide", answer.Slide, "step", answer.Step, |
| 174 | "seq", answer.Seq, "actor", actor, "issuer", p.Actor) |
| 175 | |
| 176 | if strings.Contains(r.Header.Get("Accept"), "application/json") { |
| 177 | a.writeJSON(w, http.StatusOK, answer) |
| 178 | return |
| 179 | } |
| 180 | if ref := r.Header.Get("Referer"); ref != "" && a.sameOrigin(ref) { |
| 181 | http.Redirect(w, r, pressLanding(ref, answer), http.StatusSeeOther) |
| 182 | return |
| 183 | } |
| 184 | w.WriteHeader(http.StatusNoContent) |
| 185 | } |
| 186 | |
| 187 | // pressLanding is where a form's press sends the browser back to: the |
| 188 | // page it came from, saying the slide and step the talk is now at, so a |
| 189 | // projector with scripts off draws that state (stepPlace) and a press |
| 190 | // resubmitted lands on the same page as the first. A press made from the |
| 191 | // presenter window's picker (?pick=1) lands on the window itself. ref is |
| 192 | // already known to be this app's own. |
| 193 | func pressLanding(ref string, at positionAnswer) string { |
| 194 | u, err := url.Parse(ref) |
| 195 | if err != nil { |
| 196 | return ref |
| 197 | } |
| 198 | q := u.Query() |
| 199 | // A press from the presenter window's picker lands on the window. |
| 200 | q.Del("pick") |
| 201 | q.Set("slide", strconv.Itoa(at.number)) |
| 202 | q.Set("step", strconv.Itoa(at.Step)) |
| 203 | u.RawQuery = q.Encode() |
| 204 | return u.String() |
| 205 | } |
| 206 | |
| 207 | // place is a slide of the reader deck, the step it is drawn at, and |
| 208 | // whether that is where the session is. |
| 209 | type place struct { |
| 210 | index, step int |
| 211 | live bool |
| 212 | } |
| 213 | |
| 214 | // stepPlace is what a surface of a session with a position draws. |
| 215 | // Without ?slide it is the session's own place. ?slide=N counts from 1 |
| 216 | // through the full talk (the reader deck, and talkSlide.Number), and |
| 217 | // is read as ?slide= always was: a number outside the talk, or not a |
| 218 | // number, is ok false, the 404 (a parked slide's old place must reach |
| 219 | // nobody). ?step=k clamps into the slide's presses; a step that is not |
| 220 | // a number is the same 404. A slide named without a step is drawn at |
| 221 | // the session's step when it is the session's slide, and finished |
| 222 | // otherwise. |
| 223 | func stepPlace(view *deck.Deck, p Presentation, q url.Values) (place, bool) { |
| 224 | cur, curStep, _ := sessionPlace(view, p) |
| 225 | raw := q.Get("slide") |
| 226 | if raw == "" { |
| 227 | return place{index: cur, step: curStep, live: true}, true |
| 228 | } |
| 229 | index, ok := slideIndex(raw, len(view.Slides)) |
| 230 | if !ok { |
| 231 | return place{}, false |
| 232 | } |
| 233 | presses := len(deck.Presses(view.Slides[index], view.Scenes)) |
| 234 | step := presses |
| 235 | if rawStep := q.Get("step"); rawStep != "" { |
| 236 | k, err := strconv.Atoi(rawStep) |
| 237 | if err != nil { |
| 238 | return place{}, false |
| 239 | } |
| 240 | step = min(max(k, 0), presses) |
| 241 | } else if index == cur { |
| 242 | step = curStep |
| 243 | } |
| 244 | return place{index: index, step: step, live: index == cur && step == curStep}, true |
| 245 | } |
| 246 | |
| 247 | // sessionPlace is the session's position in the reader deck: the index |
| 248 | // of its slide, the step (settled into the deck as it is now, as a |
| 249 | // press would settle it), and how many presses the slide has. |
| 250 | func sessionPlace(view *deck.Deck, p Presentation) (index, step, presses int) { |
| 251 | pos := deck.SettleNear(view, positionOf(p), p.Slide) |
| 252 | for i, s := range view.Slides { |
| 253 | if s.ID == pos.Slide { |
| 254 | return i, pos.Step, len(deck.Presses(s, view.Scenes)) |
| 255 | } |
| 256 | } |
| 257 | index = clampSlide(p.Slide, len(view.Slides)) |
| 258 | return index, 0, len(deck.Presses(view.Slides[index], view.Scenes)) |
| 259 | } |
| 260 | |
| 261 | // talkPlace is the counter's "n / total" for the slide at index: its |
| 262 | // place in the talk being given, in mode. A slide outside that talk (a |
| 263 | // full-only slide shown in the short version) takes the place of the |
| 264 | // slide before it. A talk with nothing in it counts the reader deck. |
| 265 | func talkPlace(view *deck.Deck, mode deck.Mode, index int) (int, int) { |
| 266 | order := deck.TalkOrder(view, mode) |
| 267 | if len(order) == 0 { |
| 268 | return index + 1, len(view.Slides) |
| 269 | } |
| 270 | n := 1 |
| 271 | for k, i := range order { |
| 272 | if i <= index { |
| 273 | n = k + 1 |
| 274 | } |
| 275 | } |
| 276 | return n, len(order) |
| 277 | } |
| 278 | |
| 279 | // nextInTalk is the reader-deck index of the slide after index in the |
| 280 | // talk being given, in mode, or -1 at its end: what "Coming next" |
| 281 | // previews, so the short version never previews a full-only slide. |
| 282 | func nextInTalk(view *deck.Deck, mode deck.Mode, index int) int { |
| 283 | for _, i := range deck.TalkOrder(view, mode) { |
| 284 | if i > index { |
| 285 | return i |
| 286 | } |
| 287 | } |
| 288 | return -1 |
| 289 | } |
| 290 | |
| 291 | // pressLabels are the back and forward buttons' words: a press may be |
| 292 | // a step rather than a slide on a session with a position, so there |
| 293 | // they say Back and Next; a session begun before keeps its own. |
| 294 | func pressLabels(r *http.Request, p Presentation) (back, next string) { |
| 295 | if p.Positional { |
| 296 | return rastrillo.T(r, "slides.present.press.back"), rastrillo.T(r, "slides.present.press.next") |
| 297 | } |
| 298 | return rastrillo.T(r, "slides.present.prev"), rastrillo.T(r, "slides.present.next") |
| 299 | } |
| 300 | |
| 301 | // pressCommandFrom reads the form. A missing or malformed press id is |
| 302 | // errBadCommand: without one a press resent cannot be told from a |
| 303 | // second press. So is a step or a mode that is present but malformed; |
| 304 | // the op itself is checked when it is applied. |
| 305 | func pressCommandFrom(r *http.Request) (pressCommand, error) { |
| 306 | cmd := pressCommand{ |
| 307 | Op: r.PostFormValue("op"), |
| 308 | Slide: r.PostFormValue("slide"), |
| 309 | Press: r.PostFormValue("press"), |
| 310 | AtSlide: r.PostFormValue("at_slide"), |
| 311 | AtMode: r.PostFormValue("at_mode"), |
| 312 | } |
| 313 | if !pressIDPattern.MatchString(cmd.Press) { |
| 314 | return pressCommand{}, errBadCommand |
| 315 | } |
| 316 | if raw := r.PostFormValue("at_step"); raw != "" { |
| 317 | n, err := strconv.Atoi(raw) |
| 318 | if err != nil { |
| 319 | return pressCommand{}, errBadCommand |
| 320 | } |
| 321 | cmd.AtStep = n |
| 322 | } |
| 323 | switch deck.Mode(cmd.AtMode) { |
| 324 | case "", deck.ModeCore, deck.ModeFull: |
| 325 | default: |
| 326 | return pressCommand{}, errBadCommand |
| 327 | } |
| 328 | switch raw := r.PostFormValue("at_black"); raw { |
| 329 | case "": |
| 330 | case "true", "false": |
| 331 | black := raw == "true" |
| 332 | cmd.AtBlack = &black |
| 333 | default: |
| 334 | return pressCommand{}, errBadCommand |
| 335 | } |
| 336 | // A link's command is numbered, from 1; a number without a link |
| 337 | // means nothing and is ignored. |
| 338 | if cmd.Link = r.PostFormValue("link"); cmd.Link != "" { |
| 339 | if !pressIDPattern.MatchString(cmd.Link) { |
| 340 | return pressCommand{}, errBadCommand |
| 341 | } |
| 342 | n, err := strconv.ParseInt(r.PostFormValue("n"), 10, 64) |
| 343 | if err != nil || n < 1 { |
| 344 | return pressCommand{}, errBadCommand |
| 345 | } |
| 346 | cmd.N = n |
| 347 | } |
| 348 | return cmd, nil |
| 349 | } |
| 350 | |
| 351 | // applyPress applies one press to the session, in one transaction, and |
| 352 | // tells everybody watching. |
| 353 | // |
| 354 | // The row is locked by the transaction itself. This is SQLite |
| 355 | // (rastrillo/db), which has no SELECT … FOR UPDATE, and rastrillo's |
| 356 | // write pool is a single connection: a transaction holds it from its |
| 357 | // first statement to its commit, so two presses cannot interleave, and |
| 358 | // the second reads the row the first wrote. The deck is read on the |
| 359 | // same connection, so the position and deckSeq agree with each other. |
| 360 | // |
| 361 | // The request's credential is checked again against the row the |
| 362 | // transaction read, before anything else: a token replaced after the |
| 363 | // request was let in drives nothing (errNotDriver). actor is the |
| 364 | // presenter mayDrive admitted by the cookie, or "": who is signed in |
| 365 | // does not change with the row, only the token can. |
| 366 | func (a *app) applyPress(r *http.Request, p Presentation, cmd pressCommand, actor string) (positionAnswer, error) { |
| 367 | ctx := r.Context() |
| 368 | now := time.Now().UTC() |
| 369 | token := pressToken(r) |
| 370 | var updated Presentation |
| 371 | var deckSeq int64 |
| 372 | moved := false |
| 373 | err := a.db.G.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 374 | var cur Presentation |
| 375 | if err := tx.Where("id = ? AND ended_at IS NULL", p.ID).Take(&cur).Error; err != nil { |
| 376 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 377 | return errPresentationEnded |
| 378 | } |
| 379 | return err |
| 380 | } |
| 381 | if !drivesRow(cur, token, actor) { |
| 382 | return errNotDriver |
| 383 | } |
| 384 | var d Deck |
| 385 | if err := tx.Take(&d, cur.DeckID).Error; err != nil { |
| 386 | return err |
| 387 | } |
| 388 | deckSeq = d.Seq |
| 389 | |
| 390 | var seen int64 |
| 391 | if err := tx.Model(&PresentationPress{}). |
| 392 | Where("presentation_id = ? AND press_id = ?", cur.ID, cmd.Press). |
| 393 | Count(&seen).Error; err != nil { |
| 394 | return err |
| 395 | } |
| 396 | if seen > 0 { |
| 397 | // Sent twice: where the talk is, and nothing else. |
| 398 | updated = cur |
| 399 | return nil |
| 400 | } |
| 401 | if cmd.Link != "" { |
| 402 | var newest PresentationLink |
| 403 | err := tx.Where("presentation_id = ? AND link_id = ?", cur.ID, cmd.Link).Take(&newest).Error |
| 404 | switch { |
| 405 | case err == nil && cmd.N <= newest.N: |
| 406 | // Older than a command this link has had applied: the |
| 407 | // window gave up on it, and has said since where it is. |
| 408 | updated = cur |
| 409 | return nil |
| 410 | case err != nil && !errors.Is(err, gorm.ErrRecordNotFound): |
| 411 | return err |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | h, view, err := a.openDeckOn(ctx, tx, d) |
| 416 | if err != nil { |
| 417 | return err |
| 418 | } |
| 419 | h.Close(ctx) |
| 420 | changes, err := pressChanges(view, cur, cmd, now) |
| 421 | if err != nil { |
| 422 | return err |
| 423 | } |
| 424 | // A map, as presentCommand's: only the columns named here. |
| 425 | res := tx.Model(&Presentation{}). |
| 426 | Where("id = ? AND ended_at IS NULL", cur.ID).Updates(changes) |
| 427 | if res.Error != nil { |
| 428 | return res.Error |
| 429 | } |
| 430 | if res.RowsAffected == 0 { |
| 431 | return errPresentationEnded |
| 432 | } |
| 433 | if err := rememberPress(tx, cur.ID, cmd.Press, now); err != nil { |
| 434 | return err |
| 435 | } |
| 436 | if cmd.Link != "" { |
| 437 | if err := rememberLink(tx, cur.ID, cmd.Link, cmd.N, now); err != nil { |
| 438 | return err |
| 439 | } |
| 440 | } |
| 441 | moved = true |
| 442 | return tx.Take(&updated, cur.ID).Error |
| 443 | }) |
| 444 | if err != nil { |
| 445 | return positionAnswer{}, err |
| 446 | } |
| 447 | if moved { |
| 448 | a.hub.Publish(p.Sqid, eventFor(updated)) |
| 449 | } |
| 450 | return positionAnswer{ |
| 451 | Slide: updated.SlideID, |
| 452 | Step: updated.Step, |
| 453 | Mode: updated.Mode, |
| 454 | Black: updated.Blackout, |
| 455 | Seq: updated.Seq, |
| 456 | DeckSeq: deckSeq, |
| 457 | TimerAt: timerMillis(updated.TimerAt), |
| 458 | number: updated.Slide + 1, |
| 459 | }, nil |
| 460 | } |
| 461 | |
| 462 | // rememberPress records a press id and forgets all but the session's |
| 463 | // newest keptPresses. Newest by rowid: the table has no key of its own, |
| 464 | // and SQLite hands out rowids in insertion order while the largest is |
| 465 | // never deleted, which the trim guarantees. |
| 466 | func rememberPress(tx *gorm.DB, presentationID int64, press string, now time.Time) error { |
| 467 | if err := tx.Create(&PresentationPress{ |
| 468 | PresentationID: presentationID, |
| 469 | PressID: press, |
| 470 | CreatedAt: now, |
| 471 | }).Error; err != nil { |
| 472 | return err |
| 473 | } |
| 474 | return tx.Exec(`DELETE FROM presentation_presses |
| 475 | WHERE presentation_id = ? AND rowid NOT IN ( |
| 476 | SELECT rowid FROM presentation_presses |
| 477 | WHERE presentation_id = ? ORDER BY rowid DESC LIMIT ?)`, |
| 478 | presentationID, presentationID, keptPresses).Error |
| 479 | } |
| 480 | |
| 481 | // rememberLink records the newest command a link has had applied. Every |
| 482 | // link is kept for the life of the session (at most twelve hours, one |
| 483 | // link per window load): a link forgotten would let its abandoned |
| 484 | // presses apply again. They go with the session's row (deleteDeckTx). |
| 485 | func rememberLink(tx *gorm.DB, presentationID int64, link string, n int64, now time.Time) error { |
| 486 | res := tx.Model(&PresentationLink{}). |
| 487 | Where("presentation_id = ? AND link_id = ?", presentationID, link). |
| 488 | Updates(map[string]any{"n": n, "updated_at": now}) |
| 489 | if res.Error != nil || res.RowsAffected > 0 { |
| 490 | return res.Error |
| 491 | } |
| 492 | return tx.Create(&PresentationLink{PresentationID: presentationID, LinkID: link, N: n, UpdatedAt: now}).Error |
| 493 | } |
| 494 | |
| 495 | // moveOps are the presses that move the talk, and so start its timer. |
| 496 | var moveOps = map[string]string{ |
| 497 | "next": "next", |
| 498 | "prev": "back", |
| 499 | "first": "first", |
| 500 | "last": "last", |
| 501 | "goto": "goto", |
| 502 | } |
| 503 | |
| 504 | // pressChanges is the whole decision a press makes, as the columns it |
| 505 | // writes: settle the stored position into the deck as it is now, then |
| 506 | // apply the move. |
| 507 | func pressChanges(view *deck.Deck, cur Presentation, cmd pressCommand, now time.Time) (map[string]any, error) { |
| 508 | changes := map[string]any{"seq": cur.Seq + 1} |
| 509 | if cmd.Op == "end" { |
| 510 | changes["ended_at"] = now |
| 511 | return changes, nil |
| 512 | } |
| 513 | |
| 514 | // The stored slide number is the hint for a slide deleted since: |
| 515 | // the talk lands on the one that followed it (deck.SettleNear). |
| 516 | pos := deck.SettleNear(view, positionOf(cur), cur.Slide) |
| 517 | move, isMove := moveOps[cmd.Op] |
| 518 | switch { |
| 519 | case isMove: |
| 520 | mv := deck.Move{Op: move} |
| 521 | if cmd.Op == "goto" { |
| 522 | id, err := gotoTarget(view, cmd.Slide) |
| 523 | if err != nil { |
| 524 | return nil, err |
| 525 | } |
| 526 | mv.Slide = id |
| 527 | } |
| 528 | pos = deck.Advance(view, pos, mv) |
| 529 | case cmd.Op == "blackout": |
| 530 | pos = deck.Advance(view, pos, deck.Move{Op: "black"}) |
| 531 | case cmd.Op == "mode": |
| 532 | pos = deck.Advance(view, pos, deck.Move{Op: "mode"}) |
| 533 | case cmd.Op == "resume": |
| 534 | // The window that moved offline wins (S18): its position, |
| 535 | // black screen included, settled into the deck as the server |
| 536 | // has it. |
| 537 | mode := deck.Mode(cmd.AtMode) |
| 538 | if mode == "" { |
| 539 | mode = pos.Mode |
| 540 | } |
| 541 | black := pos.Black |
| 542 | if cmd.AtBlack != nil { |
| 543 | black = *cmd.AtBlack |
| 544 | } |
| 545 | pos = deck.Settle(view, deck.Position{Slide: cmd.AtSlide, Step: cmd.AtStep, Mode: mode, Black: black}) |
| 546 | isMove = true |
| 547 | case cmd.Op == "timer-reset": |
| 548 | changes["timer_at"] = nil |
| 549 | default: |
| 550 | return nil, errBadCommand |
| 551 | } |
| 552 | if isMove && cur.TimerAt == nil { |
| 553 | changes["timer_at"] = now |
| 554 | } |
| 555 | changes["slide_id"] = pos.Slide |
| 556 | changes["step"] = pos.Step |
| 557 | changes["mode"] = string(pos.Mode) |
| 558 | changes["blackout"] = pos.Black |
| 559 | changes["slide"] = fullIndex(view, pos.Slide) |
| 560 | return changes, nil |
| 561 | } |
| 562 | |
| 563 | // positionOf is the row's position. |
| 564 | func positionOf(p Presentation) deck.Position { |
| 565 | mode := deck.Mode(p.Mode) |
| 566 | if mode != deck.ModeFull { |
| 567 | mode = deck.ModeCore |
| 568 | } |
| 569 | return deck.Position{Slide: p.SlideID, Step: p.Step, Mode: mode, Black: p.Blackout} |
| 570 | } |
| 571 | |
| 572 | // gotoTarget is the slide a goto names: a slide's id, or its 1-based |
| 573 | // number in the full talk, which is what a scriptless form sends. An id |
| 574 | // the deck no longer has passes through, and deck.Advance leaves the |
| 575 | // talk where it is; a number outside the talk is refused, as it always |
| 576 | // was. |
| 577 | func gotoTarget(view *deck.Deck, slide string) (string, error) { |
| 578 | if slide == "" { |
| 579 | return "", errBadCommand |
| 580 | } |
| 581 | for _, s := range view.Slides { |
| 582 | if s.ID == slide { |
| 583 | return slide, nil |
| 584 | } |
| 585 | } |
| 586 | if n, err := strconv.Atoi(slide); err == nil { |
| 587 | full := deck.TalkOrder(view, deck.ModeFull) |
| 588 | if n < 1 || n > len(full) { |
| 589 | return "", errBadCommand |
| 590 | } |
| 591 | return view.Slides[full[n-1]].ID, nil |
| 592 | } |
| 593 | return slide, nil |
| 594 | } |
| 595 | |
| 596 | // fullIndex is the 0-based place of the slide with this id in the full |
| 597 | // talk: the value Presentation.Slide holds, so that slide number N means |
| 598 | // the same slide to every page. 0 for nowhere (an empty talk). |
| 599 | func fullIndex(view *deck.Deck, id string) int { |
| 600 | for n, i := range deck.TalkOrder(view, deck.ModeFull) { |
| 601 | if view.Slides[i].ID == id { |
| 602 | return n |
| 603 | } |
| 604 | } |
| 605 | return 0 |
| 606 | } |
| 607 | |
| 608 | // startPosition is where a new session begins: deck.Start, with the |
| 609 | // stored slide number in step with it. |
| 610 | func startPosition(view *deck.Deck) (deck.Position, int) { |
| 611 | start := deck.Start(view) |
| 612 | return start, fullIndex(view, start.Slide) |
| 613 | } |
| 614 | |