builders.gno
6.14 Kb · 234 lines
1// Package builders is a public directory of people with proven, paid work.
2//
3// Anyone can list themselves, but only after finishing at least one paid task
4// on Gild. The proof is not copied or claimed: the directory asks the Gild
5// realm directly, at the moment you join and every time the page renders.
6// No oracle, no admin, no off-chain list. This is the point of the Gild
7// record: any realm can trust it with a single function call.
8package builders
9
10import (
11 "strconv"
12 "strings"
13 "time"
14
15 "gno.land/p/nt/avl/v0"
16 "gno.land/r/g1jw2yrfqnd4fp90qadw88pnjgpden4vgagjvpe6/gild/v0"
17)
18
19const (
20 // MinTasks is how many paid Gild tasks you need before you can join.
21 MinTasks = 1
22 MaxPitch = 160
23 MaxLink = 200
24 MaxPage = 50
25)
26
27const (
28 ErrNoProof = "builders: finish a paid task on Gild first"
29 ErrPitch = "builders: pitch must be 1 to 160 characters"
30 ErrLink = "builders: link must start with https:// and have no spaces"
31 ErrNotIn = "builders: you are not in the directory"
32)
33
34// Entry is one person in the directory. Their work stats are not stored here:
35// they are read from Gild so they are always current.
36type Entry struct {
37 Addr address
38 Pitch string
39 Link string
40 JoinedAt time.Time
41}
42
43var (
44 entries avl.Tree // address string -> *Entry
45 webPath string
46)
47
48func init(cur realm) {
49 webPath = strings.TrimPrefix(cur.PkgPath(), "gno.land")
50}
51
52// Join adds the caller to the directory, or updates their pitch and link.
53// It fails unless the caller has finished at least MinTasks paid Gild tasks.
54func Join(cur realm, pitch, link string) {
55 caller := cur.Previous().Address()
56 if !gild.HasCompleted(caller, MinTasks) {
57 panic(ErrNoProof)
58 }
59 pitch = strings.TrimSpace(pitch)
60 link = strings.TrimSpace(link)
61 if len(pitch) == 0 || len(pitch) > MaxPitch || hasControl(pitch) {
62 panic(ErrPitch)
63 }
64 if link != "" && !isLink(link) {
65 panic(ErrLink)
66 }
67 key := caller.String()
68 if v := entries.Get(key); v != nil {
69 e := v.(*Entry)
70 e.Pitch, e.Link = pitch, link
71 return
72 }
73 entries.Set(key, &Entry{Addr: caller, Pitch: pitch, Link: link, JoinedAt: time.Now()})
74}
75
76// Leave removes the caller from the directory.
77func Leave(cur realm) {
78 key := cur.Previous().Address().String()
79 if _, removed := entries.Remove(key); !removed {
80 panic(ErrNotIn)
81 }
82}
83
84// IsListed reports whether addr is in the directory.
85func IsListed(addr address) bool {
86 return entries.Has(addr.String())
87}
88
89// Size returns how many people are listed.
90func Size() int { return entries.Size() }
91
92// BuildersJSON lists entries with their live Gild stats.
93func BuildersJSON(offset, limit int) string {
94 if offset < 0 {
95 offset = 0
96 }
97 if limit <= 0 || limit > MaxPage {
98 limit = MaxPage
99 }
100 var b strings.Builder
101 b.WriteString(`{"total":`)
102 b.WriteString(strconv.Itoa(entries.Size()))
103 b.WriteString(`,"items":[`)
104 first := true
105 entries.IterateByOffset(offset, limit, func(_ string, v any) bool {
106 e := v.(*Entry)
107 if !first {
108 b.WriteByte(',')
109 }
110 first = false
111 b.WriteString(`{"address":`)
112 b.WriteString(jsonStr(e.Addr.String()))
113 b.WriteString(`,"pitch":`)
114 b.WriteString(jsonStr(e.Pitch))
115 b.WriteString(`,"link":`)
116 b.WriteString(jsonStr(e.Link))
117 b.WriteString(`,"completed":`)
118 b.WriteString(strconv.Itoa(gild.Completed(e.Addr)))
119 b.WriteString(`,"earned":`)
120 b.WriteString(strconv.FormatInt(gild.Earned(e.Addr), 10))
121 b.WriteString(`,"joinedAt":`)
122 b.WriteString(strconv.FormatInt(e.JoinedAt.Unix(), 10))
123 b.WriteByte('}')
124 return false
125 })
126 b.WriteString("]}")
127 return b.String()
128}
129
130// Render shows the directory on gno.land.
131func Render(path string) string {
132 var b strings.Builder
133 b.WriteString("# Gild Builders\n\n")
134 b.WriteString("People with proven, paid work on [Gild](/r/" + gildPath() + "). ")
135 b.WriteString("To join, finish at least ")
136 b.WriteString(strconv.Itoa(MinTasks))
137 b.WriteString(" paid task, then call `Join`. Stats below are read live from Gild.\n\n")
138 if entries.Size() == 0 {
139 b.WriteString("_Nobody has joined yet._\n")
140 return b.String()
141 }
142 b.WriteString("| Builder | Tasks done | Earned | Pitch |\n|---|---|---|---|\n")
143 entries.Iterate("", "", func(_ string, v any) bool {
144 e := v.(*Entry)
145 addr := e.Addr.String()
146 short := addr[:8] + "…" + addr[len(addr)-4:]
147 b.WriteString("| [" + short + "](" + "/r/" + gildPath() + ":u/" + addr + ") | ")
148 b.WriteString(strconv.Itoa(gild.Completed(e.Addr)))
149 b.WriteString(" | ")
150 b.WriteString(gnot(gild.Earned(e.Addr)))
151 b.WriteString(" | ")
152 b.WriteString(mdText(e.Pitch))
153 if e.Link != "" {
154 b.WriteString(" ([link](" + e.Link + "))")
155 }
156 b.WriteString(" |\n")
157 return false
158 })
159 return b.String()
160}
161
162// gildPath is the Gild realm path without the gno.land/r/ prefix, derived from
163// this realm's own namespace so it works wherever both are deployed.
164func gildPath() string {
165 p := strings.TrimPrefix(webPath, "/r/")
166 if i := strings.Index(p, "/"); i >= 0 {
167 return p[:i] + "/gild/v0"
168 }
169 return "gild/v0"
170}
171
172func hasControl(s string) bool {
173 for _, r := range s {
174 if r < 0x20 || r == 0x7f {
175 return true
176 }
177 }
178 return false
179}
180
181func isLink(s string) bool {
182 if len(s) > MaxLink || !strings.HasPrefix(s, "https://") || len(s) <= len("https://") {
183 return false
184 }
185 for _, r := range s {
186 if r <= ' ' || r == 0x7f || strings.ContainsRune("\"<>`()[]|", r) {
187 return false
188 }
189 }
190 return true
191}
192
193func gnot(u int64) string {
194 whole := u / 1_000_000
195 frac := u % 1_000_000
196 s := strconv.FormatInt(whole, 10)
197 if frac != 0 {
198 f := strconv.FormatInt(frac+1_000_000, 10)[1:]
199 s += "." + strings.TrimRight(f, "0")
200 }
201 return s + " GNOT"
202}
203
204// mdText escapes characters that would change markdown or table layout.
205func mdText(s string) string {
206 var b strings.Builder
207 for _, r := range s {
208 if strings.ContainsRune("\\`*_[]()<>#|!~", r) {
209 b.WriteByte('\\')
210 }
211 b.WriteRune(r)
212 }
213 return b.String()
214}
215
216func jsonStr(s string) string {
217 var b strings.Builder
218 b.WriteByte('"')
219 for _, r := range s {
220 switch {
221 case r == '"':
222 b.WriteString(`\"`)
223 case r == '\\':
224 b.WriteString(`\\`)
225 case r < 0x20 || r == '<' || r == '>' || r == '&' || r == 0x2028 || r == 0x2029:
226 h := strconv.FormatInt(int64(r), 16)
227 b.WriteString(`\u` + strings.Repeat("0", 4-len(h)) + h)
228 default:
229 b.WriteRune(r)
230 }
231 }
232 b.WriteByte('"')
233 return b.String()
234}