render.gno
7.99 Kb · 212 lines
1package gild
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 "gno.land/p/nt/seqid/v0"
9)
10
11// AppURL is the web app. Every gnoweb page links to it, but nothing here
12// depends on it: the realm works the same from gnokey or gnoweb.
13const AppURL = "https://gild.sithunyein.com/app"
14
15// Render serves the gnoweb pages:
16//
17// "" the board: totals, open tasks, rules
18// "task/<id>" one task
19// "u/<address>" someone's work record
20func Render(path string) string {
21 switch {
22 case path == "":
23 return renderHome()
24 case strings.HasPrefix(path, "task/"):
25 id, err := strconv.ParseInt(strings.TrimPrefix(path, "task/"), 10, 64)
26 if err != nil || id < 1 {
27 return notFound()
28 }
29 v := tasks.Get(seqid.ID(id).Binary())
30 if v == nil {
31 return notFound()
32 }
33 return renderTask(v.(*Task))
34 case strings.HasPrefix(path, "u/"):
35 addr := address(strings.TrimPrefix(path, "u/"))
36 if !addr.IsValid() {
37 return notFound()
38 }
39 return renderProfile(addr)
40 }
41 return notFound()
42}
43
44func notFound() string {
45 return "# Not found\n\n[Back to the board](" + webPath + ")\n"
46}
47
48func renderHome() string {
49 var b strings.Builder
50 b.WriteString("# Gild\n\n")
51 b.WriteString("**Post work. Pay on proof.** The reward is locked in this realm when a task is posted, ")
52 b.WriteString("and paid to the doer when the poster approves. Every payout adds to a public work record ")
53 b.WriteString("that other realms can read.\n\n")
54 b.WriteString("[Open the app](" + AppURL + ") · [Builders](" + siblingPath("builders/v2") + ") · ")
55 b.WriteString("[Source](" + webPath + "$source) · [Earlier board (v0)](" + siblingPath("gild/v0") + ")\n\n")
56
57 b.WriteString("| Open tasks | Locked | Paid to doers | Tasks completed |\n|---|---|---|---|\n")
58 b.WriteString("| " + strconv.Itoa(openTasks.Size()) + " | " + gnot(lockedTotal) + " | " + gnot(paidTotal) + " | " + strconv.Itoa(book.Total()) + " |\n\n")
59
60 b.WriteString("## Open tasks\n\n")
61 if openTasks.Size() == 0 {
62 b.WriteString("No open tasks right now. [Post one in the app](" + AppURL + "#/post).\n\n")
63 } else {
64 b.WriteString("| Task | Reward | Who can take it | Due |\n|---|---|---|---|\n")
65 openTasks.ReverseIterateByOffset(0, 30, func(_ string, v any) bool {
66 t := v.(*Task)
67 b.WriteString("| [" + cell(t.Title) + "](" + webPath + ":task/" + idStr(t.ID) + ") | **" + gnot(t.Reward) + "** | " + whoCanTake(t) + " | " + t.Deadline.UTC().Format("Jan 2") + " |\n")
68 return false
69 })
70 b.WriteString("\n")
71 }
72
73 b.WriteString("## How a task works\n\n")
74 b.WriteString("1. **Post and lock.** The poster sends the reward with `Post`. It stays in this realm.\n")
75 b.WriteString("2. **Take.** One doer takes it. Posters can require past paid work, or offer the task to one person.\n")
76 b.WriteString("3. **Send work.** The doer sends a link with `Submit`. The poster can ask for changes once.\n")
77 b.WriteString("4. **Get paid.** `Approve` pays the doer. If the poster is silent for 7 days, the doer can `ClaimAfterSilence`.\n\n")
78 b.WriteString("Nobody took it, or no work by the deadline? The poster can `Cancel` for a full refund. ")
79 b.WriteString("Once work is sent, the poster can no longer cancel. There is no admin key and no fee.\n\n")
80
81 b.WriteString("## Use the work record in your realm\n\n")
82 b.WriteString("```go\nimport gild \"gno.land" + webPath + "\"\n\n")
83 b.WriteString("func Join(cur realm) {\n\tif !gild.HasCompleted(cur.Previous().Address(), 1) {\n\t\tpanic(\"finish a paid Gild task first\")\n\t}\n}\n```\n\n")
84 b.WriteString("`Completed`, `Earned` and `HasCompleted` count paid work from both Gild boards. ")
85 b.WriteString("[Builders](" + siblingPath("builders/v2") + ") uses them to accept only people with proven work.\n")
86 return b.String()
87}
88
89func renderTask(t *Task) string {
90 var b strings.Builder
91 b.WriteString("# " + mdText(t.Title) + "\n\n")
92 b.WriteString("[← Board](" + webPath + ") · [Open in the app](" + AppURL + "#/task/" + idStr(t.ID) + ")\n\n")
93 b.WriteString("| | |\n|---|---|\n")
94 b.WriteString("| Reward | **" + gnot(t.Reward) + "** |\n")
95 b.WriteString("| Status | " + statusText(t.Status) + " |\n")
96 b.WriteString("| Category | " + t.Category + " |\n")
97 b.WriteString("| Who can take it | " + whoCanTake(t) + " |\n")
98 b.WriteString("| Poster | " + who(t.Poster) + " |\n")
99 if t.Doer != "" {
100 b.WriteString("| Doer | " + who(t.Doer) + " |\n")
101 }
102 b.WriteString("| Deadline | " + t.Deadline.UTC().Format("Jan 2, 2006 15:04 MST") + " |\n")
103 if t.Status == StatusInReview {
104 b.WriteString("| Auto-pay from | " + t.SubmittedAt.Add(ReviewWindow).UTC().Format("Jan 2, 2006 15:04 MST") + " |\n")
105 }
106 if t.Status == StatusPaid {
107 b.WriteString("| Paid by | " + t.PaidBy + " |\n")
108 }
109 b.WriteString("\n")
110 if t.Details != "" {
111 b.WriteString("## The task\n\n" + mdText(t.Details) + "\n\n")
112 }
113 if t.Done != "" {
114 b.WriteString("## Done looks like\n\n" + mdText(t.Done) + "\n\n")
115 }
116 if t.WorkURL != "" {
117 b.WriteString("## Work\n\n" + mdText(t.WorkURL) + "\n\n")
118 }
119 if t.ChangesNote != "" {
120 b.WriteString("## Changes asked\n\n" + mdText(t.ChangesNote) + "\n\n")
121 }
122 if fn := nextStep(t); fn != "" {
123 b.WriteString("## Next step\n\n" + fn + "\n")
124 }
125 return b.String()
126}
127
128// nextStep tells a gnoweb reader which function moves the task forward.
129func nextStep(t *Task) string {
130 act := func(fn string) string { return "[" + fn + "](" + webPath + "$help&func=" + fn + ")" }
131 switch t.Status {
132 case StatusOpen:
133 if time.Now().Before(t.Deadline) {
134 return "Call " + act("Take") + " with id " + idStr(t.ID) + ", or [take it in the app](" + AppURL + "#/task/" + idStr(t.ID) + ")."
135 }
136 return "The deadline passed. The poster can " + act("Cancel") + " for a refund."
137 case StatusTaken:
138 return "The doer sends work with " + act("Submit") + "."
139 case StatusInReview:
140 return "The poster can " + act("Approve") + " or " + act("RequestChanges") + ". After 7 days of silence the doer can " + act("ClaimAfterSilence") + "."
141 }
142 return ""
143}
144
145func renderProfile(addr address) string {
146 p := book.Profile(addr)
147 var b strings.Builder
148 b.WriteString("# Work record\n\n`" + addr.String() + "`\n\n")
149 b.WriteString("[← Board](" + webPath + ") · [Profile in the app](" + AppURL + "#/u/" + addr.String() + ")\n\n")
150 b.WriteString("| Tasks done | Earned | Paid out (v1) |\n|---|---|---|\n")
151 b.WriteString("| " + strconv.Itoa(Completed(addr)) + " | " + gnot(Earned(addr)) + " | " + strconv.Itoa(p.Posted) + " |\n\n")
152 b.WriteString("Tasks done and earned include work on the [earlier board](" + siblingPath("gild/v0") + ":u/" + addr.String() + ").\n\n")
153 recent := p.Recent()
154 if len(recent) == 0 {
155 return b.String()
156 }
157 b.WriteString("## Recent work on this board\n\n| Task | Paid | When |\n|---|---|---|\n")
158 for _, r := range recent {
159 b.WriteString("| [" + cell(r.Title) + "](" + webPath + ":task/" + r.TaskID + ") | " + gnot(r.Amount) + " | " + r.At.UTC().Format("Jan 2, 2006") + " |\n")
160 }
161 return b.String()
162}
163
164func whoCanTake(t *Task) string {
165 switch {
166 case t.Invite != "":
167 return "Offered to " + who(t.Invite)
168 case t.MinDone > 0:
169 return strconv.Itoa(t.MinDone) + "+ paid tasks"
170 }
171 return "Anyone"
172}
173
174func statusText(s Status) string {
175 switch s {
176 case StatusOpen:
177 return "Open"
178 case StatusTaken:
179 return "In progress"
180 case StatusInReview:
181 return "In review"
182 case StatusPaid:
183 return "Paid"
184 case StatusRefunded:
185 return "Refunded"
186 }
187 return "Unknown"
188}
189
190// who links an address to its work record, shortened to fit a table.
191func who(a address) string {
192 s := a.String()
193 short := s
194 if len(s) > 12 {
195 short = s[:6] + "…" + s[len(s)-4:]
196 }
197 return "[" + short + "](" + webPath + ":u/" + s + ")"
198}
199
200// cell is mdText for a table cell: one line, no pipes.
201func cell(s string) string {
202 return mdText(strings.Join(strings.Fields(s), " "))
203}
204
205// siblingPath returns another realm in this namespace, e.g. "builders/v2".
206func siblingPath(rel string) string {
207 p := webPath
208 if i := strings.Index(p, "/gild/"); i >= 0 {
209 p = p[:i]
210 }
211 return p + "/" + rel
212}