read.gno
7.11 Kb · 257 lines
1package gild
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 "gno.land/p/nt/avl/v0"
9 "gno.land/p/nt/seqid/v0"
10)
11
12// ---- JSON views for apps ----
13
14// TaskJSON returns one task as JSON, or "null" if it does not exist.
15func TaskJSON(id int64) string {
16 if id < 1 {
17 return "null"
18 }
19 v := tasks.Get(seqid.ID(id).Binary())
20 if v == nil {
21 return "null"
22 }
23 return taskJSON(v.(*Task))
24}
25
26// TasksJSON lists tasks newest first. filter is "open" or "all".
27func TasksJSON(filter string, offset, limit int) string {
28 switch filter {
29 case "open":
30 return pageJSON(&openTasks, offset, limit)
31 case "all":
32 return pageJSON(&tasks, offset, limit)
33 }
34 return `{"total":0,"items":[]}`
35}
36
37// PosterTasksJSON lists tasks posted by addr, newest first.
38func PosterTasksJSON(addr address, offset, limit int) string {
39 return pageJSON(sub(&byPoster, addr), offset, limit)
40}
41
42// DoerTasksJSON lists tasks taken by addr, newest first.
43func DoerTasksJSON(addr address, offset, limit int) string {
44 return pageJSON(sub(&byDoer, addr), offset, limit)
45}
46
47// InvitesJSON lists tasks offered directly to addr, newest first. It keeps
48// every offer, so check each task's status.
49func InvitesJSON(addr address, offset, limit int) string {
50 return pageJSON(sub(&byInvite, addr), offset, limit)
51}
52
53// StatsJSON returns totals for this board (v1). Add gild/v0 StatsJSON for
54// all-time numbers.
55func StatsJSON() string {
56 var b strings.Builder
57 b.WriteString(`{"open":`)
58 b.WriteString(strconv.Itoa(openTasks.Size()))
59 b.WriteString(`,"tasks":`)
60 b.WriteString(strconv.Itoa(tasks.Size()))
61 b.WriteString(`,"locked":`)
62 b.WriteString(strconv.FormatInt(lockedTotal, 10))
63 b.WriteString(`,"paid":`)
64 b.WriteString(strconv.FormatInt(paidTotal, 10))
65 b.WriteString(`,"completed":`)
66 b.WriteString(strconv.Itoa(book.Total()))
67 b.WriteString(`,"builders":`)
68 b.WriteString(strconv.Itoa(book.Size()))
69 b.WriteString(`,"denom":"` + Denom + `"}`)
70 return b.String()
71}
72
73// ProfileJSON returns the work record of addr. completed and earned count
74// both boards; posted, spent and recent cover this board only.
75func ProfileJSON(addr address) string {
76 p := book.Profile(addr)
77 var b strings.Builder
78 b.WriteString(`{"address":`)
79 b.WriteString(jsonStr(addr.String()))
80 b.WriteString(`,"completed":`)
81 b.WriteString(strconv.Itoa(Completed(addr)))
82 b.WriteString(`,"earned":`)
83 b.WriteString(strconv.FormatInt(Earned(addr), 10))
84 b.WriteString(`,"posted":`)
85 b.WriteString(strconv.Itoa(p.Posted))
86 b.WriteString(`,"spent":`)
87 b.WriteString(strconv.FormatInt(p.Spent, 10))
88 b.WriteString(`,"recent":[`)
89 for i, r := range p.Recent() {
90 if i > 0 {
91 b.WriteByte(',')
92 }
93 b.WriteString(`{"taskId":`)
94 b.WriteString(jsonStr(r.TaskID))
95 b.WriteString(`,"title":`)
96 b.WriteString(jsonStr(r.Title))
97 b.WriteString(`,"poster":`)
98 b.WriteString(jsonStr(r.Poster.String()))
99 b.WriteString(`,"doer":`)
100 b.WriteString(jsonStr(r.Doer.String()))
101 b.WriteString(`,"amount":`)
102 b.WriteString(strconv.FormatInt(r.Amount, 10))
103 b.WriteString(`,"at":`)
104 b.WriteString(strconv.FormatInt(r.At.Unix(), 10))
105 b.WriteByte('}')
106 }
107 b.WriteString("]}")
108 return b.String()
109}
110
111func sub(tree *avl.Tree, addr address) *avl.Tree {
112 if v := tree.Get(addr.String()); v != nil {
113 return v.(*avl.Tree)
114 }
115 return avl.NewTree()
116}
117
118func clampPage(offset, limit int) (int, int) {
119 if offset < 0 {
120 offset = 0
121 }
122 if limit < 1 || limit > MaxPage {
123 limit = MaxPage
124 }
125 return offset, limit
126}
127
128func pageJSON(tree *avl.Tree, offset, limit int) string {
129 offset, limit = clampPage(offset, limit)
130 var b strings.Builder
131 b.WriteString(`{"total":`)
132 b.WriteString(strconv.Itoa(tree.Size()))
133 b.WriteString(`,"items":[`)
134 first := true
135 tree.ReverseIterateByOffset(offset, limit, func(_ string, v any) bool {
136 if !first {
137 b.WriteByte(',')
138 }
139 first = false
140 b.WriteString(taskJSON(v.(*Task)))
141 return false
142 })
143 b.WriteString("]}")
144 return b.String()
145}
146
147func taskJSON(t *Task) string {
148 var b strings.Builder
149 b.WriteString(`{"id":`)
150 b.WriteString(strconv.FormatInt(int64(t.ID), 10))
151 b.WriteString(`,"title":`)
152 b.WriteString(jsonStr(t.Title))
153 b.WriteString(`,"details":`)
154 b.WriteString(jsonStr(t.Details))
155 b.WriteString(`,"done":`)
156 b.WriteString(jsonStr(t.Done))
157 b.WriteString(`,"category":`)
158 b.WriteString(jsonStr(t.Category))
159 b.WriteString(`,"poster":`)
160 b.WriteString(jsonStr(t.Poster.String()))
161 b.WriteString(`,"doer":`)
162 b.WriteString(jsonStr(t.Doer.String()))
163 b.WriteString(`,"reward":`)
164 b.WriteString(strconv.FormatInt(t.Reward, 10))
165 b.WriteString(`,"denom":"` + Denom + `","status":"`)
166 b.WriteString(t.Status.String())
167 b.WriteString(`","createdAt":`)
168 b.WriteString(unix(t.CreatedAt))
169 b.WriteString(`,"deadline":`)
170 b.WriteString(unix(t.Deadline))
171 b.WriteString(`,"takenAt":`)
172 b.WriteString(unix(t.TakenAt))
173 b.WriteString(`,"submittedAt":`)
174 b.WriteString(unix(t.SubmittedAt))
175 b.WriteString(`,"closedAt":`)
176 b.WriteString(unix(t.ClosedAt))
177 b.WriteString(`,"claimableAt":`)
178 if t.Status == StatusInReview {
179 b.WriteString(unix(t.SubmittedAt.Add(ReviewWindow)))
180 } else {
181 b.WriteString("0")
182 }
183 b.WriteString(`,"workUrl":`)
184 b.WriteString(jsonStr(t.WorkURL))
185 b.WriteString(`,"changesAsked":`)
186 b.WriteString(strconv.FormatBool(t.ChangesAsked))
187 b.WriteString(`,"changesNote":`)
188 b.WriteString(jsonStr(t.ChangesNote))
189 b.WriteString(`,"paidBy":`)
190 b.WriteString(jsonStr(t.PaidBy))
191 b.WriteString(`,"minDone":`)
192 b.WriteString(strconv.Itoa(t.MinDone))
193 b.WriteString(`,"invite":`)
194 b.WriteString(jsonStr(t.Invite.String()))
195 b.WriteByte('}')
196 return b.String()
197}
198
199func unix(t time.Time) string {
200 if t.IsZero() {
201 return "0"
202 }
203 return strconv.FormatInt(t.Unix(), 10)
204}
205
206// jsonStr returns s as a JSON string literal. It also escapes <, > and & so
207// the output is safe to place inside HTML.
208func jsonStr(s string) string {
209 const hex = "0123456789abcdef"
210 var b strings.Builder
211 b.WriteByte('"')
212 for _, r := range s {
213 switch {
214 case r == '"':
215 b.WriteString(`\"`)
216 case r == '\\':
217 b.WriteString(`\\`)
218 case r == '\n':
219 b.WriteString(`\n`)
220 case r == '\t':
221 b.WriteString(`\t`)
222 case r < 0x20 || r == '<' || r == '>' || r == '&' || r == 0x2028 || r == 0x2029:
223 b.WriteString(`\u`)
224 b.WriteByte(hex[(r>>12)&0xf])
225 b.WriteByte(hex[(r>>8)&0xf])
226 b.WriteByte(hex[(r>>4)&0xf])
227 b.WriteByte(hex[r&0xf])
228 default:
229 b.WriteRune(r)
230 }
231 }
232 b.WriteByte('"')
233 return b.String()
234}
235
236// mdText escapes characters that would turn user text into markdown links,
237// images, headings or HTML.
238func mdText(s string) string {
239 r := strings.NewReplacer(
240 `\`, `\\`, "[", `\[`, "]", `\]`, "(", `\(`, ")", `\)`,
241 "<", "<", ">", ">", "`", "\\`", "*", `\*`, "_", `\_`, "#", `\#`, "!", `\!`,
242 "|", `\|`,
243 )
244 return r.Replace(s)
245}
246
247// gnot formats an amount in ugnot as GNOT, e.g. 1500000 -> "1.5 GNOT".
248func gnot(u int64) string {
249 whole := u / 1_000_000
250 frac := u % 1_000_000
251 s := strconv.FormatInt(whole, 10)
252 if frac != 0 {
253 f := strconv.FormatInt(frac+1_000_000, 10)[1:]
254 s += "." + strings.TrimRight(f, "0")
255 }
256 return s + " GNOT"
257}