package gild import ( "strconv" "strings" "time" "gno.land/p/nt/seqid/v0" ) // AppURL is the web app. Every gnoweb page links to it, but nothing here // depends on it: the realm works the same from gnokey or gnoweb. const AppURL = "https://gild.sithunyein.com/app" // Render serves the gnoweb pages: // // "" the board: totals, open tasks, rules // "task/" one task // "u/
" someone's work record func Render(path string) string { switch { case path == "": return renderHome() case strings.HasPrefix(path, "task/"): id, err := strconv.ParseInt(strings.TrimPrefix(path, "task/"), 10, 64) if err != nil || id < 1 { return notFound() } v := tasks.Get(seqid.ID(id).Binary()) if v == nil { return notFound() } return renderTask(v.(*Task)) case strings.HasPrefix(path, "u/"): addr := address(strings.TrimPrefix(path, "u/")) if !addr.IsValid() { return notFound() } return renderProfile(addr) } return notFound() } func notFound() string { return "# Not found\n\n[Back to the board](" + webPath + ")\n" } func renderHome() string { var b strings.Builder b.WriteString("# Gild\n\n") b.WriteString("**Post work. Pay on proof.** The reward is locked in this realm when a task is posted, ") b.WriteString("and paid to the doer when the poster approves. Every payout adds to a public work record ") b.WriteString("that other realms can read.\n\n") b.WriteString("[Open the app](" + AppURL + ") · [Builders](" + siblingPath("builders/v2") + ") · ") b.WriteString("[Source](" + webPath + "$source) · [Earlier board (v0)](" + siblingPath("gild/v0") + ")\n\n") b.WriteString("| Open tasks | Locked | Paid to doers | Tasks completed |\n|---|---|---|---|\n") b.WriteString("| " + strconv.Itoa(openTasks.Size()) + " | " + gnot(lockedTotal) + " | " + gnot(paidTotal) + " | " + strconv.Itoa(book.Total()) + " |\n\n") b.WriteString("## Open tasks\n\n") if openTasks.Size() == 0 { b.WriteString("No open tasks right now. [Post one in the app](" + AppURL + "#/post).\n\n") } else { b.WriteString("| Task | Reward | Who can take it | Due |\n|---|---|---|---|\n") openTasks.ReverseIterateByOffset(0, 30, func(_ string, v any) bool { t := v.(*Task) b.WriteString("| [" + cell(t.Title) + "](" + webPath + ":task/" + idStr(t.ID) + ") | **" + gnot(t.Reward) + "** | " + whoCanTake(t) + " | " + t.Deadline.UTC().Format("Jan 2") + " |\n") return false }) b.WriteString("\n") } b.WriteString("## How a task works\n\n") b.WriteString("1. **Post and lock.** The poster sends the reward with `Post`. It stays in this realm.\n") b.WriteString("2. **Take.** One doer takes it. Posters can require past paid work, or offer the task to one person.\n") b.WriteString("3. **Send work.** The doer sends a link with `Submit`. The poster can ask for changes once.\n") b.WriteString("4. **Get paid.** `Approve` pays the doer. If the poster is silent for 7 days, the doer can `ClaimAfterSilence`.\n\n") b.WriteString("Nobody took it, or no work by the deadline? The poster can `Cancel` for a full refund. ") b.WriteString("Once work is sent, the poster can no longer cancel. There is no admin key and no fee.\n\n") b.WriteString("## Use the work record in your realm\n\n") b.WriteString("```go\nimport gild \"gno.land" + webPath + "\"\n\n") 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") b.WriteString("`Completed`, `Earned` and `HasCompleted` count paid work from both Gild boards. ") b.WriteString("[Builders](" + siblingPath("builders/v2") + ") uses them to accept only people with proven work.\n") return b.String() } func renderTask(t *Task) string { var b strings.Builder b.WriteString("# " + mdText(t.Title) + "\n\n") b.WriteString("[← Board](" + webPath + ") · [Open in the app](" + AppURL + "#/task/" + idStr(t.ID) + ")\n\n") b.WriteString("| | |\n|---|---|\n") b.WriteString("| Reward | **" + gnot(t.Reward) + "** |\n") b.WriteString("| Status | " + statusText(t.Status) + " |\n") b.WriteString("| Category | " + t.Category + " |\n") b.WriteString("| Who can take it | " + whoCanTake(t) + " |\n") b.WriteString("| Poster | " + who(t.Poster) + " |\n") if t.Doer != "" { b.WriteString("| Doer | " + who(t.Doer) + " |\n") } b.WriteString("| Deadline | " + t.Deadline.UTC().Format("Jan 2, 2006 15:04 MST") + " |\n") if t.Status == StatusInReview { b.WriteString("| Auto-pay from | " + t.SubmittedAt.Add(ReviewWindow).UTC().Format("Jan 2, 2006 15:04 MST") + " |\n") } if t.Status == StatusPaid { b.WriteString("| Paid by | " + t.PaidBy + " |\n") } b.WriteString("\n") if t.Details != "" { b.WriteString("## The task\n\n" + mdText(t.Details) + "\n\n") } if t.Done != "" { b.WriteString("## Done looks like\n\n" + mdText(t.Done) + "\n\n") } if t.WorkURL != "" { b.WriteString("## Work\n\n" + mdText(t.WorkURL) + "\n\n") } if t.ChangesNote != "" { b.WriteString("## Changes asked\n\n" + mdText(t.ChangesNote) + "\n\n") } if fn := nextStep(t); fn != "" { b.WriteString("## Next step\n\n" + fn + "\n") } return b.String() } // nextStep tells a gnoweb reader which function moves the task forward. func nextStep(t *Task) string { act := func(fn string) string { return "[" + fn + "](" + webPath + "$help&func=" + fn + ")" } switch t.Status { case StatusOpen: if time.Now().Before(t.Deadline) { return "Call " + act("Take") + " with id " + idStr(t.ID) + ", or [take it in the app](" + AppURL + "#/task/" + idStr(t.ID) + ")." } return "The deadline passed. The poster can " + act("Cancel") + " for a refund." case StatusTaken: return "The doer sends work with " + act("Submit") + "." case StatusInReview: return "The poster can " + act("Approve") + " or " + act("RequestChanges") + ". After 7 days of silence the doer can " + act("ClaimAfterSilence") + "." } return "" } func renderProfile(addr address) string { p := book.Profile(addr) var b strings.Builder b.WriteString("# Work record\n\n`" + addr.String() + "`\n\n") b.WriteString("[← Board](" + webPath + ") · [Profile in the app](" + AppURL + "#/u/" + addr.String() + ")\n\n") b.WriteString("| Tasks done | Earned | Paid out (v1) |\n|---|---|---|\n") b.WriteString("| " + strconv.Itoa(Completed(addr)) + " | " + gnot(Earned(addr)) + " | " + strconv.Itoa(p.Posted) + " |\n\n") b.WriteString("Tasks done and earned include work on the [earlier board](" + siblingPath("gild/v0") + ":u/" + addr.String() + ").\n\n") recent := p.Recent() if len(recent) == 0 { return b.String() } b.WriteString("## Recent work on this board\n\n| Task | Paid | When |\n|---|---|---|\n") for _, r := range recent { b.WriteString("| [" + cell(r.Title) + "](" + webPath + ":task/" + r.TaskID + ") | " + gnot(r.Amount) + " | " + r.At.UTC().Format("Jan 2, 2006") + " |\n") } return b.String() } func whoCanTake(t *Task) string { switch { case t.Invite != "": return "Offered to " + who(t.Invite) case t.MinDone > 0: return strconv.Itoa(t.MinDone) + "+ paid tasks" } return "Anyone" } func statusText(s Status) string { switch s { case StatusOpen: return "Open" case StatusTaken: return "In progress" case StatusInReview: return "In review" case StatusPaid: return "Paid" case StatusRefunded: return "Refunded" } return "Unknown" } // who links an address to its work record, shortened to fit a table. func who(a address) string { s := a.String() short := s if len(s) > 12 { short = s[:6] + "…" + s[len(s)-4:] } return "[" + short + "](" + webPath + ":u/" + s + ")" } // cell is mdText for a table cell: one line, no pipes. func cell(s string) string { return mdText(strings.Join(strings.Fields(s), " ")) } // siblingPath returns another realm in this namespace, e.g. "builders/v2". func siblingPath(rel string) string { p := webPath if i := strings.Index(p, "/gild/"); i >= 0 { p = p[:i] } return p + "/" + rel }