From 3d0e5928fda0c82761bb02874385d36b2ea14170 Mon Sep 17 00:00:00 2001 From: jeronimoalbi Date: Tue, 12 Nov 2024 12:02:41 +0100 Subject: [PATCH] feat: add `assertIsBoardName()` function Also changed the expression to validate board names to check that the name starts with three letters and ends with three numbers. This has been defined this way to match `users` realm implementation. See: https://github.com/gnolang/gno/issues/2827 In a next iteration we can support vanity names for boards, once vanity names are supported for the `users` realm. --- examples/gno.land/r/demo/boards2/board.gno | 18 +++++++++++++----- examples/gno.land/r/demo/boards2/boards.gno | 11 +---------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/examples/gno.land/r/demo/boards2/board.gno b/examples/gno.land/r/demo/boards2/board.gno index 79b27da84b2..a855bf4e002 100644 --- a/examples/gno.land/r/demo/boards2/board.gno +++ b/examples/gno.land/r/demo/boards2/board.gno @@ -1,6 +1,7 @@ package boards import ( + "regexp" "std" "strconv" "time" @@ -9,8 +10,9 @@ import ( "gno.land/p/moul/txlink" ) -//---------------------------------------- -// Board +var reBoardName = regexp.MustCompile(`^[a-z]{3}[_a-z0-9]{0,23}[0-9]{3}$`) + +// TODO: Move post and board to a package to improve API and reduce realm size? type BoardID uint64 @@ -30,9 +32,7 @@ type Board struct { } func newBoard(id BoardID, url string, name string, creator std.Address) *Board { - if !reName.MatchString(name) { - panic("invalid name: " + name) - } + assertIsBoardName(name) exists := gBoardsByName.Has(name) if exists { panic("board already exists") @@ -58,6 +58,7 @@ func NewPrivateBoard(url string, name string, creator std.Address) *Board { } */ +// TODO: Do we want to support private boards? If so move forward with implementation. func (board *Board) IsPrivate() bool { return board.id == 0 } @@ -89,6 +90,7 @@ func (board *Board) DeleteThread(pid PostID) { } } +// TODO: Change HasPermission to use a new authorization interface's `CanDo()` func (board *Board) HasPermission(addr std.Address, perm Permission) bool { if board.creator == addr { switch perm { @@ -137,3 +139,9 @@ func (board *Board) GetURLFromThreadAndReplyID(threadID, replyID PostID) string func (board *Board) GetPostFormURL() string { return txlink.URL("CreateThread", "bid", board.id.String()) } + +func assertIsBoardName(name string) { + if !reBoardName.MatchString(name) { + panic("invalid board name: " + name) + } +} diff --git a/examples/gno.land/r/demo/boards2/boards.gno b/examples/gno.land/r/demo/boards2/boards.gno index 5de0555a2f9..5d228c2251e 100644 --- a/examples/gno.land/r/demo/boards2/boards.gno +++ b/examples/gno.land/r/demo/boards2/boards.gno @@ -1,22 +1,13 @@ package boards import ( - "regexp" - "gno.land/p/demo/avl" ) -//---------------------------------------- -// Realm (package) state - +// TODO: Create a Boards or App struct to handle counter and tree indexes var ( gBoards avl.Tree // id -> *Board gBoardsCtr int // increments Board.id gBoardsByName avl.Tree // name -> *Board gDefaultAnonFee = 100000000 // minimum fee required if anonymous ) - -//---------------------------------------- -// Constants - -var reName = regexp.MustCompile(`^[a-z]+[_a-z0-9]{2,29}$`)