render.gno

2.44 Kb ยท 89 lines
 1package cockpit
 2
 3import (
 4	"std"
 5	"strings"
 6
 7	"gno.land/p/demo/mux"
 8	"gno.land/p/demo/ufmt"
 9	"gno.land/p/demo/users"
10	"gno.land/p/moul/md"
11	"gno.land/r/demo/profile"
12	rusers "gno.land/r/demo/users"
13)
14
15const (
16	chainId       = "test5"
17	pkgPath       = "r/zenao/cockpit"
18	userSlug      = "u"
19	usersRealm    = "r/demo/users"
20	ghLinkHint    = "r/gnoland/ghverify?help&__func=RequestVerification"
21	ghProfileHint = "/r/demo/profile:u/"
22	gatewayDomain = "rose-many-bass-859.mypinata.cloud"
23)
24
25func Render(reqPath string) string {
26	router := mux.NewRouter()
27
28	router.HandleFunc("", renderHome)
29	router.HandleFunc(userSlug+"/{aon}", renderUser)
30
31	return router.Render(reqPath)
32}
33
34func renderHome(res *mux.ResponseWriter, req *mux.Request) {
35	res.Write("# Cockpit\n\n")
36	res.Write("See `:" + userSlug + "/{addressOrName}`\n\n")
37	res.Write("Examples:\n")
38	elems := rusers.ListUsersByPrefix("", 20)
39	for _, elem := range elems {
40		res.Write("- [ " + elem + " ](./" + pkgPath + ":" + userSlug + "/@" + elem + ")\n")
41	}
42}
43
44func renderUser(res *mux.ResponseWriter, req *mux.Request) {
45	addr, username, ok := resolveUserBasics(req.GetVar("aon"))
46	if !ok {
47		res.Write("404")
48		return
49	}
50
51	// FIXME: markdown injection, spam
52	res.Write("# " + profile.GetStringField(addr, profile.DisplayName, "Anon") + "\n")
53
54	res.Write(md.Image("Avatar", pfpURL(profile.GetStringField(addr, profile.Avatar, "Anon"))) + "\n")
55
56	if username != "" {
57		res.Write("- Username: [" + username + "](/" + usersRealm + ":" + username + ")\n")
58	} else {
59		res.Write("- Username: [Not registered](/" + usersRealm + "?help&__func=Register)\n")
60	}
61
62	res.Write("- Address: [" + addr.String() + "](https://gnoscan.io/accounts/" + addr.String() + "?chainId=" + chainId + ")\n\n")
63
64	// FIXME: markdown injection, spam
65	res.Write("```\n" + profile.GetStringField(addr, profile.Bio, "No bio") + "\n```\n[Full profile / edit](" + ghProfileHint + addr.String() + ")\n")
66}
67
68func resolveUserBasics(aon string) (std.Address, string, bool) {
69	user := rusers.GetUserByAddressOrName(users.AddressOrName(aon))
70	if user != nil {
71		return user.Address, user.Name, true
72	}
73
74	addr := std.Address(aon)
75	if addr.IsValid() {
76		return addr, "", true
77	}
78
79	return addr, "", false
80}
81
82func pfpURL(uri string) string {
83	if !strings.HasPrefix(uri, "ipfs://") {
84		return uri
85	}
86	withoutScheme := strings.TrimPrefix(uri, "ipfs://")
87	res := ufmt.Sprintf(`https://%s/ipfs/%s?img-width=100&img-height=100&img-fit=cover&img-dpr=2`, gatewayDomain, withoutScheme)
88	return res
89}