1package gnoblog
2
3import (
4 "std"
5 "strings"
6
7 "gno.land/p/demo/avl"
8 "gno.land/p/demo/context"
9 "gno.land/p/gov/proposal"
10)
11
12var (
13 adminAddr std.Address
14 moderatorList avl.Tree
15 commenterList avl.Tree
16 inPause bool
17)
18
19func init() {
20 // adminAddr = std.GetOrigCaller() // FIXME: find a way to use this from the main's genesis.
21 adminAddr = "g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq"
22}
23
24func AdminSetAdminAddr(addr std.Address) {
25 assertIsAdmin()
26 adminAddr = addr
27}
28
29func AdminSetInPause(state bool) {
30 assertIsAdmin()
31 inPause = state
32}
33
34func AdminAddModerator(addr std.Address) {
35 assertIsAdmin()
36 moderatorList.Set(addr.String(), true)
37}
38
39func AdminRemoveModerator(addr std.Address) {
40 assertIsAdmin()
41 moderatorList.Set(addr.String(), false) // FIXME: delete instead?
42}
43
44func DaoAddPost(ctx context.Context, slug, title, body, publicationDate, authors, tags string) {
45 proposal.AssertContextApprovedByGovDAO(ctx)
46 caller := std.DerivePkgAddr("gno.land/r/gov/dao")
47 addPost(caller, slug, title, body, publicationDate, authors, tags)
48}
49
50func ModAddPost(slug, title, body, publicationDate, authors, tags string) {
51 assertIsModerator()
52 caller := std.GetOrigCaller()
53 addPost(caller, slug, title, body, publicationDate, authors, tags)
54}
55
56func addPost(caller std.Address, slug, title, body, publicationDate, authors, tags string) {
57 var tagList []string
58 if tags != "" {
59 tagList = strings.Split(tags, ",")
60 }
61 var authorList []string
62 if authors != "" {
63 authorList = strings.Split(authors, ",")
64 }
65
66 err := b.NewPost(caller, slug, title, body, publicationDate, authorList, tagList)
67
68 checkErr(err)
69}
70
71func ModEditPost(slug, title, body, publicationDate, authors, tags string) {
72 assertIsModerator()
73
74 tagList := strings.Split(tags, ",")
75 authorList := strings.Split(authors, ",")
76
77 err := b.GetPost(slug).Update(title, body, publicationDate, authorList, tagList)
78 checkErr(err)
79}
80
81func ModRemovePost(slug string) {
82 assertIsModerator()
83
84 b.RemovePost(slug)
85}
86
87func ModAddCommenter(addr std.Address) {
88 assertIsModerator()
89 commenterList.Set(addr.String(), true)
90}
91
92func ModDelCommenter(addr std.Address) {
93 assertIsModerator()
94 commenterList.Set(addr.String(), false) // FIXME: delete instead?
95}
96
97func ModDelComment(slug string, index int) {
98 assertIsModerator()
99
100 err := b.GetPost(slug).DeleteComment(index)
101 checkErr(err)
102}
103
104func isAdmin(addr std.Address) bool {
105 return addr == adminAddr
106}
107
108func isModerator(addr std.Address) bool {
109 _, found := moderatorList.Get(addr.String())
110 return found
111}
112
113func isCommenter(addr std.Address) bool {
114 _, found := commenterList.Get(addr.String())
115 return found
116}
117
118func assertIsAdmin() {
119 caller := std.GetOrigCaller()
120 if !isAdmin(caller) {
121 panic("access restricted.")
122 }
123}
124
125func assertIsModerator() {
126 caller := std.GetOrigCaller()
127 if isAdmin(caller) || isModerator(caller) {
128 return
129 }
130 panic("access restricted")
131}
132
133func assertIsCommenter() {
134 caller := std.GetOrigCaller()
135 if isAdmin(caller) || isModerator(caller) || isCommenter(caller) {
136 return
137 }
138 panic("access restricted")
139}
140
141func assertNotInPause() {
142 if inPause {
143 panic("access restricted (pause)")
144 }
145}
admin.gno
3.05 Kb ยท 145 lines