render.gno
3.62 Kb · 124 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 amount := ufmt.Sprintf("%dugnot", registerPrice)
74 out += md.H3(ufmt.Sprintf(" [[Click here to register]](%s)", txlink.NewLink("Register").SetSend(amount).URL()))
75 // XXX: Display registration price adjusting for dynamic GNOT price when it becomes possible.
76 out += ufmt.Sprintf("Registration price: %f GNOT (%s)\n\n", float64(registerPrice)/1_000_000, amount)
77 }
78
79 out += md.HorizontalRule()
80 out += "\n\n"
81
82 return out
83}
84
85// resolveUser resolves the user based on the path, determining if it's a name or address
86func resolveUser(path string) (*susers.UserData, bool, bool) {
87 if std.Address(path).IsValid() {
88 return susers.ResolveAddress(std.Address(path)), false, false
89 }
90
91 data, isLatest := susers.ResolveName(path)
92 return data, isLatest, true
93}
94
95// renderUserPage generates the user page based on user data and path
96func renderUserPage(path string) string {
97 var out string
98
99 // Render single user page
100 data, isLatest, isName := resolveUser(path)
101 if data == nil {
102 out += md.H1("User not found.")
103 out += "This user does not exist or has been deleted.\n"
104 return out
105 }
106
107 out += md.H1("User - " + md.InlineCode(data.Name()))
108
109 if isName && !isLatest {
110 out += md.Paragraph(ufmt.Sprintf(
111 "Note: You searched for `%s`, which is a previous name of [`%s`](/r/gnoland/users/v1:%s).",
112 path, data.Name(), data.Name()))
113 } else {
114 out += ufmt.Sprintf("Address: %s\n\n", data.Addr().String())
115
116 out += md.H2("Bio")
117 out += profile.GetStringField(data.Addr(), "Bio", "No bio defined.")
118 out += "\n\n"
119 out += ufmt.Sprintf("[Update bio](%s)", txlink.Realm("gno.land/r/demo/profile").Call("SetStringField", "field", "Bio"))
120 out += "\n\n"
121 }
122
123 return out
124}