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.

render.gno

9.83 Kb · 292 lines
  1package erratabank
  2
  3import (
  4	"strconv"
  5
  6	"gno.land/p/nt/avl/v0"
  7
  8	"gno.land/p/g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807/errata/v0"
  9	"gno.land/p/nt/markdown/sanitize/v0"
 10	"gno.land/p/nt/mux/v0"
 11	"gno.land/p/nt/ufmt/v0"
 12)
 13
 14const pageSize = 20
 15const reportNotice = "Reports are public and permanent on-chain."
 16
 17var router = mux.NewRouter()
 18
 19func init() {
 20	router.HandleFunc("", renderHome)
 21	router.HandleFunc("book/{id}", renderBook)
 22	router.HandleFunc("book/{id}/pending", renderPending)
 23	router.HandleFunc("book/{id}/all", renderAll)
 24	router.HandleFunc("erratum/{id}", renderErratum)
 25	router.HandleFunc("finder/{addr}", renderFinder)
 26	router.HandleFunc("finder/{addr}/unpaid", renderUnpaidFinder)
 27	router.SetNotFoundHandler(func(w *mux.ResponseWriter, _ *mux.Request) {
 28		notFound(w, "Page")
 29	})
 30}
 31
 32// Render is the public Markdown entry point for gnoweb.
 33func Render(path string) string {
 34	return router.Render(path)
 35}
 36
 37func finderURL(addr address) string {
 38	return realmURL + "finder/" + addr.String()
 39}
 40
 41func bookRoute(id int) string {
 42	return "book/" + strconv.Itoa(id)
 43}
 44
 45func erratumURL(id int) string {
 46	return realmURL + "erratum/" + strconv.Itoa(id)
 47}
 48
 49func bookURL(id int) string {
 50	return realmURL + bookRoute(id)
 51}
 52
 53func pageOffset(req *mux.Request, total int) (int, bool) {
 54	value := req.Query.Get("page")
 55	if value == "" {
 56		return 0, true
 57	}
 58	page, err := strconv.Atoi(value)
 59	if err != nil || page < 1 || (page > 1 && page-1 > (total-1)/pageSize) {
 60		return 0, false
 61	}
 62	return (page - 1) * pageSize, true
 63}
 64
 65func pagination(w *mux.ResponseWriter, route string, offset, total int) {
 66	if total <= pageSize {
 67		return
 68	}
 69	page := offset/pageSize + 1
 70	last := (total-1)/pageSize + 1
 71	w.Write(ufmt.Sprintf("\nPage %d of %d", page, last))
 72	if page > 1 {
 73		w.Write(ufmt.Sprintf(" · [Previous](%s%s?page=%d)", realmURL, route, page-1))
 74	}
 75	if page < last {
 76		w.Write(ufmt.Sprintf(" · [Next](%s%s?page=%d)", realmURL, route, page+1))
 77	}
 78	w.Write("\n")
 79}
 80
 81func notFound(w *mux.ResponseWriter, item string) {
 82	w.Write("# " + item + " not found\n\n[Erratabank home](" + realmURL + ")\n")
 83}
 84
 85func renderHome(w *mux.ResponseWriter, req *mux.Request) {
 86	offset, ok := pageOffset(req, books.Size())
 87	if !ok {
 88		notFound(w, "Page")
 89		return
 90	}
 91	w.Write("# Erratabank\n\n")
 92	w.Write("Erratabank is a public errata page for books, courses, and technical docs.\n\n")
 93	w.Write("Book owners decide which reports to accept. Owner deposits and locked sponsor funds can cover rewards for eligible reports.\n\n")
 94	w.Write(reportNotice + "\n\n")
 95	w.Write("Use the Actions tab to register a book, submit a correction, or review reports for a book you own.\n\n")
 96	w.Write("## Books\n\n")
 97	if books.Size() == 0 {
 98		w.Write("No books registered yet.\n")
 99	} else {
100		w.Write("| Book | Owner | Reward per erratum | Owner funds | Sponsor funds | Owed | Accepted |\n")
101		w.Write("| --- | --- | ---: | ---: | ---: | ---: | ---: |\n")
102		for _, book := range ListBooks(offset, pageSize) {
103			w.Write(ufmt.Sprintf("| [%s](%s) | %s | %s | %s | %s | %s | %d |\n",
104				sanitize.TableCell(book.Title), bookURL(book.ID), errata.ShortAddr(book.Owner.String()),
105				errata.FormatUgnot(book.RewardPerErratum), errata.FormatUgnot(book.OwnerFunds),
106				errata.FormatUgnot(book.SponsorFunds), errata.FormatUgnot(book.Owed),
107				requireBook(book.ID).accepted.Size()))
108		}
109		pagination(w, "", offset, books.Size())
110	}
111	w.Write("\nStarted 23 Nov 2026 for Gnome Tavern.\n")
112}
113
114func routeBook(w *mux.ResponseWriter, req *mux.Request) *bookRecord {
115	if id, err := strconv.Atoi(req.GetVar("id")); err == nil {
116		if record := bookByID(id); record != nil {
117			return record
118		}
119	}
120	notFound(w, "Book")
121	return nil
122}
123
124func renderBook(w *mux.ResponseWriter, req *mux.Request) {
125	record := routeBook(w, req)
126	if record == nil {
127		return
128	}
129	offset, ok := pageOffset(req, record.accepted.Size())
130	if !ok {
131		notFound(w, "Page")
132		return
133	}
134	book := record.book
135	state := "Closed"
136	if book.Open {
137		state = "Open"
138	}
139	w.Write(ufmt.Sprintf("# %s\n\n- Edition: %s\n- Owner: %s\n- Submissions: %s\n- Reward per erratum: %s\n- Owner funds: %s\n- Sponsor funds: %s (locked)\n- Owed: %s\n- Owner funds available to withdraw: %s\n",
140		sanitize.InlineText(book.Title), sanitize.InlineText(book.Edition), sanitize.InlineCode(book.Owner.String()),
141		state, errata.FormatUgnot(book.RewardPerErratum), errata.FormatUgnot(book.OwnerFunds),
142		errata.FormatUgnot(book.SponsorFunds), errata.FormatUgnot(book.Owed), errata.FormatUgnot(record.ownerWithdrawable())))
143	if url := sanitize.URL(book.Link); url != "" {
144		w.Write("- [Read the book](" + url + ")\n")
145	}
146	w.Write("\nThe owner decides which reports to accept. Eligible rewards are paid from book funds when available or recorded as owed.\n\n")
147	w.Write(errata.RenderSection("Accepted errata", AcceptedEntriesPage(book.ID, offset, pageSize)))
148	pagination(w, bookRoute(book.ID), offset, record.accepted.Size())
149	w.Write("\n" + reportNotice + "\n\n")
150	w.Write(ufmt.Sprintf("[Pending (%d)](%s/pending) · [All reports (%d)](%s/all) · Rejected: %d · Duplicates: %d · [All books](%s)\n",
151		record.pending.Size(), bookURL(book.ID), record.all.Size(), bookURL(book.ID),
152		record.rejected.Size(), record.duplicate.Size(), realmURL))
153}
154
155func renderPending(w *mux.ResponseWriter, req *mux.Request) {
156	renderReportList(w, req, Pending, "Pending reports", "/pending")
157}
158
159func renderAll(w *mux.ResponseWriter, req *mux.Request) {
160	renderReportList(w, req, "", "All reports", "/all")
161}
162
163func renderReportList(w *mux.ResponseWriter, req *mux.Request, status, heading, suffix string) {
164	record := routeBook(w, req)
165	if record == nil {
166		return
167	}
168	tree := record.reports(status)
169	offset, ok := pageOffset(req, tree.Size())
170	if !ok {
171		notFound(w, "Page")
172		return
173	}
174	book := record.book
175	w.Write(ufmt.Sprintf("# %s\n\n[Back to %s](%s)\n\n",
176		heading, sanitize.InlineText(book.Title), bookURL(book.ID)))
177	if tree.Size() == 0 {
178		w.Write("No reports here yet.\n")
179		return
180	}
181	header := "| Report | Location | Status | Finder | Height found |"
182	dividers := "| --- | --- | --- | --- | ---: |"
183	if status == "" {
184		header += " Correction |"
185		dividers += " --- |"
186	}
187	w.Write(header + "\n" + dividers + "\n")
188	for _, report := range listReports(tree, offset, pageSize) {
189		w.Write(ufmt.Sprintf("| [#%d](%s) | %s | %s | [%s](%s) | %d |",
190			report.ID, erratumURL(report.ID), sanitize.TableCell(report.Location), report.Status,
191			errata.ShortAddr(report.Finder.String()), finderURL(report.Finder), report.SubmittedAt))
192		if status == "" {
193			w.Write(" " + sanitize.TableCell(report.Text))
194			if report.Status == Rejected && report.Note != "" {
195				w.Write(" — " + sanitize.TableCell(report.Note))
196			}
197			w.Write(" |")
198		}
199		w.Write("\n")
200	}
201	pagination(w, bookRoute(book.ID)+suffix, offset, tree.Size())
202}
203
204func renderErratum(w *mux.ResponseWriter, req *mux.Request) {
205	id, err := strconv.Atoi(req.GetVar("id"))
206	report, ok := GetErratum(id)
207	if err != nil || !ok {
208		notFound(w, "Erratum")
209		return
210	}
211	book, _ := GetBook(report.BookID)
212	w.Write(ufmt.Sprintf("# Erratum #%d\n\n[Back to %s](%s)\n\n",
213		id, sanitize.InlineText(book.Title), bookURL(book.ID)))
214	w.Write(ufmt.Sprintf("- Location: %s\n- Finder: [%s](%s)\n- Status: %s\n- Paid: %s\n- Owed: %s\n\n",
215		sanitize.InlineText(report.Location), report.Finder.String(), finderURL(report.Finder),
216		report.Status, errata.FormatUgnot(report.Paid), errata.FormatUgnot(report.Owed)))
217	if report.Status == Rejected {
218		w.Write("[View the rejected correction in All reports](" + bookURL(book.ID) + "/all)\n\n")
219	} else {
220		w.Write("## Correction\n\n" + sanitize.InlineText(report.Text) + "\n\n")
221	}
222	w.Write(ufmt.Sprintf("## Status history\n\n- Height %d — Pending (submitted)\n", report.SubmittedAt))
223	if report.Status != Pending {
224		w.Write(ufmt.Sprintf("- Height %d — %s\n", report.ReviewedAt, report.Status))
225	}
226	if report.Note != "" && report.Status != Rejected {
227		w.Write("- Review note: " + sanitize.InlineText(report.Note) + "\n")
228	}
229	if report.DuplicateOf != 0 {
230		w.Write(ufmt.Sprintf("- Earlier report: [#%d](%s)\n", report.DuplicateOf, erratumURL(report.DuplicateOf)))
231	}
232}
233
234func renderFinder(w *mux.ResponseWriter, req *mux.Request) {
235	renderFinderCredits(w, req, false)
236}
237
238func renderUnpaidFinder(w *mux.ResponseWriter, req *mux.Request) {
239	renderFinderCredits(w, req, true)
240}
241
242func renderFinderCredits(w *mux.ResponseWriter, req *mux.Request, unpaid bool) {
243	raw := req.GetVar("addr")
244	cleaned := sanitize.BechString(raw, "g")
245	if cleaned == "" || cleaned != raw || !address(cleaned).IsValid() {
246		notFound(w, "Finder")
247		return
248	}
249	addr := address(cleaned)
250	stats := FinderStats(addr)
251	finder, _ := finders.Get(cleaned).(*finderRecord)
252	route := "finder/" + cleaned
253	count := stats.Credits
254	var tree *avl.Tree
255	if finder != nil {
256		tree = &finder.credits
257	}
258	if unpaid {
259		route += "/unpaid"
260		count = stats.UnpaidCredits
261		if finder != nil {
262			tree = &finder.unpaid
263		}
264	}
265	offset, ok := pageOffset(req, count)
266	if !ok {
267		notFound(w, "Page")
268		return
269	}
270
271	w.Write(ufmt.Sprintf("# Finder account\n\n%s\n\n- Reward-eligible credits: %d\n- Unpaid credits: %d\n- Paid: %s\n- Owed: %s\n\n",
272		sanitize.InlineCode(cleaned), stats.Credits, stats.UnpaidCredits,
273		errata.FormatUgnot(stats.Paid), errata.FormatUgnot(stats.Owed)))
274	if unpaid {
275		w.Write("[Reward-eligible credits](" + finderURL(addr) + ")\n\n## Unpaid credits\n\n")
276	} else {
277		w.Write("[Unpaid credits](" + finderURL(addr) + "/unpaid)\n\n## Reward-eligible credits\n\n")
278	}
279	if count == 0 {
280		w.Write("No credits in this view yet.\n")
281		return
282	}
283	w.Write("| Book | Find | Height found | Paid | Owed |\n| --- | --- | ---: | ---: | ---: |\n")
284	for _, report := range listReports(tree, offset, pageSize) {
285		book, _ := GetBook(report.BookID)
286		w.Write(ufmt.Sprintf("| [%s](%s) | [%s](%s) | %d | %s | %s |\n",
287			sanitize.TableCell(book.Title), bookURL(book.ID),
288			sanitize.TableCell(report.Location), erratumURL(report.ID), report.SubmittedAt,
289			errata.FormatUgnot(report.Paid), errata.FormatUgnot(report.Owed)))
290	}
291	pagination(w, route, offset, count)
292}