render.gno

3.85 Kb · 139 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	out += RenderLatestUsersWidget(-1)
 42
 43	return out
 44}
 45
 46func renderIntroParagraph() string {
 47	out := md.Paragraph("Welcome to the gno.land user registry (v1). Please register a username.")
 48	out += md.Paragraph(`Registering a username grants the registering address the right to deploy packages and realms
 49under that username’s namespace. For example, if an address registers the username ` + md.InlineCode("gnome123") + `, it 
 50will gain permission to deploy packages and realms to package paths with the pattern ` + md.InlineCode("gno.land/{p,r}/gnome123/*") + `.`)
 51
 52	out += md.Paragraph("In V1, usernames must follow these rules, in order to prevent username squatting:")
 53	items := []string{
 54		"Must start with 3 characters",
 55		"Must end with 3 numbers",
 56		"Have a maximum length of 20 characters",
 57		"With the only special character allowed being `_`",
 58	}
 59	out += md.BulletList(items)
 60
 61	out += "\n\n"
 62	out += md.Paragraph("In later versions of the registry, vanity usernames will be allowed through specific mechanisms.")
 63
 64	if !paused {
 65		amount := ufmt.Sprintf("%dugnot", registerPrice)
 66		out += md.H3(ufmt.Sprintf(" [[Click here to register]](%s)", txlink.NewLink("Register").SetSend(amount).URL()))
 67		// XXX: Display registration price adjusting for dynamic GNOT price when it becomes possible.
 68		out += ufmt.Sprintf("Registration price: %f GNOT (%s)\n\n", float64(registerPrice)/1_000_000, amount)
 69	}
 70
 71	out += md.HorizontalRule()
 72	out += "\n\n"
 73
 74	return out
 75}
 76
 77// resolveUser resolves the user based on the path, determining if it's a name or address
 78func resolveUser(path string) (*susers.UserData, bool, bool) {
 79	if std.Address(path).IsValid() {
 80		return susers.ResolveAddress(std.Address(path)), false, false
 81	}
 82
 83	data, isLatest := susers.ResolveName(path)
 84	return data, isLatest, true
 85}
 86
 87// renderUserPage generates the user page based on user data and path
 88func renderUserPage(path string) string {
 89	var out string
 90
 91	// Render single user page
 92	data, isLatest, isName := resolveUser(path)
 93	if data == nil {
 94		out += md.H1("User not found.")
 95		out += "This user does not exist or has been deleted.\n"
 96		return out
 97	}
 98
 99	out += md.H1("User - " + md.InlineCode(data.Name()))
100
101	if isName && !isLatest {
102		out += md.Paragraph(ufmt.Sprintf(
103			"Note: You searched for `%s`, which is a previous name of [`%s`](/u/%s).",
104			path, data.Name(), data.Name()))
105	} else {
106		out += ufmt.Sprintf("Address: %s\n\n", data.Addr().String())
107
108		out += md.H2("Bio")
109		out += profile.GetStringField(data.Addr(), "Bio", "No bio defined.")
110		out += "\n\n"
111		out += ufmt.Sprintf("[Update bio](%s)", txlink.Realm("gno.land/r/demo/profile").Call("SetStringField", "field", "Bio"))
112		out += "\n\n"
113	}
114
115	return out
116}
117
118// RenderLatestUsersWidget renders the latest num registered users
119// For num = -1, maximum number (10) will be displayed
120func RenderLatestUsersWidget(num int) string {
121	size := latestUsers.Size()
122	if size == 0 {
123		return "No registered users."
124	}
125
126	if num > size || num < 0 {
127		num = size
128	}
129
130	entries := latestUsers.Entries()
131	var out string
132
133	for i := size - 1; i >= size-num; i-- {
134		user := entries[i].(string)
135		out += md.BulletItem(md.UserLink(user))
136	}
137
138	return out
139}