chunks.gno
1.08 Kb · 30 lines
1package memba_notes_v1
2
3import "strconv"
4
5const MaxBodyChunkBytes = 8192
6
7func NoteMetaJSON(idHex string) string {
8 v, ok := notes.Get(idText(readID(idHex)))
9 if !ok {
10 return "null"
11 }
12 return noteJSON(v.(*note), false)
13}
14
15// Each page is tied to the complete state revision, including access/mode and
16// epoch changes. Clients concatenate binary chunks before decoding UTF8/AEAD.
17func NoteBodyChunkJSON(idHex string, expectedRevision uint64, offset, limit int) string {
18 n := find(readID(idHex))
19 if expectedRevision != n.Revision {
20 panic("notes: stale body revision")
21 }
22 if limit < 1 || limit > MaxBodyChunkBytes || offset < 0 || offset > n.BodyBytes {
23 panic("notes: body chunk bounds")
24 }
25 end := offset + limit
26 if end > n.BodyBytes {
27 end = n.BodyBytes
28 }
29 return `{"id":` + q(idText(n.ID)) + `,"state_revision":` + u(n.Revision) + `,"body_revision":` + u(n.BodyRevision) + `,"epoch":` + u(uint64(n.Epoch)) + `,"offset":` + q(strconv.Itoa(offset)) + `,"total":` + q(strconv.Itoa(n.BodyBytes)) + `,"next_offset":` + q(strconv.Itoa(end)) + `,"chunk_blob":` + blob(n.Body[offset:end]) + `}`
30}