getters.gno
3.12 Kb · 75 lines
1package memba_notes_v1
2
3import (
4 "encoding/base64"
5 "gno.land/p/samcrew/avl"
6 keys "gno.land/r/samcrew/enckeys_v1"
7 "strconv"
8 "strings"
9)
10
11func q(s string) string { return strconv.Quote(s) }
12func u(n uint64) string { return q(strconv.FormatUint(n, 10)) }
13func blob(b []byte) string { return "\"" + base64.StdEncoding.EncodeToString(b) + "\"" }
14func noteJSON(n *note, withBody bool) string {
15 s := `{"id":` + q(idText(n.ID)) + `,"owner":` + q(string(n.Owner)) + `,"pending_owner":` + q(string(n.PendingOwner)) + `,"owner_generation":` + u(n.OwnerGeneration) + `,"mode":` + strconv.Itoa(int(n.Mode)) + `,"state_revision":` + u(n.Revision) + `,"title_revision":` + u(n.TitleRevision) + `,"body_revision":` + u(n.BodyRevision) + `,"epoch":` + u(uint64(n.Epoch)) + `,"body_bytes":` + q(strconv.Itoa(n.BodyBytes)) + `,"title_blob":` + blob(n.Title) + `,"commitment":` + blob(n.Commitment) + `,"deleted":` + strconv.FormatBool(n.Deleted) + `,"listed":` + strconv.FormatBool(n.Listed && !n.ModeratorHidden) + `,"owner_listed":` + strconv.FormatBool(n.Listed) + `,"moderator_hidden":` + strconv.FormatBool(n.ModeratorHidden) + `,"created_height":` + q(strconv.FormatInt(n.CreatedHeight, 10)) + `,"op_id":` + q(idText(n.Last.Operation)) + `,"executor":` + q(string(n.LastExecutor)) + `,"gov_writers":` + strconv.FormatBool(n.GovWriters) + `,"actor":` + q(string(n.Last.Actor)) + `,"height":` + q(strconv.FormatInt(n.Last.Height, 10))
16 if withBody {
17 s += `,"body_blob":` + blob(n.Body)
18 }
19 return s + `}`
20}
21func NoteJSON(idHex string) string {
22 v, ok := notes.Get(idText(readID(idHex)))
23 if !ok {
24 return "null"
25 }
26 return noteJSON(v.(*note), true)
27}
28func page(tree *avl.Tree, prefix, cursor string, limit int) string {
29 if limit < 1 || limit > MaxPage {
30 panic("notes: page limit")
31 }
32 if len(cursor) > 53 {
33 panic("notes: cursor length")
34 }
35 if cursor != "" {
36 if len(cursor) != 53 || cursor[20] != ':' {
37 panic("notes: bad cursor")
38 }
39 readID(cursor[21:])
40 for _, c := range cursor[:20] {
41 if c < '0' || c > '9' {
42 panic("notes: bad cursor")
43 }
44 }
45 }
46 end := prefix + "\xff"
47 if cursor != "" {
48 end = prefix + cursor
49 }
50 items := []string{}
51 next := ""
52 tree.ReverseIterate(prefix, end, func(key string, value interface{}) bool {
53 if cursor != "" && key == end {
54 return false
55 }
56 v, ok := notes.Get(value.(string))
57 if !ok {
58 panic("notes: corrupt index")
59 }
60 items = append(items, noteJSON(v.(*note), false))
61 next = strings.TrimPrefix(key, prefix)
62 return len(items) == limit
63 })
64 if len(items) < limit {
65 next = ""
66 }
67 return `{"items":[` + strings.Join(items, ",") + `],"next_cursor":` + q(next) + `}`
68}
69func ListByOwnerJSON(addr address, cursor string, limit int) string {
70 return page(ownerIndex, string(keys.CanonicalAddress(addr))+":", cursor, limit)
71}
72func ListPublicJSON(cursor string, limit int) string { return page(publicIndex, "", cursor, limit) }
73func ConfigJSON() string {
74 return `{"realm":` + q(RealmPath) + `,"admin":` + q(string(admin)) + `,"pending_admin":` + q(string(pendingAdmin)) + `,"treasury":` + q(string(treasury)) + `,"create_fee_ugnot":` + q(strconv.FormatInt(createFee, 10)) + `,"paused":` + strconv.FormatBool(paused) + `}`
75}