public.gno

4.41 Kb ยท 191 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(cur realm, 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(cur realm, bid BoardID, title string, body string) PostID {
 47	if !std.PreviousRealm().IsUser() {
 48		panic("invalid non-user call")
 49	}
 50
 51	caller := std.OriginCaller()
 52	if usernameOf(caller) == "" {
 53		if !checkAnonFee() {
 54			panic("please register, otherwise minimum fee " + strconv.Itoa(gDefaultAnonFee) + " is required if anonymous")
 55		}
 56	}
 57	board := getBoard(bid)
 58	if board == nil {
 59		panic("board not exist")
 60	}
 61	thread := board.AddThread(caller, title, body)
 62	return thread.id
 63}
 64
 65func CreateReply(cur realm, bid BoardID, threadid, postid PostID, body string) PostID {
 66	if !std.PreviousRealm().IsUser() {
 67		panic("invalid non-user call")
 68	}
 69
 70	caller := std.OriginCaller()
 71	if usernameOf(caller) == "" {
 72		if !checkAnonFee() {
 73			panic("please register, otherwise minimum fee " + strconv.Itoa(gDefaultAnonFee) + " is required if anonymous")
 74		}
 75	}
 76	board := getBoard(bid)
 77	if board == nil {
 78		panic("board not exist")
 79	}
 80	thread := board.GetThread(threadid)
 81	if thread == nil {
 82		panic("thread not exist")
 83	}
 84	if postid == threadid {
 85		reply := thread.AddReply(caller, body)
 86		return reply.id
 87	} else {
 88		post := thread.GetReply(postid)
 89		reply := post.AddReply(caller, body)
 90		return reply.id
 91	}
 92}
 93
 94// If dstBoard is private, does not ping back.
 95// If board specified by bid is private, panics.
 96func CreateRepost(cur realm, bid BoardID, postid PostID, title string, body string, dstBoardID BoardID) PostID {
 97	if !std.PreviousRealm().IsUser() {
 98		panic("invalid non-user call")
 99	}
100
101	caller := std.OriginCaller()
102	if usernameOf(caller) == "" {
103		// TODO: allow with gDefaultAnonFee payment.
104		if !checkAnonFee() {
105			panic("please register, otherwise minimum fee " + strconv.Itoa(gDefaultAnonFee) + " is required if anonymous")
106		}
107	}
108	board := getBoard(bid)
109	if board == nil {
110		panic("src board not exist")
111	}
112	if board.IsPrivate() {
113		panic("cannot repost from a private board")
114	}
115	dst := getBoard(dstBoardID)
116	if dst == nil {
117		panic("dst board not exist")
118	}
119	thread := board.GetThread(postid)
120	if thread == nil {
121		panic("thread not exist")
122	}
123	repost := thread.AddRepostTo(caller, title, body, dst)
124	return repost.id
125}
126
127func DeletePost(cur realm, bid BoardID, threadid, postid PostID, reason string) {
128	if !std.PreviousRealm().IsUser() {
129		panic("invalid non-user call")
130	}
131
132	caller := std.OriginCaller()
133	board := getBoard(bid)
134	if board == nil {
135		panic("board not exist")
136	}
137	thread := board.GetThread(threadid)
138	if thread == nil {
139		panic("thread not exist")
140	}
141	if postid == threadid {
142		// delete thread
143		if !thread.HasPermission(caller, DeletePermission) {
144			panic("unauthorized")
145		}
146		board.DeleteThread(threadid)
147	} else {
148		// delete thread's post
149		post := thread.GetReply(postid)
150		if post == nil {
151			panic("post not exist")
152		}
153		if !post.HasPermission(caller, DeletePermission) {
154			panic("unauthorized")
155		}
156		thread.DeletePost(postid)
157	}
158}
159
160func EditPost(cur realm, bid BoardID, threadid, postid PostID, title, body string) {
161	if !std.PreviousRealm().IsUser() {
162		panic("invalid non-user call")
163	}
164
165	caller := std.OriginCaller()
166	board := getBoard(bid)
167	if board == nil {
168		panic("board not exist")
169	}
170	thread := board.GetThread(threadid)
171	if thread == nil {
172		panic("thread not exist")
173	}
174	if postid == threadid {
175		// edit thread
176		if !thread.HasPermission(caller, EditPermission) {
177			panic("unauthorized")
178		}
179		thread.Update(title, body)
180	} else {
181		// edit thread's post
182		post := thread.GetReply(postid)
183		if post == nil {
184			panic("post not exist")
185		}
186		if !post.HasPermission(caller, EditPermission) {
187			panic("unauthorized")
188		}
189		post.Update(title, body)
190	}
191}