Skip to content

Commit

Permalink
Merge branch 'devx/feature/boardsv2' into feat/support-listing-members
Browse files Browse the repository at this point in the history
  • Loading branch information
jeronimoalbi committed Feb 25, 2025
2 parents 6a1c7fe + d4ee2c9 commit 3c8cc84
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 37 deletions.
2 changes: 1 addition & 1 deletion docs/reference/stdlibs/std/chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ caller := std.OriginCaller()

## OriginPkgAddress
```go
func OriginPkgAddress() string
func OriginPkgAddress() Address
```
Returns the address of the first (entry point) realm/package in a sequence of realm/package calls.

Expand Down
30 changes: 27 additions & 3 deletions examples/gno.land/r/nt/boards2/v1/board.gno
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ type Board struct {
readOnly bool
}

func newBoard(id BoardID, name string, creator std.Address) *Board {
func newBoard(id BoardID, name string, creator std.Address, p Permissions) *Board {
return &Board{
id: id,
name: name,
creator: creator,
threads: avl.Tree{},
createdAt: time.Now(),
deleted: avl.Tree{},
perms: createDefaultBoardPermissions(creator),
perms: p,
}
}

Expand Down Expand Up @@ -110,7 +110,7 @@ func (board *Board) DeleteThread(pid PostID) {
//
// Pager is used for pagination if it's not nil.
func (board *Board) Render(p *PaginationOpts) string {
sb := &strings.Builder{}
var sb strings.Builder

sb.WriteString(newButtonLink("post", board.GetPostFormURL()) + " ")
sb.WriteString(newButtonLink("members", board.GetMembersURL()))
Expand Down Expand Up @@ -154,6 +154,30 @@ func (board *Board) GetURLFromReplyID(threadID, replyID PostID) string {
return board.GetURL() + "/" + threadID.String() + "/" + replyID.String()
}

func (board *Board) GetRenameFormURL() string {
return txlink.Call("RenameBoard", "name", board.name)
}

func (board *Board) GetFreezeFormURL() string {
return txlink.Call("FreezeBoard", "boardID", board.id.String())
}

func (board *Board) GetFlaggingThresholdFormURL() string {
return txlink.Call("SetFlaggingThreshold", "boardID", board.id.String())
}

func (board *Board) GetInviteMemberFormURL() string {
return txlink.Call("InviteMember", "boardID", board.id.String())
}

func (board *Board) GetRemoveMemberFormURL() string {
return txlink.Call("RemoveMember", "boardID", board.id.String())
}

func (board *Board) GetChangeMemberRoleFormURL() string {
return txlink.Call("ChangeMemberRole", "boardID", board.id.String())
}

func (board *Board) GetPostFormURL() string {
return txlink.Call("CreateThread", "boardID", board.id.String())
}
Expand Down
30 changes: 20 additions & 10 deletions examples/gno.land/r/nt/boards2/v1/board_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"

"gno.land/p/demo/testutils"
"gno.land/p/demo/uassert"
"gno.land/p/moul/txlink"
)
Expand Down Expand Up @@ -36,16 +37,17 @@ func TestBoard_GetURL(t *testing.T) {
name := "foobar_test_get_url123"
want := pkgPath + ":" + name

var addr std.Address

board := newBoard(1, name, addr)
addr := testutils.TestAddress("creator")
perms := createDefaultBoardPermissions(addr)
board := newBoard(1, name, addr, perms)
got := board.GetURL()
uassert.Equal(t, want, got)
}

func TestBoard_GetThread(t *testing.T) {
var addr std.Address
b := newBoard(1, "test123", addr)
addr := testutils.TestAddress("creator")
perms := createDefaultBoardPermissions(addr)
b := newBoard(1, "test123", addr, perms)

_, ok := b.GetThread(12345)
uassert.False(t, ok)
Expand All @@ -56,8 +58,9 @@ func TestBoard_GetThread(t *testing.T) {
}

func TestBoard_DeleteThread(t *testing.T) {
var addr std.Address
b := newBoard(1, "test123", addr)
addr := testutils.TestAddress("creator")
perms := createDefaultBoardPermissions(addr)
b := newBoard(1, "test123", addr, perms)

post := b.AddThread(addr, "foo", "bar")
id := post.GetPostID()
Expand All @@ -72,7 +75,9 @@ var boardUrlPrefix = strings.TrimPrefix(std.CurrentRealm().PkgPath(), "gno.land"

func TestBoard_GetURLFromThreadID(t *testing.T) {
boardName := "test12345"
b := newBoard(BoardID(11), boardName, "")
addr := testutils.TestAddress("creator")
perms := createDefaultBoardPermissions(addr)
b := newBoard(BoardID(11), boardName, addr, perms)
want := boardUrlPrefix + ":" + boardName + "/10"

got := b.GetURLFromThreadID(10)
Expand All @@ -81,7 +86,9 @@ func TestBoard_GetURLFromThreadID(t *testing.T) {

func TestBoard_GetURLFromReplyID(t *testing.T) {
boardName := "test12345"
b := newBoard(BoardID(11), boardName, "")
addr := testutils.TestAddress("creator")
perms := createDefaultBoardPermissions(addr)
b := newBoard(BoardID(11), boardName, addr, perms)
want := boardUrlPrefix + ":" + boardName + "/10/20"

got := b.GetURLFromReplyID(10, 20)
Expand All @@ -90,8 +97,11 @@ func TestBoard_GetURLFromReplyID(t *testing.T) {

func TestBoard_GetPostFormURL(t *testing.T) {
bid := BoardID(386)
b := newBoard(bid, "foo1234", "")
addr := testutils.TestAddress("creator")
perms := createDefaultBoardPermissions(addr)
b := newBoard(bid, "foo1234", addr, perms)
expect := txlink.Call("CreateThread", "boardID", bid.String())

got := b.GetPostFormURL()
uassert.Equal(t, expect, got)
}
6 changes: 3 additions & 3 deletions examples/gno.land/r/nt/boards2/v1/boards.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
gPerm Permissions // TODO: Support assigning a different implementation
gPerms Permissions
gLastBoardID BoardID
gBoardsByID avl.Tree // string(id) -> *Board
gBoardsByName avl.Tree // string(name) -> *Board
Expand All @@ -17,8 +17,8 @@ func init() {
// TODO: Define and change the default realm owner (or owners)
owner := std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // @test1

// Initialize the default realm permissions
gPerm = createDefaultPermissions(owner)
// Initialize default realm permissions
gPerms = createDefaultPermissions(owner)
}

// incGetBoardID returns a new board ID.
Expand Down
5 changes: 4 additions & 1 deletion examples/gno.land/r/nt/boards2/v1/pagination.gno
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func (opts *PaginationOpts) Iterate(tree *avl.Tree, cb func(k string, val interf
}
}

return page
if page.TotalPages > 1 {
return page
}
return nil
}

func mustGetPagination(rawPath string, pageSize int) *PaginationOpts {
Expand Down
1 change: 1 addition & 0 deletions examples/gno.land/r/nt/boards2/v1/permissions.gno
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
PermissionMemberInvite = "member:invite"
PermissionMemberRemove = "member:remove"
PermissionRoleChange = "role:change"
PermissionPermissionsUpdate = "permissions:update"
)

const (
Expand Down
43 changes: 37 additions & 6 deletions examples/gno.land/r/nt/boards2/v1/post.gno
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ func (post *Post) GetURL() string {
}

func (post *Post) GetReplyFormURL() string {
if post.IsThread() {
return txlink.Call("CreateReply",
"boardID", post.board.id.String(),
"threadID", post.threadID.String(),
"replyID", "0",
)
}
return txlink.Call("CreateReply",
"boardID", post.board.id.String(),
"threadID", post.threadID.String(),
Expand Down Expand Up @@ -241,6 +248,24 @@ func (post *Post) GetDeleteFormURL() string {
)
}

func (post *Post) GetEditFormURL() string {
if post.IsThread() {
return txlink.Call("EditThread",
"boardID", post.board.id.String(),
"threadID", post.threadID.String(),
"title", post.GetTitle(),
"body", post.GetBody(),
)
}

return txlink.Call("EditReply",
"boardID", post.board.id.String(),
"threadID", post.threadID.String(),
"replyID", post.id.String(),
"body", post.GetBody(),
)
}

func (post *Post) GetFlagFormURL() string {
if post.IsThread() {
return txlink.Call("FlagThread",
Expand Down Expand Up @@ -325,21 +350,24 @@ func (post *Post) renderPostContent(sb *strings.Builder, indent string) {
sb.WriteString("\n")
}

postURL := post.GetURL()

// Buttons & counters
sb.WriteString(indent)
sb.WriteString(`\- `)
if !post.IsThread() {
sb.WriteString(" \n" + indent)
}

sb.WriteString(newUserLink(post.creator))
sb.WriteString(", ")
sb.WriteString(newLink(post.createdAt.Format(dateFormat), postURL))
sb.WriteString(post.createdAt.Format(dateFormat))

if post.repostsCount > 0 {
sb.WriteString(", ")
sb.WriteString(strconv.FormatUint(post.repostsCount, 10))
sb.WriteString(" reposts")
}

sb.WriteString(" - ")

if srcPost != nil {
sb.WriteString(" ")
sb.WriteString(newButtonLink("see source post", srcPost.GetURL()))
Expand All @@ -353,6 +381,9 @@ func (post *Post) renderPostContent(sb *strings.Builder, indent string) {
sb.WriteString(newButtonLink("repost", post.GetRepostFormURL()))
}

sb.WriteString(" ")
sb.WriteString(newButtonLink("edit", post.GetEditFormURL()))

sb.WriteString(" ")
sb.WriteString(newButtonLink("flag", post.GetFlagFormURL()))

Expand All @@ -367,7 +398,7 @@ func (post *Post) Render(p *PaginationOpts, indent string, levels int) string {
}

// TODO: pass a builder as arg into Render.
sb := &strings.Builder{}
var sb strings.Builder

if post.title != "" {
sb.WriteString(indent)
Expand All @@ -378,7 +409,7 @@ func (post *Post) Render(p *PaginationOpts, indent string, levels int) string {
sb.WriteString("\n")
}

post.renderPostContent(sb, indent)
post.renderPostContent(&sb, indent)

if post.replies.Size() == 0 {
return sb.String()
Expand Down
27 changes: 21 additions & 6 deletions examples/gno.land/r/nt/boards2/v1/post_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
)

func TestPostUpdate(t *testing.T) {
board := newBoard(1, "test123", testutils.TestAddress("creator"))
addr := testutils.TestAddress("creator")
perms := createDefaultBoardPermissions(addr)
board := newBoard(1, "test123", addr, perms)
creator := testutils.TestAddress("creator")
post := newPost(board, 1, creator, "Title", "Body", 1, 0, 0)
title := "New Title"
Expand Down Expand Up @@ -48,6 +50,9 @@ func TestPostSetVisible(t *testing.T) {
}

func TestPostAddRepostTo(t *testing.T) {
// TODO: Improve this unit test
addr := testutils.TestAddress("creatorDstBoard")
perms := createDefaultBoardPermissions(addr)
cases := []struct {
name, title, body string
dstBoard *Board
Expand All @@ -59,7 +64,7 @@ func TestPostAddRepostTo(t *testing.T) {
name: "repost thread",
title: "Repost Title",
body: "Repost body",
dstBoard: newBoard(42, "dst123", testutils.TestAddress("creatorDstBoard")),
dstBoard: newBoard(42, "dst123", addr, perms),
setup: func() *Post { return createTestThread(t) },
},
{
Expand Down Expand Up @@ -112,17 +117,24 @@ func TestNewThread(t *testing.T) {
boardID := BoardID(1)
threadID := PostID(42)
boardName := "test123"
board := newBoard(boardID, boardName, creator)
perms := createDefaultBoardPermissions(creator)
board := newBoard(boardID, boardName, creator, perms)
url := ufmt.Sprintf(
"/r/nt/boards2/v1:%s/%d",
boardName,
uint(threadID),
)
replyURL := ufmt.Sprintf(
"/r/nt/boards2/v1$help&func=CreateReply&boardID=%d&replyID=%d&threadID=%d",
"/r/nt/boards2/v1$help&func=CreateReply&boardID=%d&replyID=0&threadID=%d",
uint(boardID),
uint(threadID),
)
editURL := ufmt.Sprintf(
"/r/nt/boards2/v1$help&func=EditThread&boardID=%d&body=%s&threadID=%d&title=%s",
uint(boardID),
body,
uint(threadID),
strings.ReplaceAll(title, " ", "+"),
)
repostURL := ufmt.Sprintf(
"/r/nt/boards2/v1$help&func=CreateRepost&boardID=%d&threadID=%d",
Expand Down Expand Up @@ -151,6 +163,7 @@ func TestNewThread(t *testing.T) {
uassert.False(t, thread.HasReplies())
uassert.Equal(t, url, thread.GetURL())
uassert.Equal(t, replyURL, thread.GetReplyFormURL())
uassert.Equal(t, editURL, thread.GetEditFormURL())
uassert.Equal(t, repostURL, thread.GetRepostFormURL())
uassert.Equal(t, deleteURL, thread.GetDeleteFormURL())
uassert.Equal(t, flagURL, thread.GetFlagFormURL())
Expand Down Expand Up @@ -282,7 +295,8 @@ func TestNewReply(t *testing.T) {
parentID := PostID(1)
replyID := PostID(2)
boardName := "test123"
board := newBoard(boardID, boardName, creator)
perms := createDefaultBoardPermissions(creator)
board := newBoard(boardID, boardName, creator, perms)
url := ufmt.Sprintf(
"/r/nt/boards2/v1:%s/%d/%d",
boardName,
Expand Down Expand Up @@ -391,7 +405,8 @@ func createTestThread(t *testing.T) *Post {
t.Helper()

creator := testutils.TestAddress("creator")
board := newBoard(1, "test_board_123", creator)
perms := createDefaultBoardPermissions(creator)
board := newBoard(1, "test_board_123", creator, perms)
return board.AddThread(creator, "Title", "Body")
}

Expand Down
Loading

0 comments on commit 3c8cc84

Please sign in to comment.