admin.gno

3.42 Kb ยท 164 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	crossing()
 26	assertIsAdmin()
 27	adminAddr = addr
 28}
 29
 30func AdminSetInPause(state bool) {
 31	assertIsAdmin()
 32	inPause = state
 33}
 34
 35func AdminAddModerator(addr std.Address) {
 36	assertIsAdmin()
 37	moderatorList.Set(addr.String(), true)
 38}
 39
 40func AdminRemoveModerator(addr std.Address) {
 41	assertIsAdmin()
 42	moderatorList.Set(addr.String(), false) // FIXME: delete instead?
 43}
 44
 45func NewPostProposalRequest(slug, title, body, publicationDate, authors, tags string) dao.ProposalRequest {
 46	caller := std.PreviousRealm().Address()
 47	e := dao.NewSimpleExecutor(
 48		func() error {
 49			addPost(caller, slug, title, body, publicationDate, authors, tags)
 50
 51			return nil
 52		},
 53		ufmt.Sprintf("- Post Title: %v\n- Post Publication Date: %v\n- Authors: %v\n- Tags: %v", title, publicationDate, authors, tags),
 54	)
 55
 56	return dao.NewProposalRequest(
 57		"Add new post to gnoland blog",
 58		"This propoposal is looking to add a new post to gnoland blog",
 59		e,
 60	)
 61}
 62
 63func ModAddPost(slug, title, body, publicationDate, authors, tags string) {
 64	crossing()
 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(slug, title, body, publicationDate, authors, tags string) {
 86	crossing()
 87	assertIsModerator()
 88
 89	tagList := strings.Split(tags, ",")
 90	authorList := strings.Split(authors, ",")
 91
 92	err := b.GetPost(slug).Update(title, body, publicationDate, authorList, tagList)
 93	checkErr(err)
 94}
 95
 96func ModRemovePost(slug string) {
 97	crossing()
 98	assertIsModerator()
 99
100	b.RemovePost(slug)
101}
102
103func ModAddCommenter(addr std.Address) {
104	crossing()
105	assertIsModerator()
106	commenterList.Set(addr.String(), true)
107}
108
109func ModDelCommenter(addr std.Address) {
110	crossing()
111	assertIsModerator()
112	commenterList.Set(addr.String(), false) // FIXME: delete instead?
113}
114
115func ModDelComment(slug string, index int) {
116	crossing()
117	assertIsModerator()
118
119	err := b.GetPost(slug).DeleteComment(index)
120	checkErr(err)
121}
122
123func isAdmin(addr std.Address) bool {
124	return addr == adminAddr
125}
126
127func isModerator(addr std.Address) bool {
128	_, found := moderatorList.Get(addr.String())
129	return found
130}
131
132func isCommenter(addr std.Address) bool {
133	_, found := commenterList.Get(addr.String())
134	return found
135}
136
137func assertIsAdmin() {
138	caller := std.OriginCaller()
139	if !isAdmin(caller) {
140		panic("access restricted.")
141	}
142}
143
144func assertIsModerator() {
145	caller := std.OriginCaller()
146	if isAdmin(caller) || isModerator(caller) {
147		return
148	}
149	panic("access restricted")
150}
151
152func assertIsCommenter() {
153	caller := std.OriginCaller()
154	if isAdmin(caller) || isModerator(caller) || isCommenter(caller) {
155		return
156	}
157	panic("access restricted")
158}
159
160func assertNotInPause() {
161	if inPause {
162		panic("access restricted (pause)")
163	}
164}