admin.gno
3.59 Kb ยท 156 lines
1package gnoblog
2
3import (
4 "errors"
5 "std"
6 "strings"
7
8 "gno.land/p/demo/avl"
9 "gno.land/p/demo/ufmt"
10 "gno.land/r/gov/dao"
11)
12
13var (
14 errNotAdmin = errors.New("access restricted: not admin")
15 errNotModerator = errors.New("access restricted: not moderator")
16 errNotCommenter = errors.New("access restricted: not commenter")
17)
18
19var (
20 adminAddr = std.Address("g1manfred47kzduec920z88wfr64ylksmdcedlf5") // FIXME: find a way to use this from the main's genesis.
21 moderatorList = avl.NewTree()
22 commenterList = avl.NewTree()
23 inPause bool
24)
25
26func AdminSetAdminAddr(_ realm, addr std.Address) {
27 assertIsAdmin()
28 adminAddr = addr
29}
30
31func AdminSetInPause(_ realm, state bool) {
32 assertIsAdmin()
33 inPause = state
34}
35
36func AdminAddModerator(_ realm, addr std.Address) {
37 assertIsAdmin()
38 moderatorList.Set(addr.String(), true)
39}
40
41func AdminRemoveModerator(_ realm, addr std.Address) {
42 assertIsAdmin()
43 moderatorList.Set(addr.String(), false) // FIXME: delete instead?
44}
45
46func NewPostProposalRequest(_ realm, slug, title, body, publicationDate, authors, tags string) dao.ProposalRequest {
47 caller := std.PreviousRealm().Address()
48 e := dao.NewSimpleExecutor(
49 func() error {
50 addPost(caller, slug, title, body, publicationDate, authors, tags)
51
52 return nil
53 },
54 ufmt.Sprintf("- Post Title: %v\n- Post Publication Date: %v\n- Authors: %v\n- Tags: %v", title, publicationDate, authors, tags),
55 )
56
57 return dao.NewProposalRequest(
58 "Add new post to gnoland blog",
59 "This propoposal is looking to add a new post to gnoland blog",
60 e,
61 )
62}
63
64func ModAddPost(_ realm, slug, title, body, publicationDate, authors, tags string) {
65 assertIsModerator()
66 caller := std.OriginCaller()
67 addPost(caller, slug, title, body, publicationDate, authors, tags)
68}
69
70func addPost(caller std.Address, slug, title, body, publicationDate, authors, tags string) {
71 var tagList []string
72 if tags != "" {
73 tagList = strings.Split(tags, ",")
74 }
75 var authorList []string
76 if authors != "" {
77 authorList = strings.Split(authors, ",")
78 }
79
80 err := b.NewPost(caller, slug, title, body, publicationDate, authorList, tagList)
81
82 checkErr(err)
83}
84
85func ModEditPost(_ realm, slug, title, body, publicationDate, authors, tags string) {
86 assertIsModerator()
87 tagList := strings.Split(tags, ",")
88 authorList := strings.Split(authors, ",")
89
90 err := b.GetPost(slug).Update(title, body, publicationDate, authorList, tagList)
91 checkErr(err)
92}
93
94func ModRemovePost(_ realm, slug string) {
95 assertIsModerator()
96 b.RemovePost(slug)
97}
98
99func ModAddCommenter(_ realm, addr std.Address) {
100 assertIsModerator()
101 commenterList.Set(addr.String(), true)
102}
103
104func ModDelCommenter(_ realm, addr std.Address) {
105 assertIsModerator()
106 commenterList.Set(addr.String(), false) // FIXME: delete instead?
107}
108
109func ModDelComment(_ realm, slug string, index int) {
110 assertIsModerator()
111 err := b.GetPost(slug).DeleteComment(index)
112 checkErr(err)
113}
114
115func isAdmin(addr std.Address) bool {
116 return addr == adminAddr
117}
118
119func isModerator(addr std.Address) bool {
120 _, found := moderatorList.Get(addr.String())
121 return found
122}
123
124func isCommenter(addr std.Address) bool {
125 _, found := commenterList.Get(addr.String())
126 return found
127}
128
129func assertIsAdmin() {
130 caller := std.OriginCaller()
131 if !isAdmin(caller) {
132 panic(errNotAdmin.Error())
133 }
134}
135
136func assertIsModerator() {
137 caller := std.OriginCaller()
138 if isAdmin(caller) || isModerator(caller) {
139 return
140 }
141 panic(errNotModerator.Error())
142}
143
144func assertIsCommenter() {
145 caller := std.OriginCaller()
146 if isAdmin(caller) || isModerator(caller) || isCommenter(caller) {
147 return
148 }
149 panic(errNotCommenter.Error())
150}
151
152func assertNotInPause() {
153 if inPause {
154 panic("access restricted (pause)")
155 }
156}