builders.gno
11.61 Kb · 424 lines
1// Package builders is a public directory of people with proven, paid work.
2//
3// v1 adds full profiles: a display name, a headline, a short bio, skills,
4// links and an "open to work" switch. Each builder edits their own profile
5// and nobody else can. Work stats are never stored here: they are read from
6// the Gild realm every time, so they cannot be edited or faked.
7//
8// Anyone can list themselves, but only after finishing at least one paid
9// task on Gild. No oracle, no admin, no off-chain list.
10package builders
11
12import (
13 "chain"
14 "strconv"
15 "strings"
16 "time"
17
18 "gno.land/p/nt/avl/v0"
19 "gno.land/r/g1jw2yrfqnd4fp90qadw88pnjgpden4vgagjvpe6/gild/v0"
20)
21
22const (
23 // MinTasks is how many paid Gild tasks you need before you can join.
24 MinTasks = 1
25 MaxName = 40
26 MaxHeadline = 160
27 MaxBio = 600
28 MaxSkills = 6
29 MaxSkill = 24
30 MaxLink = 200
31 MaxPage = 50
32)
33
34const (
35 ErrNoProof = "builders: finish a paid task on Gild first"
36 ErrName = "builders: name must be 1 to 40 characters"
37 ErrHeadline = "builders: headline must be 1 to 160 characters"
38 ErrBio = "builders: bio must be at most 600 characters"
39 ErrSkills = "builders: use up to 6 skills of letters, numbers, spaces, + # . or -"
40 ErrWebsite = "builders: website must start with https:// and have no spaces"
41 ErrGitHub = "builders: GitHub must be a username, like octocat"
42 ErrX = "builders: X must be a handle, like gnoland"
43 ErrNotIn = "builders: you are not in the directory"
44)
45
46// Entry is one person in the directory. It holds only what the builder
47// wrote about themselves; their Gild stats are read live.
48type Entry struct {
49 Addr address
50 Name string
51 Headline string
52 Bio string
53 Skills []string
54 Website string
55 GitHub string
56 X string
57 Open bool
58 JoinedAt time.Time
59 UpdatedAt time.Time
60}
61
62var (
63 entries avl.Tree // address string -> *Entry
64 webPath string
65)
66
67func init(cur realm) {
68 webPath = strings.TrimPrefix(cur.PkgPath(), "gno.land")
69}
70
71// SetProfile creates or updates the caller's profile. Skills is a
72// comma-separated list. Empty links are allowed. It fails unless the caller
73// has finished at least MinTasks paid Gild tasks.
74func SetProfile(cur realm, name, headline, bio, skills, website, github, x string, open bool) {
75 caller := cur.Previous().Address()
76 if !gild.HasCompleted(caller, MinTasks) {
77 panic(ErrNoProof)
78 }
79 name = strings.TrimSpace(name)
80 headline = strings.TrimSpace(headline)
81 bio = strings.TrimSpace(strings.ReplaceAll(bio, "\r\n", "\n"))
82 website = strings.TrimSpace(website)
83 github = strings.TrimPrefix(strings.TrimSpace(github), "@")
84 x = strings.TrimPrefix(strings.TrimSpace(x), "@")
85
86 if len(name) == 0 || len(name) > MaxName || hasControl(name) {
87 panic(ErrName)
88 }
89 if len(headline) == 0 || len(headline) > MaxHeadline || hasControl(headline) {
90 panic(ErrHeadline)
91 }
92 if len(bio) > MaxBio || hasControl(strings.ReplaceAll(bio, "\n", " ")) {
93 panic(ErrBio)
94 }
95 tags, ok := parseSkills(skills)
96 if !ok {
97 panic(ErrSkills)
98 }
99 if website != "" && !isLink(website) {
100 panic(ErrWebsite)
101 }
102 if github != "" && !isGitHub(github) {
103 panic(ErrGitHub)
104 }
105 if x != "" && !isX(x) {
106 panic(ErrX)
107 }
108
109 now := time.Now()
110 key := caller.String()
111 e := &Entry{Addr: caller, JoinedAt: now}
112 if v := entries.Get(key); v != nil {
113 e = v.(*Entry)
114 }
115 e.Name, e.Headline, e.Bio, e.Skills = name, headline, bio, tags
116 e.Website, e.GitHub, e.X, e.Open = website, github, x, open
117 e.UpdatedAt = now
118 entries.Set(key, e)
119 chain.Emit("ProfileSet", "addr", key)
120}
121
122// SetOpen flips only the "open to work" switch.
123func SetOpen(cur realm, open bool) {
124 key := cur.Previous().Address().String()
125 v := entries.Get(key)
126 if v == nil {
127 panic(ErrNotIn)
128 }
129 e := v.(*Entry)
130 e.Open = open
131 e.UpdatedAt = time.Now()
132 chain.Emit("ProfileSet", "addr", key)
133}
134
135// Leave removes the caller from the directory. Their Gild record stays.
136func Leave(cur realm) {
137 key := cur.Previous().Address().String()
138 if _, removed := entries.Remove(key); !removed {
139 panic(ErrNotIn)
140 }
141 chain.Emit("ProfileRemoved", "addr", key)
142}
143
144// IsListed reports whether addr is in the directory.
145func IsListed(addr address) bool {
146 return entries.Has(addr.String())
147}
148
149// Size returns how many people are listed.
150func Size() int { return entries.Size() }
151
152// ProfileJSON returns one builder with live Gild stats, or null.
153func ProfileJSON(addr address) string {
154 v := entries.Get(addr.String())
155 if v == nil {
156 return "null"
157 }
158 var b strings.Builder
159 writeEntry(&b, v.(*Entry))
160 return b.String()
161}
162
163// BuildersJSON lists builders with their live Gild stats.
164func BuildersJSON(offset, limit int) string {
165 if offset < 0 {
166 offset = 0
167 }
168 if limit <= 0 || limit > MaxPage {
169 limit = MaxPage
170 }
171 var b strings.Builder
172 b.WriteString(`{"total":`)
173 b.WriteString(strconv.Itoa(entries.Size()))
174 b.WriteString(`,"items":[`)
175 first := true
176 entries.IterateByOffset(offset, limit, func(_ string, v any) bool {
177 if !first {
178 b.WriteByte(',')
179 }
180 first = false
181 writeEntry(&b, v.(*Entry))
182 return false
183 })
184 b.WriteString("]}")
185 return b.String()
186}
187
188func writeEntry(b *strings.Builder, e *Entry) {
189 b.WriteString(`{"address":`)
190 b.WriteString(jsonStr(e.Addr.String()))
191 b.WriteString(`,"name":`)
192 b.WriteString(jsonStr(e.Name))
193 b.WriteString(`,"headline":`)
194 b.WriteString(jsonStr(e.Headline))
195 b.WriteString(`,"bio":`)
196 b.WriteString(jsonStr(e.Bio))
197 b.WriteString(`,"skills":[`)
198 for i, s := range e.Skills {
199 if i > 0 {
200 b.WriteByte(',')
201 }
202 b.WriteString(jsonStr(s))
203 }
204 b.WriteString(`],"website":`)
205 b.WriteString(jsonStr(e.Website))
206 b.WriteString(`,"github":`)
207 b.WriteString(jsonStr(e.GitHub))
208 b.WriteString(`,"x":`)
209 b.WriteString(jsonStr(e.X))
210 b.WriteString(`,"open":`)
211 b.WriteString(strconv.FormatBool(e.Open))
212 b.WriteString(`,"completed":`)
213 b.WriteString(strconv.Itoa(gild.Completed(e.Addr)))
214 b.WriteString(`,"earned":`)
215 b.WriteString(strconv.FormatInt(gild.Earned(e.Addr), 10))
216 b.WriteString(`,"joinedAt":`)
217 b.WriteString(strconv.FormatInt(e.JoinedAt.Unix(), 10))
218 b.WriteString(`,"updatedAt":`)
219 b.WriteString(strconv.FormatInt(e.UpdatedAt.Unix(), 10))
220 b.WriteByte('}')
221}
222
223// Render shows the directory, or one profile at u/<address>.
224func Render(path string) string {
225 if strings.HasPrefix(path, "u/") {
226 return renderProfile(strings.TrimPrefix(path, "u/"))
227 }
228 var b strings.Builder
229 b.WriteString("# Gild Builders\n\n")
230 b.WriteString("People with proven, paid work on [Gild](/r/" + gildPath() + "). ")
231 b.WriteString("To join, finish at least ")
232 b.WriteString(strconv.Itoa(MinTasks))
233 b.WriteString(" paid task, then call `SetProfile`. Stats below are read live from Gild.\n\n")
234 if entries.Size() == 0 {
235 b.WriteString("_Nobody has joined yet._\n")
236 return b.String()
237 }
238 b.WriteString("| Builder | Open to work | Tasks done | Earned | Skills |\n|---|---|---|---|---|\n")
239 entries.Iterate("", "", func(_ string, v any) bool {
240 e := v.(*Entry)
241 b.WriteString("| [" + mdText(e.Name) + "](" + webPath + ":u/" + e.Addr.String() + ") | ")
242 if e.Open {
243 b.WriteString("Yes")
244 } else {
245 b.WriteString("No")
246 }
247 b.WriteString(" | ")
248 b.WriteString(strconv.Itoa(gild.Completed(e.Addr)))
249 b.WriteString(" | ")
250 b.WriteString(gnot(gild.Earned(e.Addr)))
251 b.WriteString(" | ")
252 b.WriteString(mdText(strings.Join(e.Skills, ", ")))
253 b.WriteString(" |\n")
254 return false
255 })
256 return b.String()
257}
258
259func renderProfile(addr string) string {
260 v := entries.Get(addr)
261 if v == nil {
262 return "# Not listed\n\nThis address is not in the directory. [Back to Builders](" + webPath + ")\n"
263 }
264 e := v.(*Entry)
265 var b strings.Builder
266 b.WriteString("# " + mdText(e.Name) + "\n\n")
267 b.WriteString("**" + mdText(e.Headline) + "**\n\n")
268 if e.Open {
269 b.WriteString("Open to work.\n\n")
270 }
271 b.WriteString("| Tasks done | Earned |\n|---|---|\n| ")
272 b.WriteString(strconv.Itoa(gild.Completed(e.Addr)))
273 b.WriteString(" | ")
274 b.WriteString(gnot(gild.Earned(e.Addr)))
275 b.WriteString(" |\n\n")
276 if e.Bio != "" {
277 for _, line := range strings.Split(e.Bio, "\n") {
278 b.WriteString(mdText(line) + " \n")
279 }
280 b.WriteString("\n")
281 }
282 if len(e.Skills) > 0 {
283 b.WriteString("Skills: " + mdText(strings.Join(e.Skills, ", ")) + "\n\n")
284 }
285 if e.Website != "" {
286 b.WriteString("- Website: [" + mdText(strings.TrimPrefix(e.Website, "https://")) + "](" + e.Website + ")\n")
287 }
288 if e.GitHub != "" {
289 b.WriteString("- GitHub: [" + e.GitHub + "](https://github.com/" + e.GitHub + ")\n")
290 }
291 if e.X != "" {
292 b.WriteString("- X: [@" + mdText(e.X) + "](https://x.com/" + e.X + ")\n")
293 }
294 b.WriteString("\nAddress: `" + addr + "` · [Gild record](/r/" + gildPath() + ":u/" + addr + ")\n")
295 return b.String()
296}
297
298// gildPath is the Gild realm path without the gno.land/r/ prefix, derived from
299// this realm's own namespace so it works wherever both are deployed.
300func gildPath() string {
301 p := strings.TrimPrefix(webPath, "/r/")
302 if i := strings.Index(p, "/"); i >= 0 {
303 return p[:i] + "/gild/v0"
304 }
305 return "gild/v0"
306}
307
308// parseSkills splits, trims, lowercases and de-duplicates a comma list.
309func parseSkills(s string) ([]string, bool) {
310 out := []string{}
311 seen := map[string]bool{}
312 for _, part := range strings.Split(s, ",") {
313 t := strings.ToLower(strings.TrimSpace(part))
314 if t == "" {
315 continue
316 }
317 if len(t) > MaxSkill {
318 return nil, false
319 }
320 for _, r := range t {
321 ok := (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || strings.ContainsRune(" +#.-", r)
322 if !ok {
323 return nil, false
324 }
325 }
326 if seen[t] {
327 continue
328 }
329 seen[t] = true
330 out = append(out, t)
331 }
332 if len(out) > MaxSkills {
333 return nil, false
334 }
335 return out, true
336}
337
338func isGitHub(s string) bool {
339 if len(s) == 0 || len(s) > 39 || s[0] == '-' {
340 return false
341 }
342 for _, r := range s {
343 if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '-') {
344 return false
345 }
346 }
347 return true
348}
349
350func isX(s string) bool {
351 if len(s) == 0 || len(s) > 15 {
352 return false
353 }
354 for _, r := range s {
355 if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_') {
356 return false
357 }
358 }
359 return true
360}
361
362func hasControl(s string) bool {
363 for _, r := range s {
364 if r < 0x20 || r == 0x7f {
365 return true
366 }
367 }
368 return false
369}
370
371func isLink(s string) bool {
372 if len(s) > MaxLink || !strings.HasPrefix(s, "https://") || len(s) <= len("https://") {
373 return false
374 }
375 for _, r := range s {
376 if r <= ' ' || r == 0x7f || strings.ContainsRune("\"<>`()[]|", r) {
377 return false
378 }
379 }
380 return true
381}
382
383func gnot(u int64) string {
384 whole := u / 1_000_000
385 frac := u % 1_000_000
386 s := strconv.FormatInt(whole, 10)
387 if frac != 0 {
388 f := strconv.FormatInt(frac+1_000_000, 10)[1:]
389 s += "." + strings.TrimRight(f, "0")
390 }
391 return s + " GNOT"
392}
393
394// mdText escapes characters that would change markdown or table layout.
395func mdText(s string) string {
396 var b strings.Builder
397 for _, r := range s {
398 if strings.ContainsRune("\\`*_[]()<>#|!~", r) {
399 b.WriteByte('\\')
400 }
401 b.WriteRune(r)
402 }
403 return b.String()
404}
405
406func jsonStr(s string) string {
407 var b strings.Builder
408 b.WriteByte('"')
409 for _, r := range s {
410 switch {
411 case r == '"':
412 b.WriteString(`\"`)
413 case r == '\\':
414 b.WriteString(`\\`)
415 case r < 0x20 || r == '<' || r == '>' || r == '&' || r == 0x2028 || r == 0x2029:
416 h := strconv.FormatInt(int64(r), 16)
417 b.WriteString(`\u` + strings.Repeat("0", 4-len(h)) + h)
418 default:
419 b.WriteRune(r)
420 }
421 }
422 b.WriteByte('"')
423 return b.String()
424}