Playground
board.gno
package hub
import (
"gno.land/p/gnoland/boards/v0"
)
// Board defines a safe type for boards.
type Board struct {
// id is the unique identifier of the board.
id uint64
// name is the current name of the board.
name string
// aliases contains a list of alternative names for the board.
aliases []string
// readonly indicates that the board is readonly.
readonly bool
// threadCount contains the number of threads within the board.
threadCount int
// memberCount contains the number of members of the board.
memberCount int
// creator is the account address that created the board.
creator address
// createdAt is the board's creation time as Unix time.
createdAt int64
// updatedAt is the board's update time as Unix time.
updatedAt int64
}
// ID returns the unique identifier of the board.
func (b Board) ID() uint64 { return b.id }
// Name returns the current name of the board.
func (b Board) Name() string { return b.name }
// Aliases returns the list of alternative names for the board.
func (b Board) Aliases() []string { return append([]string(nil), b.aliases...) }
// Readonly indicates that the board is readonly.
func (b Board) Readonly() bool { return b.readonly }
// ThreadCount returns the number of threads within the board.
func (b Board) ThreadCount() int { return b.threadCount }
// MemberCount returns the number of members of the board.
func (b Board) MemberCount() int { return b.memberCount }
// Creator returns the account address that created the board.
func (b Board) Creator() address { return b.creator }
// CreatedAt returns the board's creation time as Unix time.
func (b Board) CreatedAt() int64 { return b.createdAt }
// UpdatedAt returns the board's update time as Unix time.
func (b Board) UpdatedAt() int64 { return b.updatedAt }
// NewSafeBoard creates a safe board.
func NewSafeBoard(ref *boards.Board) Board {
if ref == nil {
panic("board reference is nil")
}
var usersCount int
if ref.Permissions != nil {
usersCount = ref.Permissions.UsersCount()
}
var threadCount int
if ref.Threads != nil {
threadCount = ref.Threads.Size()
}
return Board{
id: uint64(ref.ID),
name: ref.Name,
aliases: append([]string(nil), ref.Aliases...),
readonly: ref.Readonly,
threadCount: threadCount,
memberCount: usersCount,
creator: ref.Creator,
createdAt: timeToUnix(ref.CreatedAt),
updatedAt: timeToUnix(ref.UpdatedAt),
}
}
board_test.gno
package hub_test
import (
"testing"
"gno.land/p/gnoland/boards/exts/hub/v0"
"gno.land/p/gnoland/boards/exts/permissions/v0"
"gno.land/p/gnoland/boards/v0"
"gno.land/p/nt/urequire/v0"
)
func TestNewSafeBoard(t *testing.T) {
tests := []struct {
name string
setup func() *boards.Board
threadCount, memberCount int
createdAt int64
}{
{
name: "new board",
setup: func() *boards.Board { return boards.New(1) },
createdAt: testTime,
},
{
name: "with threads",
setup: func() *boards.Board {
b := boards.New(1)
b.Threads.Add(boards.MustNewThread(b, alice, "Title 1", "Body 1"))
b.Threads.Add(boards.MustNewThread(b, bob, "Title 2", "Body 2"))
return b
},
threadCount: 2,
createdAt: testTime,
},
{
name: "with members",
setup: func() *boards.Board {
b := boards.New(1)
perms := permissions.New()
perms.SetUserRoles(alice)
perms.SetUserRoles(bob)
b.Permissions = perms
return b
},
memberCount: 2,
createdAt: testTime,
},
{
// A board with no storages assigned must not panic: the counts
// are skipped and the zero creation time maps to a zero Unix time.
name: "bare board without storages",
setup: func() *boards.Board { return &boards.Board{ID: 1} },
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ref := tt.setup()
ref.Name = "test123"
ref.Creator = alice
board := hub.NewSafeBoard(ref)
urequire.Equal(t, uint64(1), board.ID(), "expect ID to match")
urequire.Equal(t, "test123", board.Name(), "expect name to match")
urequire.Equal(t, alice, board.Creator(), "expect creator to match")
urequire.False(t, board.Readonly(), "expect board not to be readonly")
urequire.Equal(t, tt.threadCount, board.ThreadCount(), "expect thread count to match")
urequire.Equal(t, tt.memberCount, board.MemberCount(), "expect member count to match")
urequire.Equal(t, tt.createdAt, board.CreatedAt(), "expect creation time to match")
urequire.Equal(t, int64(0), board.UpdatedAt(), "expect zero update time to map to zero")
})
}
}
// TestNewSafeBoardSnapshotsAliases asserts the invariant this package exists
// for: a safe type is a snapshot, so later writes to the source board must not
// be observable through a value already handed to a caller.
func TestNewSafeBoardSnapshotsAliases(t *testing.T) {
ref := boards.New(1)
ref.Name = "current"
ref.Aliases = []string{"previous"}
board := hub.NewSafeBoard(ref)
ref.Aliases[0] = "mutated"
ref.Aliases = append(ref.Aliases, "added")
ref.Name = "renamed"
ref.Readonly = true
aliases := board.Aliases()
urequire.Equal(t, 1, len(aliases), "expect alias count to be unaffected")
urequire.Equal(t, "previous", aliases[0], "expect alias to be unaffected")
urequire.Equal(t, "current", board.Name(), "expect name to be unaffected")
urequire.False(t, board.Readonly(), "expect readonly to be unaffected")
}
func TestNewSafeBoardNilRef(cur realm, t *testing.T) {
urequire.PanicsWithMessage(t, cur, "board reference is nil", func() {
hub.NewSafeBoard(nil)
}, "expect a nil board reference to be rejected")
}
comment.gno
package hub
import (
"gno.land/p/gnoland/boards/v0"
)
// Comment defines a type for threads comment/replies.
type Comment struct {
// id is the unique identifier of the comment.
id uint64
// boardID is the board ID where comment is created.
boardID uint64
// threadID is the ID of the thread where comment is created.
threadID uint64
// parentID is the ID of the parent comment or reply.
parentID uint64
// body contains the comment's content.
body string
// hidden indicates that comment is hidden.
hidden bool
// replyCount contains the number of comments replies.
// Count only includes top level replies, sub-replies are not included.
replyCount int
// flagCount contains the number of flags that comment has.
flagCount int
// creator is the account address that created the comment or reply.
creator address
// createdAt is thread's creation time as Unix time.
createdAt int64
// updatedAt is thread's update time as Unix time.
updatedAt int64
}
// ID returns the unique identifier of the comment.
func (c Comment) ID() uint64 { return c.id }
// BoardID returns the board ID where the comment is created.
func (c Comment) BoardID() uint64 { return c.boardID }
// ThreadID returns the ID of the thread where the comment is created.
func (c Comment) ThreadID() uint64 { return c.threadID }
// ParentID returns the ID of the parent comment or reply.
func (c Comment) ParentID() uint64 { return c.parentID }
// Body returns the comment's content.
func (c Comment) Body() string { return c.body }
// Hidden indicates that the comment is hidden.
func (c Comment) Hidden() bool { return c.hidden }
// ReplyCount returns the number of comment replies.
// Count only includes top level replies, sub-replies are not included.
func (c Comment) ReplyCount() int { return c.replyCount }
// FlagCount returns the number of flags that the comment has.
func (c Comment) FlagCount() int { return c.flagCount }
// Creator returns the account address that created the comment or reply.
func (c Comment) Creator() address { return c.creator }
// CreatedAt returns the comment's creation time as Unix time.
func (c Comment) CreatedAt() int64 { return c.createdAt }
// UpdatedAt returns the comment's update time as Unix time.
func (c Comment) UpdatedAt() int64 { return c.updatedAt }
// NewSafeComment creates a safe comment.
func NewSafeComment(ref *boards.Post) Comment {
if ref == nil {
panic("post reference is nil")
}
if boards.IsThread(ref) {
panic("post is not a comment or reply")
}
var replyCount int
if ref.Replies != nil {
replyCount = ref.Replies.Size()
}
var flagCount int
if ref.Flags != nil {
flagCount = ref.Flags.Size()
}
return Comment{
id: uint64(ref.ID),
boardID: uint64(ref.Board.ID),
threadID: uint64(ref.ThreadID),
parentID: uint64(ref.ParentID),
body: ref.Body,
hidden: ref.Hidden,
replyCount: replyCount,
flagCount: flagCount,
creator: ref.Creator,
createdAt: timeToUnix(ref.CreatedAt),
updatedAt: timeToUnix(ref.UpdatedAt),
}
}
comment_test.gno
package hub_test
import (
"testing"
"gno.land/p/gnoland/boards/exts/hub/v0"
"gno.land/p/gnoland/boards/v0"
"gno.land/p/nt/urequire/v0"
)
// Addresses and the fixed time the test VM reports for time.Now().
const (
alice address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
bob address = "g1skl80cuz8zq3lul9pgz5pc35l2pfzgxgfpsqkx"
testTime int64 = 1234567890
)
func TestNewSafeComment(t *testing.T) {
board := boards.New(1)
thread := boards.MustNewThread(board, alice, "Title", "Body")
ref := boards.MustNewReply(thread, bob, "Comment")
ref.Flags.Add(boards.Flag{User: alice, Reason: "Spam"})
comment := hub.NewSafeComment(ref)
urequire.Equal(t, uint64(ref.ID), comment.ID(), "expect ID to match")
urequire.Equal(t, uint64(1), comment.BoardID(), "expect board ID to match")
urequire.Equal(t, uint64(thread.ID), comment.ThreadID(), "expect thread ID to match")
urequire.Equal(t, "Comment", comment.Body(), "expect body to match")
urequire.Equal(t, bob, comment.Creator(), "expect creator to match")
urequire.False(t, comment.Hidden(), "expect comment not to be hidden")
urequire.Equal(t, 0, comment.ReplyCount(), "expect no replies")
urequire.Equal(t, 1, comment.FlagCount(), "expect one flag")
urequire.Equal(t, testTime, comment.CreatedAt(), "expect creation time to match")
urequire.Equal(t, int64(0), comment.UpdatedAt(), "expect zero update time to map to zero")
}
// TestNewSafeCommentParentID documents that a top level comment reports its
// thread as parent rather than a zero parent, so callers must compare
// ParentID against ThreadID to tell comments and replies apart.
func TestNewSafeCommentParentID(t *testing.T) {
board := boards.New(1)
thread := boards.MustNewThread(board, alice, "Title", "Body")
commentRef := boards.MustNewReply(thread, bob, "Comment")
replyRef := boards.MustNewReply(commentRef, alice, "Reply")
comment := hub.NewSafeComment(commentRef)
reply := hub.NewSafeComment(replyRef)
urequire.Equal(t, comment.ThreadID(), comment.ParentID(), "expect a comment's parent to be its thread")
urequire.Equal(t, comment.ID(), reply.ParentID(), "expect a reply's parent to be its comment")
urequire.Equal(t, comment.ThreadID(), reply.ThreadID(), "expect a reply to keep the thread ID")
}
func TestNewSafeCommentReplyCount(t *testing.T) {
board := boards.New(1)
thread := boards.MustNewThread(board, alice, "Title", "Body")
ref := boards.MustNewReply(thread, bob, "Comment")
ref.Replies.Add(boards.MustNewReply(ref, alice, "Reply 1"))
ref.Replies.Add(boards.MustNewReply(ref, alice, "Reply 2"))
comment := hub.NewSafeComment(ref)
urequire.Equal(t, 2, comment.ReplyCount(), "expect reply count to match")
}
// TestNewSafeCommentNilStorages covers the branches that skip counting when a
// post carries no reply or flag storage.
func TestNewSafeCommentNilStorages(t *testing.T) {
ref := &boards.Post{ID: 2, ParentID: 1, ThreadID: 1, Board: boards.New(1)}
comment := hub.NewSafeComment(ref)
urequire.Equal(t, 0, comment.ReplyCount(), "expect no replies")
urequire.Equal(t, 0, comment.FlagCount(), "expect no flags")
urequire.Equal(t, int64(0), comment.CreatedAt(), "expect zero creation time to map to zero")
}
func TestNewSafeCommentRejectsInvalidRefs(cur realm, t *testing.T) {
board := boards.New(1)
thread := boards.MustNewThread(board, alice, "Title", "Body")
urequire.PanicsWithMessage(t, cur, "post reference is nil", func() {
hub.NewSafeComment(nil)
}, "expect a nil post reference to be rejected")
urequire.PanicsWithMessage(t, cur, "post is not a comment or reply", func() {
hub.NewSafeComment(thread)
}, "expect a thread to be rejected")
}
flag.gno
package hub
import (
"gno.land/p/gnoland/boards/v0"
)
// Flag defines a type for thread and comment flags.
type Flag struct {
// user is the user that flagged.
user address
// reason is the reason for flagging.
reason string
}
// User returns the user that flagged.
func (f Flag) User() address { return f.user }
// Reason returns the reason for flagging.
func (f Flag) Reason() string { return f.reason }
// NewSafeFlag creates a safe flag.
func NewSafeFlag(ref boards.Flag) Flag {
return Flag{
user: ref.User,
reason: ref.Reason,
}
}
format.gno
package hub
import "time"
// timeToUnix converts time to Unix epoch.
func timeToUnix(t time.Time) int64 {
if t.IsZero() {
return 0
}
return t.Unix()
}
gnomod.toml
module = "gno.land/p/gnoland/boards/exts/hub/v0" gno = "0.9" [addpkg] creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"
member.gno
package hub
import (
"gno.land/p/gnoland/boards/v0"
)
// Member defines a safe type for board members.
type Member struct {
// address is the account address of the member.
address address
// roles contains the names of the roles assigned to the member.
roles []string
}
// Address returns the account address of the member.
func (m Member) Address() address { return m.address }
// Roles returns the names of the roles assigned to the member.
func (m Member) Roles() []string { return append([]string(nil), m.roles...) }
// NewSafeMember creates a safe board member.
func NewSafeMember(ref boards.User) Member {
roles := make([]string, len(ref.Roles))
for i, r := range ref.Roles {
roles[i] = string(r)
}
return Member{
address: ref.Address,
roles: roles,
}
}
member_test.gno
package hub_test
import (
"testing"
"gno.land/p/gnoland/boards/exts/hub/v0"
"gno.land/p/gnoland/boards/v0"
"gno.land/p/nt/urequire/v0"
)
func TestNewSafeMember(t *testing.T) {
ref := boards.User{
Address: alice,
Roles: []boards.Role{"owner", "admin"},
}
member := hub.NewSafeMember(ref)
urequire.Equal(t, alice.String(), member.Address().String(), "expect address to match")
urequire.Equal(t, 2, len(member.Roles()), "expect role count to match")
urequire.Equal(t, "owner", member.Roles()[0], "expect first role to match")
urequire.Equal(t, "admin", member.Roles()[1], "expect second role to match")
}
func TestNewSafeMemberWithoutRoles(t *testing.T) {
member := hub.NewSafeMember(boards.User{Address: bob})
urequire.Equal(t, bob.String(), member.Address().String(), "expect address to match")
urequire.Equal(t, 0, len(member.Roles()), "expect no roles")
}
func TestSafeMemberIsASnapshot(t *testing.T) {
roles := []boards.Role{"owner"}
member := hub.NewSafeMember(boards.User{Address: alice, Roles: roles})
// Mutating the source must not change what the member reports.
roles[0] = "guest"
urequire.Equal(t, "owner", member.Roles()[0], "expect source mutation to be ignored")
// Mutating a returned slice must not change what the member reports.
member.Roles()[0] = "guest"
urequire.Equal(t, "owner", member.Roles()[0], "expect returned slice to be a copy")
}
thread.gno
package hub
import (
"gno.land/p/gnoland/boards/v0"
)
// Thread defines a type for board threads.
type Thread struct {
// id is the unique identifier of the thread.
id uint64
// originalBoardID contains the board ID of the original thread when current is a repost.
originalBoardID uint64
// originalThreadID contains the ID of the original thread when current is a repost.
originalThreadID uint64
// boardID is the board ID where thread is created.
boardID uint64
// title contains thread's title.
title string
// body contains content of the thread.
body string
// hidden indicates that thread is hidden.
hidden bool
// readonly indicates that thread is readonly.
readonly bool
// commentCount contains the number of thread comments.
// Count only includes top level comment, replies are not included.
commentCount int
// repostCount contains the number of times thread has been reposted.
repostCount int
// flagCount contains the number of flags that thread has.
flagCount int
// creator is the account address that created the thread.
creator address
// createdAt is thread's creation time as Unix time.
createdAt int64
// updatedAt is thread's update time as unix time.
updatedAt int64
}
// ID returns the unique identifier of the thread.
func (t Thread) ID() uint64 { return t.id }
// OriginalBoardID returns the board ID of the original thread when current is a repost.
func (t Thread) OriginalBoardID() uint64 { return t.originalBoardID }
// OriginalThreadID returns the ID of the original thread when current is a repost.
func (t Thread) OriginalThreadID() uint64 { return t.originalThreadID }
// BoardID returns the board ID where the thread is created.
func (t Thread) BoardID() uint64 { return t.boardID }
// Title returns the thread's title.
func (t Thread) Title() string { return t.title }
// Body returns the content of the thread.
func (t Thread) Body() string { return t.body }
// Hidden indicates that the thread is hidden.
func (t Thread) Hidden() bool { return t.hidden }
// Readonly indicates that the thread is readonly.
func (t Thread) Readonly() bool { return t.readonly }
// CommentCount returns the number of thread comments.
// Count only includes top level comment, replies are not included.
func (t Thread) CommentCount() int { return t.commentCount }
// RepostCount returns the number of times the thread has been reposted.
func (t Thread) RepostCount() int { return t.repostCount }
// FlagCount returns the number of flags that the thread has.
func (t Thread) FlagCount() int { return t.flagCount }
// Creator returns the account address that created the thread.
func (t Thread) Creator() address { return t.creator }
// CreatedAt returns the thread's creation time as Unix time.
func (t Thread) CreatedAt() int64 { return t.createdAt }
// UpdatedAt returns the thread's update time as Unix time.
func (t Thread) UpdatedAt() int64 { return t.updatedAt }
// NewSafeThread creates a safe thread.
func NewSafeThread(ref *boards.Post) Thread {
if ref == nil {
panic("post reference is nil")
}
if !boards.IsThread(ref) {
panic("post is not a thread")
}
var commentCount int
if ref.Replies != nil {
commentCount = ref.Replies.Size()
}
var repostCount int
if ref.Reposts != nil {
repostCount = ref.Reposts.Size()
}
var flagCount int
if ref.Flags != nil {
flagCount = ref.Flags.Size()
}
return Thread{
id: uint64(ref.ID),
originalBoardID: uint64(ref.OriginalBoardID),
originalThreadID: uint64(ref.ParentID),
boardID: uint64(ref.Board.ID),
title: ref.Title,
body: ref.Body,
hidden: ref.Hidden,
readonly: ref.Readonly,
commentCount: commentCount,
repostCount: repostCount,
flagCount: flagCount,
creator: ref.Creator,
createdAt: timeToUnix(ref.CreatedAt),
updatedAt: timeToUnix(ref.UpdatedAt),
}
}
thread_test.gno
package hub_test
import (
"testing"
"gno.land/p/gnoland/boards/exts/hub/v0"
"gno.land/p/gnoland/boards/v0"
"gno.land/p/nt/urequire/v0"
)
func TestNewSafeThread(t *testing.T) {
board := boards.New(1)
ref := boards.MustNewThread(board, alice, "Title", "Body")
ref.Flags.Add(boards.Flag{User: bob, Reason: "Spam"})
thread := hub.NewSafeThread(ref)
urequire.Equal(t, uint64(ref.ID), thread.ID(), "expect ID to match")
urequire.Equal(t, uint64(1), thread.BoardID(), "expect board ID to match")
urequire.Equal(t, "Title", thread.Title(), "expect title to match")
urequire.Equal(t, "Body", thread.Body(), "expect body to match")
urequire.Equal(t, alice, thread.Creator(), "expect creator to match")
urequire.False(t, thread.Hidden(), "expect thread not to be hidden")
urequire.False(t, thread.Readonly(), "expect thread not to be readonly")
urequire.Equal(t, 0, thread.CommentCount(), "expect no comments")
urequire.Equal(t, 0, thread.RepostCount(), "expect no reposts")
urequire.Equal(t, 1, thread.FlagCount(), "expect one flag")
urequire.Equal(t, testTime, thread.CreatedAt(), "expect creation time to match")
urequire.Equal(t, int64(0), thread.UpdatedAt(), "expect zero update time to map to zero")
}
func TestNewSafeThreadCounts(t *testing.T) {
board := boards.New(1)
ref := boards.MustNewThread(board, alice, "Title", "Body")
ref.Replies.Add(boards.MustNewReply(ref, bob, "Comment 1"))
ref.Replies.Add(boards.MustNewReply(ref, bob, "Comment 2"))
thread := hub.NewSafeThread(ref)
urequire.Equal(t, 2, thread.CommentCount(), "expect comment count to match")
urequire.Equal(t, 0, thread.FlagCount(), "expect no flags")
}
// TestNewSafeThreadNilStorages covers the branches that skip counting when a
// post carries no reply, repost or flag storage.
func TestNewSafeThreadNilStorages(t *testing.T) {
ref := &boards.Post{ID: 1, ThreadID: 1, Board: boards.New(1)}
thread := hub.NewSafeThread(ref)
urequire.Equal(t, 0, thread.CommentCount(), "expect no comments")
urequire.Equal(t, 0, thread.RepostCount(), "expect no reposts")
urequire.Equal(t, 0, thread.FlagCount(), "expect no flags")
urequire.Equal(t, int64(0), thread.CreatedAt(), "expect zero creation time to map to zero")
}
func TestNewSafeThreadRejectsInvalidRefs(cur realm, t *testing.T) {
board := boards.New(1)
thread := boards.MustNewThread(board, alice, "Title", "Body")
comment := boards.MustNewReply(thread, alice, "Comment")
urequire.PanicsWithMessage(t, cur, "post reference is nil", func() {
hub.NewSafeThread(nil)
}, "expect a nil post reference to be rejected")
urequire.PanicsWithMessage(t, cur, "post is not a thread", func() {
hub.NewSafeThread(comment)
}, "expect a comment to be rejected")
}
func TestNewSafeFlag(t *testing.T) {
flag := hub.NewSafeFlag(boards.Flag{User: alice, Reason: "Spam"})
urequire.Equal(t, alice, flag.User(), "expect user to match")
urequire.Equal(t, "Spam", flag.Reason(), "expect reason to match")
}