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