public.gno

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