render.gno

3.46 Kb · 122 lines
  1package users
  2
  3import (
  4	"std"
  5
  6	"gno.land/p/demo/ufmt"
  7	"gno.land/p/moul/md"
  8	"gno.land/p/moul/realmpath"
  9	"gno.land/p/moul/txlink"
 10
 11	"gno.land/r/demo/profile"
 12	susers "gno.land/r/sys/users"
 13)
 14
 15func Render(path string) string {
 16	req := realmpath.Parse(path)
 17
 18	if req.Path == "" {
 19		return renderHomePage()
 20	}
 21
 22	// Otherwise, render the user page
 23	return renderUserPage(req.Path)
 24}
 25
 26func renderHomePage() string {
 27	var out string
 28
 29	out += "# gno.land user registry\n"
 30
 31	if paused {
 32		out += md.HorizontalRule()
 33		out += md.H2("This realm is paused.")
 34		out += md.Paragraph("Check out [`gno.land/r/gnoland/users`](/r/gnoland/users) for newer versions of the registry.")
 35		out += md.HorizontalRule()
 36	}
 37
 38	out += renderIntroParagraph()
 39
 40	out += md.H2("Latest registrations")
 41	entries := latestUsers.Entries()
 42	if len(entries) == 0 {
 43		out += "No registered users."
 44	}
 45
 46	for i := len(entries) - 1; i >= 0; i-- {
 47		user := entries[i].(string)
 48		out += ufmt.Sprintf("- User [%s](/r/gnoland/users/v1:%s)\n", md.Bold(user), user)
 49	}
 50
 51	return out
 52}
 53
 54func renderIntroParagraph() string {
 55	out := md.Paragraph("Welcome to the gno.land user registry (v1). Please register a username.")
 56	out += md.Paragraph(`Registering a username grants the registering address the right to deploy packages and realms
 57under that username’s namespace. For example, if an address registers the username ` + md.InlineCode("gnome123") + `, it 
 58will gain permission to deploy packages and realms to package paths with the pattern ` + md.InlineCode("gno.land/{p,r}/gnome123/*") + `.`)
 59
 60	out += md.Paragraph("In V1, usernames must follow these rules, in order to prevent username squatting:")
 61	items := []string{
 62		"Must start with 3 characters",
 63		"Must end with 3 numbers",
 64		"Have a maximum length of 20 characters",
 65		"With the only special character allowed being `_`",
 66	}
 67	out += md.BulletList(items)
 68
 69	out += "\n\n"
 70	out += md.Paragraph("In later versions of the registry, vanity usernames will be allowed through specific mechanisms.")
 71
 72	if !paused {
 73		out += md.H3(ufmt.Sprintf(" [[Click here to register]](%s)", txlink.Call("Register")))
 74		out += ufmt.Sprintf("Registration price: %f GNOT (%dugnot)\n\n", float64(registerPrice)/1_000_000, registerPrice)
 75	}
 76
 77	out += md.HorizontalRule()
 78	out += "\n\n"
 79
 80	return out
 81}
 82
 83// resolveUser resolves the user based on the path, determining if it's a name or address
 84func resolveUser(path string) (*susers.UserData, bool, bool) {
 85	if std.Address(path).IsValid() {
 86		return susers.ResolveAddress(std.Address(path)), false, false
 87	}
 88
 89	data, isLatest := susers.ResolveName(path)
 90	return data, isLatest, true
 91}
 92
 93// renderUserPage generates the user page based on user data and path
 94func renderUserPage(path string) string {
 95	var out string
 96
 97	// Render single user page
 98	data, isLatest, isName := resolveUser(path)
 99	if data == nil {
100		out += md.H1("User not found.")
101		out += "This user does not exist or has been deleted.\n"
102		return out
103	}
104
105	out += md.H1("User - " + md.InlineCode(data.Name()))
106
107	if isName && !isLatest {
108		out += md.Paragraph(ufmt.Sprintf(
109			"Note: You searched for `%s`, which is a previous name of [`%s`](/r/gnoland/users/v1:%s).",
110			path, data.Name(), data.Name()))
111	} else {
112		out += ufmt.Sprintf("Address: %s\n\n", data.Addr().String())
113
114		out += md.H2("Bio")
115		out += profile.GetStringField(data.Addr(), "Bio", "No bio defined.")
116		out += "\n\n"
117		out += ufmt.Sprintf("[Update bio](%s)", txlink.Realm("gno.land/r/demo/profile").Call("SetStringField", "field", "Bio"))
118		out += "\n\n"
119	}
120
121	return out
122}