From 090f3c8a2250a894f9e7ac3e4cccb6aa27d8b5e5 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Wed, 7 Jun 2023 17:10:55 +0900 Subject: [PATCH 1/9] feat: add r/gnoland/pages Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- examples/gno.land/r/gnoland/blog/admin.gno | 1 + examples/gno.land/r/gnoland/pages/admin.gno | 86 +++++++++++++ examples/gno.land/r/gnoland/pages/pages.gno | 16 +++ .../gno.land/r/gnoland/pages/pages_test.gno | 118 ++++++++++++++++++ examples/gno.land/r/gnoland/pages/util.gno | 7 ++ 5 files changed, 228 insertions(+) create mode 100644 examples/gno.land/r/gnoland/pages/admin.gno create mode 100644 examples/gno.land/r/gnoland/pages/pages.gno create mode 100644 examples/gno.land/r/gnoland/pages/pages_test.gno create mode 100644 examples/gno.land/r/gnoland/pages/util.gno diff --git a/examples/gno.land/r/gnoland/blog/admin.gno b/examples/gno.land/r/gnoland/blog/admin.gno index 14ac6373552..646fe5f155e 100644 --- a/examples/gno.land/r/gnoland/blog/admin.gno +++ b/examples/gno.land/r/gnoland/blog/admin.gno @@ -25,6 +25,7 @@ func AdminSetAdminAddr(addr std.Address) { } func AdminSetInPause(state bool) { + assertIsAdmin() inPause = state } diff --git a/examples/gno.land/r/gnoland/pages/admin.gno b/examples/gno.land/r/gnoland/pages/admin.gno new file mode 100644 index 00000000000..625b5d025ff --- /dev/null +++ b/examples/gno.land/r/gnoland/pages/admin.gno @@ -0,0 +1,86 @@ +package gnopages + +import ( + "std" + "strings" + + "gno.land/p/demo/avl" +) + +var ( + adminAddr std.Address + moderatorList avl.Tree + inPause bool +) + +func init() { + // adminAddr = std.GetOrigCaller() // FIXME: find a way to use this from the main's genesis. + adminAddr = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq" +} + +func AdminSetAdminAddr(addr std.Address) { + assertIsAdmin() + adminAddr = addr +} + +func AdminSetInPause(state bool) { + assertIsAdmin() + inPause = state +} + +func AdminAddModerator(addr std.Address) { + assertIsAdmin() + moderatorList.Set(addr.String(), true) +} + +func AdminRemoveModerator(addr std.Address) { + assertIsAdmin() + moderatorList.Set(addr.String(), false) // FIXME: delete instead? +} + +func ModAddPost(slug, title, body, tags string) { + assertIsModerator() + + caller := std.GetOrigCaller() + tagList := strings.Split(tags, ",") + err := b.NewPost(caller, slug, title, body, tagList) + checkErr(err) +} + +func ModEditPost(slug, title, body, tags string) { + assertIsModerator() + + tagList := strings.Split(tags, ",") + err := b.GetPost(slug).Update(title, body, tagList) + checkErr(err) +} + +func isAdmin(addr std.Address) bool { + return addr == adminAddr +} + +func isModerator(addr std.Address) bool { + _, found := moderatorList.Get(addr.String()) + return found +} + +func assertIsAdmin() { + caller := std.GetOrigCaller() + if !isAdmin(caller) { + panic("access restricted.") + } +} + +func assertIsModerator() { + caller := std.GetOrigCaller() + if isAdmin(caller) || isModerator(caller) { + return + } + panic("access restricted") +} + +func assertNotInPause() { + if inPause { + panic("access restricted (pause)") + } +} diff --git a/examples/gno.land/r/gnoland/pages/pages.gno b/examples/gno.land/r/gnoland/pages/pages.gno new file mode 100644 index 00000000000..92e020c6681 --- /dev/null +++ b/examples/gno.land/r/gnoland/pages/pages.gno @@ -0,0 +1,16 @@ +package gnopages + +import ( + "std" + + "gno.land/p/demo/blog" +) + +var b = &blog.Blog{ + Title: "Gnoland's Pages", + Prefix: "/r/gnoland/pages:", +} + +func Render(path string) string { + return b.Render(path) +} diff --git a/examples/gno.land/r/gnoland/pages/pages_test.gno b/examples/gno.land/r/gnoland/pages/pages_test.gno new file mode 100644 index 00000000000..c42a6654223 --- /dev/null +++ b/examples/gno.land/r/gnoland/pages/pages_test.gno @@ -0,0 +1,118 @@ +package gnopages + +import ( + "std" + "strings" + "testing" +) + +func TestPackage(t *testing.T) { + std.TestSetOrigCaller(std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")) + + author := std.GetOrigCaller() + + // by default, no posts. + { + got := Render("") + expected := ` +# Gnoland's Pages + +No posts. +` + assertMDEquals(t, got, expected) + } + + // create two posts, list post. + { + ModAddPost("slug1", "title1", "body1", "tag1,tag2") + ModAddPost("slug2", "title2", "body2", "tag1,tag3") + got := Render("") + expected := ` +# Gnoland's Pages + +## [▸ title1](/r/gnoland/pages:p/slug1) + +## [▸ title2](/r/gnoland/pages:p/slug2) +` + assertMDEquals(t, got, expected) + } + + // view post. + { + got := Render("p/slug2") + expected := ` +# [Gnoland's Pages](/r/gnoland/pages:) / p / title2 + +body2 + +[#tag1](/r/gnoland/pages:t/tag1) [#tag3](/r/gnoland/pages:t/tag3) + +by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq on 2009-02-13 11:31pm UTC +` + assertMDEquals(t, got, expected) + } + + // list by tags. + { + got := Render("t/invalid") + expected := "# [Gnoland's Pages](/r/gnoland/pages:) / t / invalid\n\nNo posts." + assertMDEquals(t, got, expected) + + got = Render("t/tag2") + expected = ` +# [Gnoland's Pages](/r/gnoland/pages:) / t / tag2 + +## [▸ title1](/r/gnoland/pages:p/slug1) +` + assertMDEquals(t, got, expected) + } + + // edit post. + { + ModEditPost("slug2", "title2++", "body2++", "tag1,tag4") + got := Render("p/slug2") + expected := ` +# [Gnoland's Pages](/r/gnoland/pages:) / p / title2++ + +body2++ + +[#tag1](/r/gnoland/pages:t/tag1) [#tag4](/r/gnoland/pages:t/tag4) + +by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq on 2009-02-13 11:31pm UTC +` + assertMDEquals(t, got, expected) + } + + // TODO: pagination. + // TODO: ?format=... + + // all 404s + { + notFoundPaths := []string{ + "p/slug3", + "p", + "p/", + "x/x", + "t", + "t/", + "/", + "p/slug1/", + } + for _, notFoundPath := range notFoundPaths { + got := Render(notFoundPath) + expected := "404" + if got != expected { + t.Errorf("path %q: expected %q, got %q.", notFoundPath, expected, got) + } + } + } +} + +func assertMDEquals(t *testing.T, got, expected string) { + t.Helper() + expected = strings.TrimSpace(expected) + got = strings.TrimSpace(got) + if expected != got { + t.Errorf("invalid render output.\nexpected %q.\ngot %q.", expected, got) + } +} diff --git a/examples/gno.land/r/gnoland/pages/util.gno b/examples/gno.land/r/gnoland/pages/util.gno new file mode 100644 index 00000000000..a267021287e --- /dev/null +++ b/examples/gno.land/r/gnoland/pages/util.gno @@ -0,0 +1,7 @@ +package gnopages + +func checkErr(err error) { + if err != nil { + panic(err) + } +} From 163d944054c8b1e888051ef283762e94d8e3ca9d Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Wed, 7 Jun 2023 17:25:37 +0900 Subject: [PATCH 2/9] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- examples/gno.land/r/gnoland/pages/pages.gno | 5 ++ .../gno.land/r/gnoland/pages/pages_test.gno | 89 +------------------ 2 files changed, 8 insertions(+), 86 deletions(-) diff --git a/examples/gno.land/r/gnoland/pages/pages.gno b/examples/gno.land/r/gnoland/pages/pages.gno index 92e020c6681..5d58e1283f9 100644 --- a/examples/gno.land/r/gnoland/pages/pages.gno +++ b/examples/gno.land/r/gnoland/pages/pages.gno @@ -11,6 +11,11 @@ var b = &blog.Blog{ Prefix: "/r/gnoland/pages:", } +func init(){ + _ = b.NewPost("", "gor", "Game of Realms", "Lorem Ipsum", nil) + _ = b.NewPost("", "events", "Events", "Lorem Ipsum", nil) +} + func Render(path string) string { return b.Render(path) } diff --git a/examples/gno.land/r/gnoland/pages/pages_test.gno b/examples/gno.land/r/gnoland/pages/pages_test.gno index c42a6654223..3dc4b3ac95d 100644 --- a/examples/gno.land/r/gnoland/pages/pages_test.gno +++ b/examples/gno.land/r/gnoland/pages/pages_test.gno @@ -11,101 +11,18 @@ func TestPackage(t *testing.T) { author := std.GetOrigCaller() - // by default, no posts. + // by default, lorem ipsum posts { got := Render("") expected := ` # Gnoland's Pages -No posts. -` - assertMDEquals(t, got, expected) - } - - // create two posts, list post. - { - ModAddPost("slug1", "title1", "body1", "tag1,tag2") - ModAddPost("slug2", "title2", "body2", "tag1,tag3") - got := Render("") - expected := ` -# Gnoland's Pages - -## [▸ title1](/r/gnoland/pages:p/slug1) - -## [▸ title2](/r/gnoland/pages:p/slug2) -` - assertMDEquals(t, got, expected) - } - - // view post. - { - got := Render("p/slug2") - expected := ` -# [Gnoland's Pages](/r/gnoland/pages:) / p / title2 - -body2 - -[#tag1](/r/gnoland/pages:t/tag1) [#tag3](/r/gnoland/pages:t/tag3) +## [▸ Events](/r/gnoland/pages:p/events) -by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq on 2009-02-13 11:31pm UTC +## [▸ Game of Realms](/r/gnoland/pages:p/gor) ` assertMDEquals(t, got, expected) } - - // list by tags. - { - got := Render("t/invalid") - expected := "# [Gnoland's Pages](/r/gnoland/pages:) / t / invalid\n\nNo posts." - assertMDEquals(t, got, expected) - - got = Render("t/tag2") - expected = ` -# [Gnoland's Pages](/r/gnoland/pages:) / t / tag2 - -## [▸ title1](/r/gnoland/pages:p/slug1) -` - assertMDEquals(t, got, expected) - } - - // edit post. - { - ModEditPost("slug2", "title2++", "body2++", "tag1,tag4") - got := Render("p/slug2") - expected := ` -# [Gnoland's Pages](/r/gnoland/pages:) / p / title2++ - -body2++ - -[#tag1](/r/gnoland/pages:t/tag1) [#tag4](/r/gnoland/pages:t/tag4) - -by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq on 2009-02-13 11:31pm UTC -` - assertMDEquals(t, got, expected) - } - - // TODO: pagination. - // TODO: ?format=... - - // all 404s - { - notFoundPaths := []string{ - "p/slug3", - "p", - "p/", - "x/x", - "t", - "t/", - "/", - "p/slug1/", - } - for _, notFoundPath := range notFoundPaths { - got := Render(notFoundPath) - expected := "404" - if got != expected { - t.Errorf("path %q: expected %q, got %q.", notFoundPath, expected, got) - } - } - } } func assertMDEquals(t *testing.T, got, expected string) { From 5c9f42be0d6f255313501f46317125e20b1db336 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Wed, 7 Jun 2023 17:54:43 +0900 Subject: [PATCH 3/9] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- gno.land/cmd/gnoland/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/gno.land/cmd/gnoland/main.go b/gno.land/cmd/gnoland/main.go index 0ebc3d7058a..abb0210fa28 100644 --- a/gno.land/cmd/gnoland/main.go +++ b/gno.land/cmd/gnoland/main.go @@ -244,6 +244,7 @@ func makeGenesisDoc( "r/demo/banktest", "r/demo/types", "r/demo/markdown_test", + "r/gnoland/pages", "r/gnoland/blog", "r/gnoland/faucet", "r/system/validators", From c7251fc527d8e65d849ffda1ec6b720dde195833 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Wed, 7 Jun 2023 18:01:25 +0900 Subject: [PATCH 4/9] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- examples/gno.land/r/gnoland/pages/pages.gno | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gno.land/r/gnoland/pages/pages.gno b/examples/gno.land/r/gnoland/pages/pages.gno index 5d58e1283f9..6113221a654 100644 --- a/examples/gno.land/r/gnoland/pages/pages.gno +++ b/examples/gno.land/r/gnoland/pages/pages.gno @@ -11,7 +11,7 @@ var b = &blog.Blog{ Prefix: "/r/gnoland/pages:", } -func init(){ +func init() { _ = b.NewPost("", "gor", "Game of Realms", "Lorem Ipsum", nil) _ = b.NewPost("", "events", "Events", "Lorem Ipsum", nil) } From 6b4f6ac92a1ef16e5b95dea56c50cfb52baa927e Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Wed, 7 Jun 2023 18:08:40 +0900 Subject: [PATCH 5/9] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- examples/gno.land/r/gnoland/pages/pages.gno | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/gno.land/r/gnoland/pages/pages.gno b/examples/gno.land/r/gnoland/pages/pages.gno index 6113221a654..fd54efda05e 100644 --- a/examples/gno.land/r/gnoland/pages/pages.gno +++ b/examples/gno.land/r/gnoland/pages/pages.gno @@ -1,8 +1,6 @@ package gnopages import ( - "std" - "gno.land/p/demo/blog" ) From 073dcaef2ba48f7df76e095538af792eb76c5a8a Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Thu, 8 Jun 2023 21:27:50 +0900 Subject: [PATCH 6/9] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- examples/gno.land/r/gnoland/pages/pages.gno | 2 ++ examples/gno.land/r/gnoland/pages/pages_test.gno | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/examples/gno.land/r/gnoland/pages/pages.gno b/examples/gno.land/r/gnoland/pages/pages.gno index fd54efda05e..dbc3d855880 100644 --- a/examples/gno.land/r/gnoland/pages/pages.gno +++ b/examples/gno.land/r/gnoland/pages/pages.gno @@ -12,6 +12,8 @@ var b = &blog.Blog{ func init() { _ = b.NewPost("", "gor", "Game of Realms", "Lorem Ipsum", nil) _ = b.NewPost("", "events", "Events", "Lorem Ipsum", nil) + _ = b.NewPost("", "tokenomics", "Tokenomics", "Lorem Ipsum", nil) + _ = b.NewPost("", "start", "Getting Started", "Lorem Ipsum", nil) } func Render(path string) string { diff --git a/examples/gno.land/r/gnoland/pages/pages_test.gno b/examples/gno.land/r/gnoland/pages/pages_test.gno index 3dc4b3ac95d..5ba9ea2ca0b 100644 --- a/examples/gno.land/r/gnoland/pages/pages_test.gno +++ b/examples/gno.land/r/gnoland/pages/pages_test.gno @@ -20,6 +20,10 @@ func TestPackage(t *testing.T) { ## [▸ Events](/r/gnoland/pages:p/events) ## [▸ Game of Realms](/r/gnoland/pages:p/gor) + +## [▸ Getting Started](/r/gnoland/pages:p/start) + +## [▸ Tokenomics](/r/gnoland/pages:p/tokenomics) ` assertMDEquals(t, got, expected) } From 4deba8cad1d97321e80bb18c3dcaaf44a06f4528 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Tue, 20 Jun 2023 17:40:21 +0200 Subject: [PATCH 7/9] chore: add gno.mod Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- examples/gno.land/r/gnoland/pages/gno.mod | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 examples/gno.land/r/gnoland/pages/gno.mod diff --git a/examples/gno.land/r/gnoland/pages/gno.mod b/examples/gno.land/r/gnoland/pages/gno.mod new file mode 100644 index 00000000000..0f5c4076509 --- /dev/null +++ b/examples/gno.land/r/gnoland/pages/gno.mod @@ -0,0 +1,6 @@ +module gno.land/r/gnoland/pages + +require ( + "gno.land/p/demo/avl" v0.0.0-latest + "gno.land/p/demo/blog" v0.0.0-latest +) From 7602ad6f4dbb368f339611942772d21ac8fae2c6 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Fri, 21 Jul 2023 20:17:20 +0200 Subject: [PATCH 8/9] Update examples/gno.land/r/gnoland/pages/admin.gno --- examples/gno.land/r/gnoland/pages/admin.gno | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gno.land/r/gnoland/pages/admin.gno b/examples/gno.land/r/gnoland/pages/admin.gno index 625b5d025ff..39fba6d3274 100644 --- a/examples/gno.land/r/gnoland/pages/admin.gno +++ b/examples/gno.land/r/gnoland/pages/admin.gno @@ -35,7 +35,7 @@ func AdminAddModerator(addr std.Address) { func AdminRemoveModerator(addr std.Address) { assertIsAdmin() - moderatorList.Set(addr.String(), false) // FIXME: delete instead? + moderatorList.Set(addr.String(), false) // XXX: delete instead? } func ModAddPost(slug, title, body, tags string) { From fc3ce83c21fc1c1ea174d35f7a6a455c1b2212c6 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Sat, 29 Jul 2023 14:45:10 +0200 Subject: [PATCH 9/9] chore: fix tests --- .../gno.land/r/gnoland/pages/pages_test.gno | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/gno.land/r/gnoland/pages/pages_test.gno b/examples/gno.land/r/gnoland/pages/pages_test.gno index 5ba9ea2ca0b..1a43153e2c8 100644 --- a/examples/gno.land/r/gnoland/pages/pages_test.gno +++ b/examples/gno.land/r/gnoland/pages/pages_test.gno @@ -17,13 +17,27 @@ func TestPackage(t *testing.T) { expected := ` # Gnoland's Pages -## [▸ Events](/r/gnoland/pages:p/events) +
-## [▸ Game of Realms](/r/gnoland/pages:p/gor) +## [Events](/r/gnoland/pages:p/events) +**[Learn More](/r/gnoland/pages:p/events)** -## [▸ Getting Started](/r/gnoland/pages:p/start) +
-## [▸ Tokenomics](/r/gnoland/pages:p/tokenomics) +## [Game of Realms](/r/gnoland/pages:p/gor) +**[Learn More](/r/gnoland/pages:p/gor)** + +
+ +## [Getting Started](/r/gnoland/pages:p/start) +**[Learn More](/r/gnoland/pages:p/start)** + +
+ +## [Tokenomics](/r/gnoland/pages:p/tokenomics) +**[Learn More](/r/gnoland/pages:p/tokenomics)** + +
` assertMDEquals(t, got, expected) }