Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

builders.gno

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