Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add r/gnoland/pages #874

Merged
merged 11 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/gno.land/r/gnoland/blog/admin.gno
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func AdminSetAdminAddr(addr std.Address) {
}

func AdminSetInPause(state bool) {
assertIsAdmin()
inPause = state
}

Expand Down
86 changes: 86 additions & 0 deletions examples/gno.land/r/gnoland/pages/admin.gno
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we open an issue for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just check, I think we already have one, it’s related to the thing we said about having a way to skip the need of faucet for local development, where we could imagine having a detection of accounts on the FS to automatically craft a genesis.

Note that we already have a system which patches the genesis to have a dynamic hostname.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we set this value in the init method (constructor)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but not with « auto-genesis »; we need to have the auto-genesis taking an account as argument to simulate the initial uploaded.

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) // XXX: 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)")
}
}
6 changes: 6 additions & 0 deletions examples/gno.land/r/gnoland/pages/gno.mod
Original file line number Diff line number Diff line change
@@ -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
)
21 changes: 21 additions & 0 deletions examples/gno.land/r/gnoland/pages/pages.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package gnopages

import (
"gno.land/p/demo/blog"
)

var b = &blog.Blog{
Title: "Gnoland's Pages",
Prefix: "/r/gnoland/pages:",
}

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 {
return b.Render(path)
}
39 changes: 39 additions & 0 deletions examples/gno.land/r/gnoland/pages/pages_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gnopages

import (
"std"
"strings"
"testing"
)

func TestPackage(t *testing.T) {
std.TestSetOrigCaller(std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"))

author := std.GetOrigCaller()

// by default, lorem ipsum posts
{
got := Render("")
expected := `
# Gnoland's Pages

## [▸ 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)
}
}

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)
}
}
7 changes: 7 additions & 0 deletions examples/gno.land/r/gnoland/pages/util.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gnopages

func checkErr(err error) {
if err != nil {
panic(err)
}
}