home.gno

1.45 Kb ยท 77 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	cross(hor.Register)("n2p5's Home Realm", "")
20
21}
22
23// Add appends a string to the preview Chonk.
24func Add(chunk string) {
25	crossing()
26	assertAdmin()
27	preview.Add(chunk)
28}
29
30// Flush clears the preview Chonk.
31func Flush() {
32	crossing()
33	assertAdmin()
34	preview.Flush()
35}
36
37// Promote promotes the preview Chonk to the active Chonk
38// and creates a new preview Chonk.
39func Promote() {
40	crossing()
41	assertAdmin()
42	active = preview
43	preview = chonk.New()
44}
45
46// Render returns the contents of the scanner for the active or preview Chonk
47// based on the path provided.
48func Render(path string) string {
49	var result string
50	scanner := getScanner(path)
51	for scanner.Scan() {
52		result += scanner.Text()
53	}
54	return result
55}
56
57// assertAdmin panics if the caller is not an admin as defined in the config realm.
58func assertAdmin() {
59	caller := std.PreviousRealm().Address()
60	if !config.IsAdmin(caller) {
61		panic("forbidden: must be admin")
62	}
63}
64
65// getScanner returns the scanner for the active or preview Chonk based
66// on the path provided.
67func getScanner(path string) *chonk.Scanner {
68	if isPreview(path) {
69		return preview.Scanner()
70	}
71	return active.Scanner()
72}
73
74// isPreview returns true if the path prefix is "preview".
75func isPreview(path string) bool {
76	return strings.HasPrefix(path, "preview")
77}