Search Apps Documentation Source Content File Folder Download Copy Actions Download

public.gno

4.50 Kb · 193 lines
  1package boards
  2
  3import (
  4	"chain"
  5	"chain/banker"
  6	"chain/runtime"
  7	"strconv"
  8)
  9
 10//----------------------------------------
 11// Public facing functions
 12
 13func GetBoardIDFromName(name string) (BoardID, bool) {
 14	boardI, exists := gBoardsByName.Get(name)
 15	if !exists {
 16		return 0, false
 17	}
 18	return boardI.(*Board).id, true
 19}
 20
 21func CreateBoard(cur realm, name string) BoardID {
 22	if !runtime.PreviousRealm().IsUser() {
 23		panic("invalid non-user call")
 24	}
 25
 26	bid := incGetBoardID()
 27	caller := runtime.OriginCaller()
 28	if usernameOf(caller) == "" {
 29		panic("unauthorized")
 30	}
 31	url := "/r/archive/boards:" + name
 32	board := newBoard(bid, url, name, caller)
 33	bidkey := boardIDKey(bid)
 34	gBoards.Set(bidkey, board)
 35	gBoardsByName.Set(name, board)
 36	return board.id
 37}
 38
 39func checkAnonFee() bool {
 40	sent := banker.OriginSend()
 41	anonFeeCoin := chain.NewCoin("ugnot", int64(gDefaultAnonFee))
 42	if len(sent) == 1 && sent[0].IsGTE(anonFeeCoin) {
 43		return true
 44	}
 45	return false
 46}
 47
 48func CreateThread(cur realm, bid BoardID, title string, body string) PostID {
 49	if !runtime.PreviousRealm().IsUser() {
 50		panic("invalid non-user call")
 51	}
 52
 53	caller := runtime.OriginCaller()
 54	if usernameOf(caller) == "" {
 55		if !checkAnonFee() {
 56			panic("please register, otherwise minimum fee " + strconv.Itoa(gDefaultAnonFee) + " is required if anonymous")
 57		}
 58	}
 59	board := getBoard(bid)
 60	if board == nil {
 61		panic("board not exist")
 62	}
 63	thread := board.AddThread(caller, title, body)
 64	return thread.id
 65}
 66
 67func CreateReply(cur realm, bid BoardID, threadid, postid PostID, body string) PostID {
 68	if !runtime.PreviousRealm().IsUser() {
 69		panic("invalid non-user call")
 70	}
 71
 72	caller := runtime.OriginCaller()
 73	if usernameOf(caller) == "" {
 74		if !checkAnonFee() {
 75			panic("please register, otherwise minimum fee " + strconv.Itoa(gDefaultAnonFee) + " is required if anonymous")
 76		}
 77	}
 78	board := getBoard(bid)
 79	if board == nil {
 80		panic("board not exist")
 81	}
 82	thread := board.GetThread(threadid)
 83	if thread == nil {
 84		panic("thread not exist")
 85	}
 86	if postid == threadid {
 87		reply := thread.AddReply(caller, body)
 88		return reply.id
 89	} else {
 90		post := thread.GetReply(postid)
 91		reply := post.AddReply(caller, body)
 92		return reply.id
 93	}
 94}
 95
 96// If dstBoard is private, does not ping back.
 97// If board specified by bid is private, panics.
 98func CreateRepost(cur realm, bid BoardID, postid PostID, title string, body string, dstBoardID BoardID) PostID {
 99	if !runtime.PreviousRealm().IsUser() {
100		panic("invalid non-user call")
101	}
102
103	caller := runtime.OriginCaller()
104	if usernameOf(caller) == "" {
105		// TODO: allow with gDefaultAnonFee payment.
106		if !checkAnonFee() {
107			panic("please register, otherwise minimum fee " + strconv.Itoa(gDefaultAnonFee) + " is required if anonymous")
108		}
109	}
110	board := getBoard(bid)
111	if board == nil {
112		panic("src board not exist")
113	}
114	if board.IsPrivate() {
115		panic("cannot repost from a private board")
116	}
117	dst := getBoard(dstBoardID)
118	if dst == nil {
119		panic("dst board not exist")
120	}
121	thread := board.GetThread(postid)
122	if thread == nil {
123		panic("thread not exist")
124	}
125	repost := thread.AddRepostTo(caller, title, body, dst)
126	return repost.id
127}
128
129func DeletePost(cur realm, bid BoardID, threadid, postid PostID, reason string) {
130	if !runtime.PreviousRealm().IsUser() {
131		panic("invalid non-user call")
132	}
133
134	caller := runtime.OriginCaller()
135	board := getBoard(bid)
136	if board == nil {
137		panic("board not exist")
138	}
139	thread := board.GetThread(threadid)
140	if thread == nil {
141		panic("thread not exist")
142	}
143	if postid == threadid {
144		// delete thread
145		if !thread.HasPermission(caller, DeletePermission) {
146			panic("unauthorized")
147		}
148		board.DeleteThread(threadid)
149	} else {
150		// delete thread's post
151		post := thread.GetReply(postid)
152		if post == nil {
153			panic("post not exist")
154		}
155		if !post.HasPermission(caller, DeletePermission) {
156			panic("unauthorized")
157		}
158		thread.DeletePost(postid)
159	}
160}
161
162func EditPost(cur realm, bid BoardID, threadid, postid PostID, title, body string) {
163	if !runtime.PreviousRealm().IsUser() {
164		panic("invalid non-user call")
165	}
166
167	caller := runtime.OriginCaller()
168	board := getBoard(bid)
169	if board == nil {
170		panic("board not exist")
171	}
172	thread := board.GetThread(threadid)
173	if thread == nil {
174		panic("thread not exist")
175	}
176	if postid == threadid {
177		// edit thread
178		if !thread.HasPermission(caller, EditPermission) {
179			panic("unauthorized")
180		}
181		thread.Update(title, body)
182	} else {
183		// edit thread's post
184		post := thread.GetReply(postid)
185		if post == nil {
186			panic("post not exist")
187		}
188		if !post.HasPermission(caller, EditPermission) {
189			panic("unauthorized")
190		}
191		post.Update(title, body)
192	}
193}