home.gno

1.40 Kb ยท 74 lines
 1package home
 2
 3import (
 4	"std"
 5	"strings"
 6
 7	"gno.land/p/n2p5/chonk"
 8
 9	"gno.land/r/leon/hor"
10	"gno.land/r/n2p5/config"
11)
12
13var (
14	active  = chonk.New()
15	preview = chonk.New()
16)
17
18func init() {
19	hor.Register("N2p5's Home Realm", "")
20
21}
22
23// Add appends a string to the preview Chonk.
24func Add(chunk string) {
25	assertAdmin()
26	preview.Add(chunk)
27}
28
29// Flush clears the preview Chonk.
30func Flush() {
31	assertAdmin()
32	preview.Flush()
33}
34
35// Promote promotes the preview Chonk to the active Chonk
36// and creates a new preview Chonk.
37func Promote() {
38	assertAdmin()
39	active = preview
40	preview = chonk.New()
41}
42
43// Render returns the contents of the scanner for the active or preview Chonk
44// based on the path provided.
45func Render(path string) string {
46	var result string
47	scanner := getScanner(path)
48	for scanner.Scan() {
49		result += scanner.Text()
50	}
51	return result
52}
53
54// assertAdmin panics if the caller is not an admin as defined in the config realm.
55func assertAdmin() {
56	caller := std.PreviousRealm().Address()
57	if !config.IsAdmin(caller) {
58		panic("forbidden: must be admin")
59	}
60}
61
62// getScanner returns the scanner for the active or preview Chonk based
63// on the path provided.
64func getScanner(path string) *chonk.Scanner {
65	if isPreview(path) {
66		return preview.Scanner()
67	}
68	return active.Scanner()
69}
70
71// isPreview returns true if the path prefix is "preview".
72func isPreview(path string) bool {
73	return strings.HasPrefix(path, "preview")
74}