Search Apps Documentation Source Content File Folder Download Copy Actions Download

protected.gno

1.99 Kb · 56 lines
 1// The `protected.gno` file contains public realm functions that must only be called
 2// from realms that live within the Boards2 package namespace. This allows sub realms
 3// to access boards data to be able to migrate content from one version to another or
 4// to implement specific features in separate sub realms.
 5package boards2
 6
 7import (
 8	"chain/runtime"
 9	"path"
10	"strings"
11
12	"gno.land/p/gnoland/boards"
13)
14
15// TODO: Authorize sub realms to be able to call protected functions (use DAOs)
16
17// Keeps the package path to Boards2 realm.
18// It ignores the last realm path element which is the current version.
19var boardsNS = path.Dir(runtime.CurrentRealm().PkgPath()) + "/"
20
21// GetRealmPermissions returns Boards2 realm permissions.
22// This is a protected function only callable by Boards2 sub realms.
23func GetRealmPermissions() boards.Permissions {
24	assertRealmHasBoardsNS(runtime.CurrentRealm())
25	return gPerms
26}
27
28// GetBoard returns a board.
29// This is a protected function only callable by Boards2 sub realms.
30func GetBoard(boardID boards.ID) (_ *boards.Board, found bool) {
31	assertRealmHasBoardsNS(runtime.CurrentRealm())
32	return gBoards.Get(boardID)
33}
34
35// MustGetBoard returns a board or panics on error.
36// This is a protected function only callable by Boards2 sub realms.
37func MustGetBoard(boardID boards.ID) *boards.Board {
38	assertRealmHasBoardsNS(runtime.CurrentRealm())
39	return mustGetBoard(boardID)
40}
41
42// Iterate iterates boards.
43// Iteration is done for all boards, including the ones that are not listed.
44// To reverse iterate boards use a negative count.
45// If the callback returns true, iteration is stopped.
46func Iterate(start, count int, fn boards.BoardIterFn) bool {
47	assertRealmHasBoardsNS(runtime.CurrentRealm())
48	return gBoards.Iterate(start, count, fn)
49}
50
51// assertRealmHasBoardsNS asserts that a realm lives within Boards2 namespace.
52func assertRealmHasBoardsNS(r runtime.Realm) {
53	if !strings.HasPrefix(r.PkgPath(), boardsNS) {
54		panic("forbidden, caller should live within \"" + boardsNS + "\" namespace")
55	}
56}