-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguiapi.go
268 lines (214 loc) · 5.83 KB
/
guiapi.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"time"
"github.com/mitchellh/mapstructure"
)
const GUIAPITimeout = 60 * time.Second
const MaxBufSize = 1024 * 1024
// Command defines the general structure how the GUI sends commands to the PeerBackend
// In JSON it looks like this:
// { "command": "someCommand", "arguments": {...} }
type Command struct {
Command string
Arguments map[string]interface{}
}
// GUIAPI is simply used as namespace
type GUIAPI struct {
*Node
}
// cmd2func maps the command with its respective handler function
var cmd2func map[string]func(args map[string]interface{}) string
// STartGUIPipe is a blocking loop providing a pipe for the GUI to
// interact with the PeerBackend. Use EOF (CTRL+D) to gracefully close
// the pipe
func StartGUIPipe(n *Node) {
gAPI := GUIAPI{n}
// cmd2func maps the command with its respective handler function
cmd2func = map[string]func(args map[string]interface{}) string{
"getPost": gAPI.getPost,
"getComment": gAPI.getComment,
"getPosts": gAPI.getPosts,
"postPost": gAPI.postPost,
"getCommentsFromPost": gAPI.getCommentsFromPost,
"postComment": gAPI.postComment,
"setPostUserData": gAPI.setPostUserData,
"setCommentUserData": gAPI.setCommentUserData,
"upvote": gAPI.upvote,
"downvote": gAPI.downvote,
"flag": gAPI.flag,
}
reader := bufio.NewReader(os.Stdin)
for {
var cmd Command
input, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
fmt.Println(`{"error": "JSON object not well formed."}`)
continue
}
err = json.Unmarshal([]byte(input), &cmd)
if err != nil {
fmt.Println(`{"error": "JSON object not well formed."}`)
continue
}
resp := GUIHandle(cmd)
// This ensures that we always return something as response
if resp == "" {
fmt.Println(`{"status": "ok"}`)
} else {
fmt.Println(resp)
}
}
}
func GUIHandle(cmd Command) string {
handler, ok := cmd2func[cmd.Command]
if !ok {
return `{"error": "No such command."}`
}
ch := make(chan string, 1)
go func() {
ch <- handler(cmd.Arguments)
}()
select {
case resp := <-ch:
return resp
case <-time.After(GUIAPITimeout):
return `{"error": "Timelimit exceeded."}`
}
}
//////////////////////////////////////////////////////////////////////
// handler functions
func (n *GUIAPI) getPost(args map[string]interface{}) string {
hash, ok := args["hash"].(string)
if !ok {
return `{"error": "Argument not well formatted."}`
}
post, err := n.Node.GetPost(hash)
if err != nil {
return `{"error": "` + err.Error() + `"}`
}
res, _ := json.Marshal(post)
return string(res)
}
func (n *GUIAPI) getComment(args map[string]interface{}) string {
hash, ok := args["hash"].(string)
if !ok {
return `{"error": "Argument not well formatted."}`
}
comment, err := n.GetComment(hash)
if err != nil {
return `{"error": "` + err.Error() + `"}`
}
res, _ := json.Marshal(comment)
return string(res)
}
func (n *GUIAPI) postPost(args map[string]interface{}) string {
content, ok := args["content"].(string)
if !ok {
return `{"error": "Argument not well formatted."}`
}
title, ok := args["title"].(string)
if !ok {
return `{"error": "Argument not well formatted."}`
}
obj, err := n.NewPost(MyUser, title, content)
if err != nil {
return `{"error": "` + err.Error() + `"}`
}
return `{"hash": "` + obj.Hash + `"}`
}
func (n *GUIAPI) getPosts(args map[string]interface{}) string {
posts, err := n.GetPosts()
if err != nil {
return `{"error": "` + err.Error() + `"}`
}
js, _ := json.Marshal(posts)
return string(js)
}
func (n *GUIAPI) getCommentsFromPost(args map[string]interface{}) string {
hash, ok := args["hash"].(string)
if !ok {
return `{"error": "Argument not well formatted."}`
}
comments, err := n.GetComments(hash)
if err != nil {
return `{"error": "` + err.Error() + `"}`
}
js, _ := json.Marshal(comments)
return string(js)
}
type postCommentArgs struct {
Post string
Content string
Parent string
}
func (n *GUIAPI) postComment(args map[string]interface{}) string {
pArgs := postCommentArgs{}
mapstructure.Decode(args, &pArgs)
obj, err := n.NewComment(MyUser, pArgs.Post, pArgs.Parent, pArgs.Content)
if err != nil {
return `{"error": "` + err.Error() + `"}`
}
return `{"hash": "` + obj.Hash + `"}`
}
type setPostUserDataArgs struct {
Hash string
UserData PostUserData
}
func (n *GUIAPI) setPostUserData(args map[string]interface{}) string {
pArgs := setPostUserDataArgs{}
mapstructure.Decode(args, &pArgs)
err := n.SetPostUserData(pArgs.Hash, pArgs.UserData)
if err != nil {
return `{"error": "` + err.Error() + `"}`
}
return `{"status": "success"}`
}
type setCommentUserDataArgs struct {
Hash string
UserData CommentUserData
}
func (n *GUIAPI) setCommentUserData(args map[string]interface{}) string {
pArgs := setCommentUserDataArgs{}
mapstructure.Decode(args, &pArgs)
err := n.SetCommentUserData(pArgs.Hash, pArgs.UserData)
if err != nil {
return `{"error": "` + err.Error() + `"}`
}
return `{"status": "success"}`
}
func (n *GUIAPI) upvote(args map[string]interface{}) string {
hash, ok := args["hash"].(string)
if !ok {
return `{"error": "Argument not well formatted."}`
}
MyCurator.UpvoteContent(hash)
return `{"status": "success"}`
}
func (n *GUIAPI) downvote(args map[string]interface{}) string {
hash, ok := args["hash"].(string)
if !ok {
return `{"error": "Argument not well formatted."}`
}
MyCurator.DownvoteContent(hash)
return `{"status": "success"}`
}
func (n *GUIAPI) flag(args map[string]interface{}) string {
hash, ok := args["hash"].(string)
if !ok {
return `{"error": "Argument not well formatted."}`
}
isFlagged, ok := args["isFlagged"].(bool)
if !ok {
return `{"error": "Argument not well formatted."}`
}
MyCurator.FlagContent(hash, isFlagged)
return `{"status": "success"}`
}