admin.gno

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