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