| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | access "amadan.net/oficina/agent-access.git" |
| 11 | "amadan.net/oficina/slides/internal/slides/core" |
| 12 | "amadan.net/oficina/slides/internal/slides/deck" |
| 13 | "github.com/google/uuid" |
| 14 | "gorm.io/gorm" |
| 15 | ) |
| 16 | |
| 17 | type slideAgentInitial struct { |
| 18 | Heading string `json:"heading"` |
| 19 | Body string `json:"body_markdown"` |
| 20 | Notes string `json:"notes_markdown"` |
| 21 | } |
| 22 | type slideAgentCreate struct { |
| 23 | Team string `json:"team"` |
| 24 | ClientID string `json:"client_id"` |
| 25 | Title string `json:"title"` |
| 26 | Theme string `json:"theme,omitempty"` |
| 27 | Slides []slideAgentInitial `json:"slides"` |
| 28 | } |
| 29 | |
| 30 | func (a *slideAgent) create(ctx context.Context, p access.Principal, u User, in slideAgentCreate) (any, error) { |
| 31 | if err := a.creator(u); err != nil { |
| 32 | return nil, slideAgentFailure(err) |
| 33 | } |
| 34 | id, e := uuid.Parse(in.ClientID) |
| 35 | if e != nil || id == uuid.Nil || id.String() != in.ClientID { |
| 36 | return nil, access.Fail("invalid", "Supply a canonical nonzero UUID client_id.") |
| 37 | } |
| 38 | in.Title = strings.TrimSpace(in.Title) |
| 39 | if in.Theme == "" { |
| 40 | in.Theme = defaultTheme |
| 41 | } |
| 42 | if in.Title == "" || len(in.Title) > 512 || !validTheme(in.Theme) || len(in.Slides) < 1 || len(in.Slides) > 50 { |
| 43 | return nil, access.Fail("invalid", "Supply a title, supported theme and one to fifty slides.") |
| 44 | } |
| 45 | n := 0 |
| 46 | for _, s := range in.Slides { |
| 47 | n += len(s.Heading) + len(s.Body) + len(s.Notes) |
| 48 | } |
| 49 | if n > 128<<10 { |
| 50 | return nil, access.Fail("too_large", "Initial slide text exceeds 128 KiB.") |
| 51 | } |
| 52 | coordinate := a.digest("receipt", []string{a.identityIssuer, a.issuer, p.Subject, p.Client, a.team, in.ClientID}) |
| 53 | content := a.digest("content", in) |
| 54 | var d Deck |
| 55 | var h *core.Handle |
| 56 | created := false |
| 57 | defer func() { |
| 58 | if h != nil { |
| 59 | h.Close(ctx) |
| 60 | } |
| 61 | }() |
| 62 | e = a.app.db.G.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 63 | if e := a.bind(tx); e != nil { |
| 64 | return e |
| 65 | } |
| 66 | if e := a.writer(ctx, tx, p, u); e != nil { |
| 67 | return e |
| 68 | } |
| 69 | if e := tx.Exec("INSERT INTO slides_agent_receipts(coordinate,content) VALUES(?,?) ON CONFLICT(coordinate) DO NOTHING", coordinate, content).Error; e != nil { |
| 70 | return e |
| 71 | } |
| 72 | var saved string |
| 73 | var deckID sql.NullInt64 |
| 74 | if e := tx.Raw("SELECT content,deck_id FROM slides_agent_receipts WHERE coordinate=?", coordinate).Row().Scan(&saved, &deckID); e != nil { |
| 75 | return e |
| 76 | } |
| 77 | if saved != content { |
| 78 | return access.Fail("conflict", "This client_id was already used with different deck content.") |
| 79 | } |
| 80 | if deckID.Valid { |
| 81 | if e := a.app.visibleDecksOn(tx, u).Where("decks.id=?", deckID.Int64).Take(&d).Error; e != nil { |
| 82 | return e |
| 83 | } |
| 84 | return a.writer(ctx, tx, p, u) |
| 85 | } |
| 86 | var e error |
| 87 | d, e = createDeckRowOn(ctx, tx, u, in.Title, in.Theme) |
| 88 | if e != nil { |
| 89 | return e |
| 90 | } |
| 91 | h, e = a.app.core.NewDoc(ctx, d.Sqid, serverClientID) |
| 92 | if e != nil { |
| 93 | return e |
| 94 | } |
| 95 | ops, e := slideAgentOps(in) |
| 96 | if e != nil { |
| 97 | return e |
| 98 | } |
| 99 | actor, e := a.actor(tx, p, u) |
| 100 | if e != nil { |
| 101 | return e |
| 102 | } |
| 103 | if e = applyOpsOn(ctx, tx, h, &d, actor, ops, nil); e != nil { |
| 104 | return e |
| 105 | } |
| 106 | if e = h.Check(ctx); e != nil { |
| 107 | return e |
| 108 | } |
| 109 | raw, e := h.JSON(ctx) |
| 110 | if e != nil { |
| 111 | return e |
| 112 | } |
| 113 | if _, e = deck.Parse(raw); e != nil { |
| 114 | return e |
| 115 | } |
| 116 | done := tx.Exec("UPDATE slides_agent_receipts SET deck_id=? WHERE coordinate=? AND deck_id IS NULL", d.ID, coordinate) |
| 117 | if done.Error != nil { |
| 118 | return done.Error |
| 119 | } |
| 120 | if done.RowsAffected != 1 { |
| 121 | return access.Fail("unavailable", "The creation receipt could not be completed.") |
| 122 | } |
| 123 | if e = a.writer(ctx, tx, p, u); e != nil { |
| 124 | return e |
| 125 | } |
| 126 | d.Seq++ |
| 127 | created = true |
| 128 | return nil |
| 129 | }) |
| 130 | if e != nil { |
| 131 | return nil, slideAgentFailure(e) |
| 132 | } |
| 133 | if created { |
| 134 | a.app.live.PublishSeq(d.Sqid, d.Seq) |
| 135 | a.app.maybeSnapshot(ctx, d, h, d.Seq) |
| 136 | } |
| 137 | a.remember(ctx, d.ID) |
| 138 | return map[string]any{"sqid": d.Sqid, "url": a.app.cfg.Origin + "/d/" + d.Sqid, "revision": strconv.FormatInt(d.Seq, 10)}, nil |
| 139 | } |
| 140 | func slideAgentOps(in slideAgentCreate) ([]byte, error) { |
| 141 | ops := []op{{"op": "set_meta", "title": in.Title, "theme": in.Theme}} |
| 142 | for _, s := range in.Slides { |
| 143 | id, blocks, e := slideIDs(2) |
| 144 | if e != nil { |
| 145 | return nil, e |
| 146 | } |
| 147 | ops = append(ops, addSlide(id, "title-body"), addBlock(id, blocks[0], "heading", "heading", s.Heading, map[string]any{"level": 1}), addBlock(id, blocks[1], "text", "body", s.Body, nil), op{"op": "set_notes", "id": id, "markdown": s.Notes}) |
| 148 | } |
| 149 | return json.Marshal(ops) |
| 150 | } |
| 151 | |