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

5.74 Kb · 175 lines
  1package threads
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	"chain/runtime"
  8
  9	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/store/v0"
 10	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
 11	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
 12	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/realmpath/v0"
 13	th "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/social/threads/v0"
 14)
 15
 16// Render routes three views: the realm's own index, one page's threads, and
 17// one thread.
 18//
 19// A page key contains slashes, so "page/<key>" is split on the prefix rather
 20// than by path element: realmpath gives the parts, and rejoining them would
 21// only be a way to get it wrong.
 22// perPage bounds every list this realm renders. A Render is paid for by every
 23// reader, so its cost has to be bounded by what it SHOWS rather than by what
 24// anybody has ever written, and both Post and Reply are permissionless with no
 25// delete path. 20 matches what the index already shows with board.Recent(20).
 26const perPage = 20
 27
 28func Render(path string) string {
 29	req := realmpath.Parse(path)
 30
 31	switch {
 32	case strings.HasPrefix(req.Path, "page/"):
 33		return renderPage(strings.TrimPrefix(req.Path, "page/"))
 34	case strings.HasPrefix(req.Path, "thread/"):
 35		return renderThread(strings.TrimPrefix(req.Path, "thread/"))
 36	case req.Path == "":
 37		return renderIndex()
 38	default:
 39		return md.H1("Not found") + "No such view: " + ui.Inline(req.Path)
 40	}
 41}
 42
 43func renderIndex() string {
 44	out := md.H1("Threads")
 45	out += "A discussion block any realm can embed, keyed on the page it appears on.\n\n"
 46
 47	t := ui.NewTable("threads", "pages", "THREAD supply")
 48	t.Row(strconv.Itoa(board.Len()), strconv.Itoa(board.Pages()), strconv.FormatInt(points.Supply(), 10))
 49	out += t.String()
 50
 51	out += md.H2("Recent, across every page")
 52	out += listing(board.Recent(20), true)
 53
 54	out += md.H2("The token")
 55	out += points.Render()
 56
 57	out += md.H2("Embedding it")
 58	out += "Two lines in the host realm, and it stores nothing:\n\n"
 59	out += md.LanguageCodeBlock("go", `import "`+realmPath+`"
 60
 61func Render(path string) string {
 62	return body + threads.RenderBlock()
 63}`)
 64	out += "The page key is the host realm's own path, read off the call. " +
 65		md.InlineCode("RenderBlockFor(page)") + " names one explicitly.\n"
 66
 67	return out
 68}
 69
 70func renderPage(page string) string {
 71	if !th.ValidPage(page) {
 72		return md.H1("Threads") + "Not a page key: " + ui.Inline(page)
 73	}
 74	out := md.H1("Threads on " + ui.Inline(page))
 75	out += md.Link("the page itself", th.RealmURL(page)) + "\n\n"
 76
 77	// Finding 325-cost-at-caps-1: this passed limit 0, which means "every
 78	// thread anybody ever posted on this page". Post is permissionless and
 79	// the page key is any string, so a visitor fills a page they do not own
 80	// and every reader of it pays. There is no delete path, so it is
 81	// irreversible.
 82	total := board.PageLen(page)
 83	out += listing(board.List(page, runtime.ChainHeight(), perPage), false)
 84	if total > perPage {
 85		out += md.Italic("Showing " + strconv.Itoa(perPage) + " of " +
 86			strconv.Itoa(total) + " threads, newest and pinned first.") + "\n"
 87	}
 88
 89	out += "\n" + ui.Action("💬 Post here", "Post", "page", page, "body", "") + "\n"
 90	return out
 91}
 92
 93func renderThread(raw string) string {
 94	id, ok := store.ParseID(raw)
 95	if !ok {
 96		return md.H1("Threads") + "Not a thread id: " + ui.Inline(raw)
 97	}
 98	t, found := board.Get(id)
 99	if !found {
100		return md.H1("Threads") + "No thread #" + id.String()
101	}
102
103	out := md.H1("Thread #" + id.String())
104	out += md.Blockquote(ui.Inline(t.Body)) + "\n"
105	out += md.Italic("by "+ui.Addr(t.Author)+" · on "+
106		md.Link(t.Page, th.PageURL(realmPath, t.Page))+
107		" · "+strconv.Itoa(t.Repliers())+" earned") + "\n\n"
108
109	if t.PinnedUntil > runtime.ChainHeight() {
110		out += "📌 pinned until block " + strconv.FormatInt(t.PinnedUntil, 10) + "\n\n"
111	}
112
113	out += md.H2("Replies")
114	if len(t.Replies) == 0 {
115		out += ui.Empty("Nobody has replied yet.")
116	} else {
117		// Finding 325-cost-at-caps-1: this rendered every reply ever
118		// written. Reply is permissionless and nothing deletes one, so the
119		// page a reader pays for grows without a bound anybody controls.
120		// The newest are shown, because that is what a reader of a thread
121		// came for.
122		shown := t.Replies
123		if len(shown) > perPage {
124			shown = shown[len(shown)-perPage:]
125		}
126		r := ui.NewTable("by", "said", "block")
127		for _, rep := range shown {
128			r.Row(ui.Addr(rep.Author), ui.Cell(rep.Body), strconv.FormatInt(rep.At, 10))
129		}
130		out += r.String()
131		if len(t.Replies) > perPage {
132			out += md.Italic("Showing the newest " + strconv.Itoa(perPage) + " of " +
133				strconv.Itoa(len(t.Replies)) + " replies.") + "\n"
134		}
135	}
136
137	out += "\n" + ui.Action("💬 Reply", "Reply", "threadID", id.String(), "body", "") +
138		" · " + ui.Action("📌 Boost", "Boost", "threadID", id.String()) + "\n"
139	return out
140}
141
142// listing renders a set of threads. withPage adds the page column, which the
143// index needs and a page view would only repeat.
144func listing(items []th.Listing, withPage bool) string {
145	if len(items) == 0 {
146		return ui.Empty("No threads yet.")
147	}
148	headers := []string{"", "#", "thread", "by", "replies"}
149	if withPage {
150		headers = append(headers, "page")
151	}
152	t := ui.NewTable(headers...)
153	now := runtime.ChainHeight()
154	for _, it := range items {
155		mark := ""
156		if it.Thread.Pinned(now) {
157			mark = "📌"
158		}
159		cells := []string{
160			mark,
161			// The link title is the id, never the body: md.Link escapes
162			// markdown but not a pipe, and a pipe in a table cell opens a
163			// column. ui.Cell is the only thing that rewrites it.
164			md.Link("#"+it.ID.String(), th.ThreadURL(realmPath, it.ID)),
165			ui.Cell(ui.ShortN(it.Thread.Body, th.ExcerptLen, 0)),
166			ui.Addr(it.Thread.Author),
167			strconv.Itoa(len(it.Thread.Replies)),
168		}
169		if withPage {
170			cells = append(cells, ui.Cell(it.Thread.Page))
171		}
172		t.Row(cells...)
173	}
174	return t.String()
175}