Playground
admin.gno
package blog
import (
"chain/runtime/unsafe"
"errors"
"strings"
"gno.land/p/nt/bptree/v0"
"gno.land/p/nt/ufmt/v0"
"gno.land/r/gov/dao"
)
var (
errNotAdmin = errors.New("access restricted: not admin")
errNotModerator = errors.New("access restricted: not moderator")
errNotCommenter = errors.New("access restricted: not commenter")
)
var (
adminAddr = address("g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx") // govdao t1 multisig
moderatorList = bptree.NewBPTree32()
commenterList = bptree.NewBPTree32()
inPause bool
)
func AdminSetAdminAddr(_ realm, addr address) {
assertIsAdmin()
adminAddr = addr
}
func AdminSetInPause(_ realm, state bool) {
assertIsAdmin()
inPause = state
}
func AdminAddModerator(_ realm, addr address) {
assertIsAdmin()
moderatorList.Set(addr.String(), true)
}
func AdminRemoveModerator(_ realm, addr address) {
assertIsAdmin()
moderatorList.Set(addr.String(), false) // entry kept as a revocation record; isModerator checks the value
}
func NewPostProposalRequest(cur realm, slug, title, body, publicationDate, authors, tags string) dao.ProposalRequest {
caller := cur.Previous().Address()
e := dao.NewSimpleExecutor(0, cur,
func(realm) error {
addPost(caller, slug, title, body, publicationDate, authors, tags)
return nil
},
ufmt.Sprintf("- Post Title: %v\n- Post Publication Date: %v\n- Authors: %v\n- Tags: %v", title, publicationDate, authors, tags),
)
return dao.NewProposalRequest(
"Add new post to gnoland blog",
"This propoposal is looking to add a new post to gnoland blog",
e,
)
}
func ModAddPost(_ realm, slug, title, body, publicationDate, authors, tags string) {
assertIsModerator()
caller := unsafe.OriginCaller()
addPost(caller, slug, title, body, publicationDate, authors, tags)
}
func addPost(caller address, slug, title, body, publicationDate, authors, tags string) {
var tagList []string
if tags != "" {
tagList = strings.Split(tags, ",")
}
var authorList []string
if authors != "" {
authorList = strings.Split(authors, ",")
}
err := b.NewPost(caller, slug, title, body, publicationDate, authorList, tagList)
checkErr(err)
}
func ModEditPost(_ realm, slug, title, body, publicationDate, authors, tags string) {
assertIsModerator()
tagList := strings.Split(tags, ",")
authorList := strings.Split(authors, ",")
err := b.GetPost(slug).Update(title, body, publicationDate, authorList, tagList)
checkErr(err)
}
func ModRemovePost(_ realm, slug string) {
assertIsModerator()
b.RemovePost(slug)
}
func ModAddCommenter(_ realm, addr address) {
assertIsModerator()
commenterList.Set(addr.String(), true)
}
func ModDelCommenter(_ realm, addr address) {
assertIsModerator()
commenterList.Set(addr.String(), false) // entry kept as a revocation record; isCommenter checks the value
}
func ModDelComment(_ realm, slug string, index int) {
assertIsModerator()
err := b.GetPost(slug).DeleteComment(index)
checkErr(err)
}
func isAdmin(addr address) bool {
return addr == adminAddr
}
func isModerator(addr address) bool {
// Removed moderators stay in the list with a false value, so the
// stored value must be checked, not just key presence.
active, _ := moderatorList.Get(addr.String()).(bool)
return active
}
func isCommenter(addr address) bool {
// Removed commenters stay in the list with a false value, so the
// stored value must be checked, not just key presence.
active, _ := commenterList.Get(addr.String()).(bool)
return active
}
func assertIsAdmin() {
caller := unsafe.OriginCaller()
if !isAdmin(caller) {
panic(errNotAdmin.Error())
}
}
func assertIsModerator() {
caller := unsafe.OriginCaller()
if isAdmin(caller) || isModerator(caller) {
return
}
panic(errNotModerator.Error())
}
func assertIsCommenter() {
caller := unsafe.OriginCaller()
if isAdmin(caller) || isModerator(caller) || isCommenter(caller) {
return
}
panic(errNotCommenter.Error())
}
func assertNotInPause() {
if inPause {
panic("access restricted (pause)")
}
}
admin_test.gno
package blog
import (
"testing"
"gno.land/p/gnoland/blog/v0"
"gno.land/p/nt/bptree/v0"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
"gno.land/p/nt/urequire/v0"
)
// cur is a zero-value realm used as a placeholder when forwarding to
// uassert/urequire dispatch helpers that gained an `rlm realm` param.
// These tests pass `func()` callbacks (no crossing inside the callback),
// so rlm is ignored — a nil realm here is safe.
var cur realm
// clearState wipes the global state between test calls
func clearState(t *testing.T) {
t.Helper()
b = &blog.Blog{
Title: "Gno.land's blog",
Prefix: "/r/gnoland/blog:",
}
adminAddr = address("g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx")
inPause = false
moderatorList = bptree.NewBPTree32()
commenterList = bptree.NewBPTree32()
}
func TestBlog_AdminControls(cur realm, t *testing.T) {
t.Run("non-admin call", func(t *testing.T) {
clearState(t)
nonAdmin := testutils.TestAddress("bob")
testing.SetOriginCaller(nonAdmin)
testing.SetRealm(testing.NewUserRealm(nonAdmin))
uassert.AbortsWithMessage(t, cur, errNotAdmin.Error(), func() {
AdminSetInPause(cross(cur), true)
})
})
t.Run("pause toggled", func(t *testing.T) {
clearState(t)
testing.SetOriginCaller(adminAddr)
testing.SetRealm(testing.NewUserRealm(adminAddr))
uassert.NotAborts(t, cur, func() {
AdminSetInPause(cross(cur), true)
})
uassert.True(t, inPause)
})
t.Run("admin set", func(t *testing.T) {
clearState(t)
// Set the new admin
var (
oldAdmin = adminAddr
newAdmin = testutils.TestAddress("alice")
)
testing.SetRealm(testing.NewUserRealm(adminAddr))
AdminSetAdminAddr(cross(cur), newAdmin)
uassert.Equal(t, adminAddr, newAdmin)
// Make sure the old admin can't do anything
testing.SetOriginCaller(oldAdmin)
testing.SetRealm(testing.NewUserRealm(oldAdmin))
uassert.AbortsWithMessage(t, cur, errNotAdmin.Error(), func() {
AdminSetInPause(cross(cur), false)
})
})
}
func TestBlog_AddRemoveModerator(cur realm, t *testing.T) {
clearState(t)
mod := testutils.TestAddress("mod")
// Add the moderator
testing.SetOriginCaller(adminAddr)
testing.SetRealm(testing.NewUserRealm(adminAddr))
AdminAddModerator(cross(cur), mod)
urequire.True(t, moderatorList.Has(mod.String()))
// Remove the moderator
AdminRemoveModerator(cross(cur), mod)
// Make sure the moderator is disabled
isMod := moderatorList.Get(mod.String())
uassert.NotNil(t, isMod)
uassert.False(t, isMod.(bool))
}
func TestBlog_AddCommenter(cur realm, t *testing.T) {
clearState(t)
var (
mod = testutils.TestAddress("mod")
commenter = testutils.TestAddress("comm")
rand = testutils.TestAddress("rand")
)
// Appoint the moderator
testing.SetOriginCaller(adminAddr)
testing.SetRealm(testing.NewUserRealm(adminAddr))
AdminAddModerator(cross(cur), mod)
// Add a commenter as a mod
testing.SetOriginCaller(mod)
testing.SetRealm(testing.NewUserRealm(mod))
ModAddCommenter(cross(cur), commenter)
uassert.True(t, commenterList.Has(commenter.String()))
// Make sure a non-mod can't add commenters
testing.SetOriginCaller(rand)
testing.SetRealm(testing.NewUserRealm(rand))
uassert.AbortsWithMessage(t, cur, errNotModerator.Error(), func() {
ModAddCommenter(cross(cur), testutils.TestAddress("evil"))
})
// Remove a commenter
testing.SetOriginCaller(mod)
testing.SetRealm(testing.NewUserRealm(mod))
ModDelCommenter(cross(cur), commenter)
active := commenterList.Get(commenter.String())
uassert.False(t, active.(bool))
}
func TestBlog_ManagePost(cur realm, t *testing.T) {
clearState(t)
var (
mod = testutils.TestAddress("mod")
slug = "slug"
)
// Appoint the moderator
testing.SetOriginCaller(adminAddr)
testing.SetRealm(testing.NewUserRealm(adminAddr))
AdminAddModerator(cross(cur), mod)
// Add the post
testing.SetOriginCaller(mod)
testing.SetRealm(testing.NewUserRealm(mod))
ModAddPost(
cross(cur),
slug, "title", "body", "2022-05-20T13:17:22Z", "moul", "tag",
)
// Make sure the post is present
uassert.NotNil(t, b.GetPost(slug))
// Remove the post
ModRemovePost(cross(cur), slug)
uassert.TypedNil(t, b.GetPost(slug))
}
func TestBlog_ManageComment(cur realm, t *testing.T) {
clearState(t)
var (
slug = "slug"
mod = testutils.TestAddress("mod")
commenter = testutils.TestAddress("comm")
)
// Appoint the moderator
testing.SetOriginCaller(adminAddr)
testing.SetRealm(testing.NewUserRealm(adminAddr))
AdminAddModerator(cross(cur), mod)
// Add a commenter as a mod
testing.SetOriginCaller(mod)
testing.SetRealm(testing.NewUserRealm(mod))
ModAddCommenter(cross(cur), commenter)
uassert.True(t, commenterList.Has(commenter.String()))
// Add the post
testing.SetOriginCaller(mod)
testing.SetRealm(testing.NewUserRealm(mod))
ModAddPost(
cross(cur),
slug, "title", "body", "2022-05-20T13:17:22Z", "moul", "tag",
)
// Make sure the post is present
uassert.NotNil(t, b.GetPost(slug))
// Add the comment
testing.SetOriginCaller(commenter)
testing.SetRealm(testing.NewUserRealm(commenter))
uassert.NotAborts(t, cur, func() {
AddComment(cross(cur), slug, "comment")
})
// Delete the comment
testing.SetOriginCaller(mod)
testing.SetRealm(testing.NewUserRealm(mod))
uassert.NotAborts(t, cur, func() {
ModDelComment(cross(cur), slug, 0)
})
}
gnoblog.gno
package blog
import (
"chain/runtime/unsafe"
"gno.land/p/gnoland/blog/v0"
)
var b = &blog.Blog{
Title: "Gno.land's blog",
Prefix: "/r/gnoland/blog:",
}
func AddComment(_ realm, postSlug, comment string) {
assertIsCommenter()
assertNotInPause()
caller := unsafe.OriginCaller()
err := b.GetPost(postSlug).AddComment(caller, comment)
checkErr(err)
}
func Render(path string) string {
return b.Render(path)
}
func RenderLastPostsWidget(limit int) string {
return b.RenderLastPostsWidget(limit)
}
func PostExists(slug string) bool {
if b.GetPost(slug) == nil {
return false
}
return true
}
gnoblog_test.gno
package blog
import (
"strings"
"testing"
)
func TestPackage(cur realm, t *testing.T) {
clearState(t)
testing.SetOriginCaller(adminAddr)
testing.SetRealm(testing.NewUserRealm(adminAddr))
// by default, no posts.
{
got := Render("")
expected := `
# Gno.land's blog
No posts.
`
assertMDEquals(t, got, expected)
}
// create two posts, list post.
{
ModAddPost(cross(cur), "slug1", "title1", "body1", "2022-05-20T13:17:22Z", "moul", "tag1,tag2")
ModAddPost(cross(cur), "slug2", "title2", "body2", "2022-05-20T13:17:23Z", "moul", "tag1,tag3")
got := Render("")
expected := `
# Gno.land's blog
<gno-columns>
### [title2](/r/gnoland/blog:p/slug2)
20 May 2022
<gno-columns-sep>
### [title1](/r/gnoland/blog:p/slug1)
20 May 2022
<gno-columns-sep>
</gno-columns>
`
assertMDEquals(t, got, expected)
}
// view post.
{
got := Render("p/slug2")
expected := `
<main class='gno-tmpl-page'>
# title2
body2
---
Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)
Written by moul on 20 May 2022
Published by g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx to Gno.land's blog
---
<details><summary>Comment section</summary>
</details>
</main>
`
assertMDEquals(t, got, expected)
}
// list by tags.
{
got := Render("t/invalid")
expected := "# [Gno.land's blog](/r/gnoland/blog:) / t / invalid\n\nNo posts."
assertMDEquals(t, got, expected)
got = Render("t/tag2")
expected = `
# [Gno.land's blog](/r/gnoland/blog:) / t / tag2
### [title1](/r/gnoland/blog:p/slug1)
20 May 2022
`
assertMDEquals(t, got, expected)
}
// add comments.
{
AddComment(cross(cur), "slug1", "comment1")
AddComment(cross(cur), "slug2", "comment2")
AddComment(cross(cur), "slug1", "comment3")
AddComment(cross(cur), "slug2", "comment4")
AddComment(cross(cur), "slug1", "comment5")
got := Render("p/slug2")
expected := `<main class='gno-tmpl-page'>
# title2
body2
---
Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag3](/r/gnoland/blog:t/tag3)
Written by moul on 20 May 2022
Published by g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx to Gno.land's blog
---
<details><summary>Comment section</summary>
<h5>comment4
</h5><h6>by g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx on 13 Feb 09 23:31 UTC</h6>
---
<h5>comment2
</h5><h6>by g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx on 13 Feb 09 23:31 UTC</h6>
---
</details>
</main>
`
assertMDEquals(t, got, expected)
}
// edit post.
{
oldTitle := "title2"
oldDate := "2022-05-20T13:17:23Z"
ModEditPost(cur, "slug2", oldTitle, "body2++", oldDate, "manfred", "tag1,tag4")
got := Render("p/slug2")
expected := `<main class='gno-tmpl-page'>
# title2
body2++
---
Tags: [#tag1](/r/gnoland/blog:t/tag1) [#tag4](/r/gnoland/blog:t/tag4)
Written by manfred on 20 May 2022
Published by g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx to Gno.land's blog
---
<details><summary>Comment section</summary>
<h5>comment4
</h5><h6>by g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx on 13 Feb 09 23:31 UTC</h6>
---
<h5>comment2
</h5><h6>by g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx on 13 Feb 09 23:31 UTC</h6>
---
</details>
</main>
`
assertMDEquals(t, got, expected)
home := Render("")
if strings.Count(home, oldTitle) != 1 {
t.Errorf("post not edited properly")
}
// Edits work everything except title, slug, and publicationDate
// Edits to the above will cause duplication on the blog home page
}
//
{ // Test remove functionality
title := "example title"
slug := "testSlug1"
ModAddPost(cross(cur), slug, title, "body1", "2022-05-25T13:17:22Z", "moul", "tag1,tag2")
got := Render("")
if !strings.Contains(got, title) {
t.Errorf("post was not added properly")
}
postRender := Render("p/" + slug)
if !strings.Contains(postRender, title) {
t.Errorf("post not rendered properly")
}
ModRemovePost(cur, slug)
got = Render("")
if strings.Contains(got, title) {
t.Errorf("post was not removed")
}
postRender = Render("p/" + slug)
assertMDEquals(t, postRender, "404")
}
//
// // 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)
}
}
gnomod.toml
module = "gno.land/r/gnoland/blog" gno = "0.9" [addpkg] creator = "g1g3lsfxhvaqgdv4ccemwpnms4fv6t3aq3p5z6u7"
util.gno
package blog
func checkErr(err error) {
if err != nil {
panic(err)
}
}