oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3import (
4 "amadan.net/oficina/home/roster"
5 "context"
6 "encoding/json"
7 "errors"
8 "gorm.io/gorm"
9 "strconv"
10
11 access "amadan.net/oficina/agent-access.git"
12)
13
14func (a *slideAgent) list(ctx context.Context, u User, limit int, cursor string) (any, error) {
15 if state, ok := ctx.Value(slideAgentKey{}).(*slideAgentContext); ok && state != nil {
16 state.list = true
17 }
18 if a.app.team != nil && u.team.state() == roster.Unknown {
19 return nil, access.Fail("unavailable", "The complete deck list cannot be verified now.")
20 }
21 if limit == 0 {
22 limit = 20
23 }
24 if limit < 1 || limit > 100 {
25 return nil, access.Fail("invalid", "Limit must be one to one hundred.")
26 }
27 q := a.app.visibleDecks(ctx, u)
28 if cursor != "" {
29 id, e := strconv.ParseInt(cursor, 10, 64)
30 if e != nil || id <= 0 || strconv.FormatInt(id, 10) != cursor {
31 return nil, access.Fail("invalid", "Use a canonical positive decimal cursor.")
32 }
33 q = q.Where("decks.id<?", id)
34 }
35 rows := []Deck{}
36 if e := q.Order("decks.id DESC").Limit(limit + 1).Find(&rows).Error; e != nil {
37 return nil, slideAgentFailure(e)
38 }
39 next := ""
40 if len(rows) > limit {
41 rows = rows[:limit]
42 next = strconv.FormatInt(rows[len(rows)-1].ID, 10)
43 }
44 out := []map[string]any{}
45 for _, d := range rows {
46 a.remember(ctx, d.ID)
47 out = append(out, map[string]any{"sqid": d.Sqid, "title": d.Title, "theme": d.Theme, "revision": strconv.FormatInt(d.Seq, 10)})
48 }
49 return slideAgentBounded(map[string]any{"decks": out, "next_cursor": next})
50}
51func (a *slideAgent) get(ctx context.Context, u User, sqid string) (any, error) {
52 var d Deck
53 if e := a.visible(ctx, a.app.db.G, u, "sqid=?", sqid, &d); e != nil {
54 return nil, slideAgentFailure(e)
55 }
56 h, _, e := a.app.openDeck(ctx, d)
57 if e != nil {
58 return nil, slideAgentFailure(e)
59 }
60 defer h.Close(ctx)
61 raw, e := h.JSON(ctx)
62 if e != nil {
63 return nil, slideAgentFailure(e)
64 }
65 a.remember(ctx, d.ID)
66 return slideAgentBounded(map[string]any{"sqid": d.Sqid, "revision": strconv.FormatInt(d.Seq, 10), "deck": json.RawMessage(raw)})
67}
68
69// visible preserves the app's roster/grant query on the caller's connection.
70// Recheck after the query: a lease may expire while SQLite reads the row.
71func (a *slideAgent) visible(ctx context.Context, db *gorm.DB, u User, predicate string, id any, d *Deck) error {
72 err := a.app.visibleDecksOn(db.WithContext(ctx), u).Where(predicate, id).Take(d).Error
73 if a.app.team == nil || u.team.state() != roster.Unknown {
74 return err
75 }
76 if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
77 return err
78 }
79 if err == nil {
80 var n int64
81 if e := db.WithContext(ctx).Model(&DeckGrant{}).Where("deck_id=? AND user_id=?", d.ID, u.ID).Count(&n).Error; e != nil {
82 return e
83 }
84 if n > 0 {
85 return nil
86 }
87 }
88 return access.Fail("unavailable", "Deck visibility cannot be verified now.")
89}
90