Search Apps Documentation Source Content File Folder Download Copy

public.gno

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