-
Notifications
You must be signed in to change notification settings - Fork 38
/
post.go
313 lines (271 loc) · 7.41 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package server
import (
"io"
"net/http"
"strings"
"time"
"github.com/discuitnet/discuit/core"
"github.com/discuitnet/discuit/internal/httperr"
"github.com/discuitnet/discuit/internal/uid"
)
// /api/posts [POST]
func (s *Server) addPost(w *responseWriter, r *request) error {
if !r.loggedIn {
return errNotLoggedIn
}
if err := s.rateLimit(r, "add_post_1_"+r.viewer.String(), time.Second*10, 1); err != nil {
return err
}
if err := s.rateLimit(r, "add_post_2_"+r.viewer.String(), time.Hour*24, 70); err != nil {
return err
}
values, err := r.unmarshalJSONBodyToStringsMap(true)
if err != nil {
return err
}
var postType core.PostType = core.PostTypeText
if values["type"] != "" {
if err = postType.UnmarshalText([]byte(values["type"])); err != nil {
return err
}
}
if s.config.DisableImagePosts && postType == core.PostTypeImage {
// Disallow image post creation.
return httperr.NewForbidden("no_image_posts", "Image posts are not allowed")
}
title := values["title"] // required
body := values["body"]
commName := values["community"] // required
userGroup := core.UserGroupNormal
if text := values["userGroup"]; text != "" {
if err := userGroup.UnmarshalText([]byte(text)); err != nil {
return err
}
}
comm, err := core.GetCommunityByName(r.ctx, s.db, commName, nil)
if err != nil {
return err
}
var post *core.Post
switch postType {
case core.PostTypeText:
post, err = core.CreateTextPost(r.ctx, s.db, *r.viewer, comm.ID, title, body)
case core.PostTypeImage:
imageID, idErr := uid.FromString(values["imageId"])
if idErr != nil {
return httperr.NewBadRequest("invalid_image_id", "Invalid image ID.")
}
post, err = core.CreateImagePost(r.ctx, s.db, *r.viewer, comm.ID, title, imageID)
case core.PostTypeLink:
post, err = core.CreateLinkPost(r.ctx, s.db, *r.viewer, comm.ID, title, values["url"])
default:
return httperr.NewBadRequest("invalid_post_type", "Invalid post type.")
}
if err != nil {
return err
}
if userGroup != core.UserGroupNormal {
if err := post.ChangeUserGroup(r.ctx, *r.viewer, userGroup); err != nil {
return err
}
}
// +1 your own post.
post.Vote(r.ctx, *r.viewer, true)
return w.writeJSON(post)
}
// /api/posts/:postID [GET]
func (s *Server) getPost(w *responseWriter, r *request) error {
postID := r.muxVar("postID") // public post id
post, err := core.GetPost(r.ctx, s.db, nil, postID, r.viewer, true)
if err != nil {
return err
}
if _, err = post.GetComments(r.ctx, r.viewer, nil); err != nil {
return err
}
if fetchCommunity := r.urlQueryValue("fetchCommunity"); fetchCommunity == "" || fetchCommunity == "true" {
comm, err := core.GetCommunityByID(r.ctx, s.db, post.CommunityID, r.viewer)
if err != nil {
return err
}
if err = comm.FetchRules(r.ctx); err != nil {
return err
}
if err = comm.PopulateMods(r.ctx); err != nil {
return err
}
post.Community = comm
}
return w.writeJSON(post)
}
// /api/posts/:postID [PUT]
func (s *Server) updatePost(w *responseWriter, r *request) error {
postID := r.muxVar("postID") // public post id
if !r.loggedIn {
return errNotLoggedIn
}
if err := s.rateLimitUpdateContent(r, *r.viewer); err != nil {
return err
}
post, err := core.GetPost(r.ctx, s.db, nil, postID, r.viewer, true)
if err != nil {
return err
}
query := r.urlQuery()
action := query.Get("action")
if action == "" {
// Update post.
var tpost core.Post
if err = r.unmarshalJSONBody(&tpost); err != nil {
return err
}
tpost.Title = strings.TrimSpace(tpost.Title)
tpost.Body.String = strings.TrimSpace(tpost.Body.String)
// override updatable fields
needSaving := false
if post.Type == core.PostTypeText && !post.DeletedContent {
if post.Body != tpost.Body {
needSaving = true
post.Body = tpost.Body
}
}
if post.Title != tpost.Title {
needSaving = true
post.Title = tpost.Title
}
if needSaving {
if err = post.Save(r.ctx, *r.viewer); err != nil {
return err
}
}
} else {
switch action {
case "lock", "unlock":
var as core.UserGroup
if err = as.UnmarshalText([]byte(query.Get("lockAs"))); err != nil {
return err
}
if action == "lock" {
err = post.Lock(r.ctx, *r.viewer, as)
} else {
err = post.Unlock(r.ctx, *r.viewer)
}
if err != nil {
return err
}
case "changeAsUser":
var as core.UserGroup
if err = as.UnmarshalText([]byte(query.Get("userGroup"))); err != nil {
return err
}
if err = post.ChangeUserGroup(r.ctx, *r.viewer, as); err != nil {
return err
}
case "pin", "unpin":
siteWide := strings.ToLower(query.Get("siteWide")) == "true"
if err = post.Pin(r.ctx, *r.viewer, siteWide, action == "unpin", false); err != nil {
return err
}
default:
return httperr.NewBadRequest("invalid_action", "Unsupported action.")
}
}
return w.writeJSON(post)
}
// /api/posts/:postID [DELETE]
func (s *Server) deletePost(w *responseWriter, r *request) error {
postID := r.muxVar("postID") // public post id
if !r.loggedIn {
return errNotLoggedIn
}
if err := s.rateLimitUpdateContent(r, *r.viewer); err != nil {
return err
}
post, err := core.GetPost(r.ctx, s.db, nil, postID, r.viewer, true)
if err != nil {
return err
}
query := r.urlQuery()
var as core.UserGroup
if err = as.UnmarshalText([]byte(query.Get("deleteAs"))); err != nil {
return err
}
deleteContent := false
if dc := strings.ToLower(query.Get("deleteContent")); dc != "" {
if dc == "true" {
deleteContent = true
} else if dc != "false" {
return httperr.NewBadRequest("", "deleteContent must be a bool.")
}
}
if err := post.Delete(r.ctx, *r.viewer, as, deleteContent); err != nil {
return err
}
return w.writeJSON(post)
}
// /api/_postVote [ POST ]
func (s *Server) postVote(w *responseWriter, r *request) error {
if !r.loggedIn {
return errNotLoggedIn
}
if err := s.rateLimitVoting(r, *r.viewer); err != nil {
return err
}
req := struct {
PostID uid.ID `json:"postId"`
Up bool `json:"up"`
}{Up: true}
if err := r.unmarshalJSONBody(&req); err != nil {
return err
}
post, err := core.GetPost(r.ctx, s.db, &req.PostID, "", r.viewer, true)
if err != nil {
return err
}
if post.ViewerVoted.Bool {
if req.Up == post.ViewerVotedUp.Bool {
err = post.DeleteVote(r.ctx, *r.viewer)
} else {
err = post.ChangeVote(r.ctx, *r.viewer, req.Up)
}
} else {
err = post.Vote(r.ctx, *r.viewer, req.Up)
}
if err != nil {
return err
}
return w.writeJSON(post)
}
// /api/_uploads [ POST ]
func (s *Server) imageUpload(w *responseWriter, r *request) error {
if s.config.DisableImagePosts {
return httperr.NewForbidden("no_image_posts", "Image posts are not all allowed.")
}
if !r.loggedIn {
return errNotLoggedIn
}
if err := s.rateLimit(r, "uploads_1_"+r.viewer.String(), time.Second*2, 1); err != nil {
return err
}
if err := s.rateLimit(r, "uploads_2_"+r.viewer.String(), time.Hour*24, 40); err != nil {
return err
}
r.req.Body = http.MaxBytesReader(w, r.req.Body, int64(s.config.MaxImageSize)) // limit max upload size
if err := r.req.ParseMultipartForm(int64(s.config.MaxImageSize)); err != nil {
return httperr.NewBadRequest("file_size_exceeded", "Max file size exceeded.")
}
file, _, err := r.req.FormFile("image")
if err != nil {
return err
}
defer file.Close()
fileData, err := io.ReadAll(file)
if err != nil {
return err
}
image, err := core.SavePostImage(r.ctx, s.db, *r.viewer, fileData)
if err != nil {
return err
}
return w.writeJSON(image.Image())
}