-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
post.go
192 lines (171 loc) · 4.03 KB
/
post.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
// Various POST request handlers
package server
import (
"database/sql"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/cutechan/cutechan/go/auth"
"github.com/cutechan/cutechan/go/config"
"github.com/cutechan/cutechan/go/db"
"github.com/cutechan/cutechan/go/feeds"
"github.com/cutechan/cutechan/go/websockets"
)
// Serve a single post as JSON
func servePost(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseUint(getParam(r, "post"), 10, 64)
if err != nil {
text400(w, err)
return
}
switch post, err := db.GetPost(id); err {
case nil:
ss, _ := getSession(r, post.Board)
if !assertNotModOnly(w, r, post.Board, ss) {
return
}
serveJSON(w, r, post)
case sql.ErrNoRows:
serve404(w, r)
default:
respondToJSONError(w, r, err)
}
}
// Client should get token and solve challenge in order to post.
func createPostToken(w http.ResponseWriter, r *http.Request) {
ip, err := auth.GetIP(r)
if err != nil {
text400(w, err)
return
}
token, err := db.NewPostToken(ip)
switch err {
case nil:
case db.ErrTokenForbidden:
text403(w, err)
return
default:
text500(w, r, err)
return
}
res := map[string]string{"id": token}
serveJSON(w, r, res)
}
// Create thread.
func createThread(w http.ResponseWriter, r *http.Request) {
postReq, ok := parsePostCreationForm(w, r)
if !ok {
return
}
// Map form data to websocket thread creation request.
subject := r.Form.Get("subject")
req := websockets.ThreadCreationRequest{
PostCreationRequest: postReq,
Subject: subject,
}
post, err := websockets.CreateThread(req)
if err != nil {
// TODO(Kagami): Not all errors are 400.
// TODO(Kagami): Write JSON errors instead.
text400(w, err)
return
}
res := map[string]uint64{"id": post.ID}
serveJSON(w, r, res)
}
// Create post.
func createPost(w http.ResponseWriter, r *http.Request) {
req, ok := parsePostCreationForm(w, r)
if !ok {
return
}
// Check board and thread.
thread := r.Form.Get("thread")
op, err := strconv.ParseUint(thread, 10, 64)
if err != nil {
text400(w, err)
return
}
ok, err = db.ValidateOP(op, req.Board)
if err != nil {
text500(w, r, err)
return
}
if !ok {
text400(w, fmt.Errorf("invalid thread: /%s/%d", req.Board, op))
return
}
post, msg, err := websockets.CreatePost(req, op)
if err != nil {
text400(w, err)
return
}
feeds.InsertPostInto(post.StandalonePost, msg)
res := map[string]uint64{"id": post.ID}
serveJSON(w, r, res)
}
// ok = false if failed and caller should return.
func parsePostCreationForm(w http.ResponseWriter, r *http.Request) (
req websockets.PostCreationRequest, ok bool,
) {
f, m, err := parseUploadForm(w, r)
if err != nil {
serveErrorJSON(w, r, err)
return
}
// Board and user validation.
board := f.Get("board")
if !assertBoardAPI(w, board) {
return
}
if board == "all" {
text400(w, errInvalidBoard)
return
}
ss, _ := getSession(r, board)
if !assertNotModOnlyAPI(w, board, ss) {
return
}
if !assertNotReadOnlyAPI(w, board, ss) {
return
}
ip, allowed := assertNotBannedAPI(w, r, board)
if !allowed {
return
}
fhs := m.File["files[]"]
if len(fhs) > config.Get().MaxFiles {
serveErrorJSON(w, r, aerrTooManyFiles)
return
}
tokens := make([]string, len(fhs))
for i, fh := range fhs {
res, err := uploadFile(fh)
if err != nil {
serveErrorJSON(w, r, err)
return
}
tokens[i] = res.token
}
// NOTE(Kagami): Browsers use CRLF newlines in form-data requests,
// see: <https://stackoverflow.com/a/6964163>.
// This in particular breaks links formatting, also we need to be
// consistent with WebSocket requests and store normalized data in DB.
body := f.Get("body")
body = strings.Replace(body, "\r\n", "\n", -1)
modOnly := config.IsModOnlyBoard(board)
req = websockets.PostCreationRequest{
FilesRequest: websockets.FilesRequest{tokens},
Board: board,
Ip: ip,
Body: body,
Token: f.Get("token"),
Sign: f.Get("sign"),
ShowBadge: f.Get("showBadge") == "on" || modOnly,
ShowName: modOnly,
Session: ss,
}
ok = true
return
}