Skip to content

Commit

Permalink
feat: add assertIsBoardName() function
Browse files Browse the repository at this point in the history
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: gnolang#2827

In a next iteration we can support vanity names for boards, once vanity
names are supported for the `users` realm.
  • Loading branch information
jeronimoalbi committed Nov 12, 2024
1 parent 1510a6f commit 3d0e592
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
18 changes: 13 additions & 5 deletions examples/gno.land/r/demo/boards2/board.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package boards

import (
"regexp"
"std"
"strconv"
"time"
Expand All @@ -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

Expand All @@ -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")
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
11 changes: 1 addition & 10 deletions examples/gno.land/r/demo/boards2/boards.gno
Original file line number Diff line number Diff line change
@@ -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}$`)

0 comments on commit 3d0e592

Please sign in to comment.