-
-
Notifications
You must be signed in to change notification settings - Fork 815
Expand file tree
/
Copy pathpost.go
More file actions
388 lines (316 loc) · 11.5 KB
/
Copy pathpost.go
File metadata and controls
388 lines (316 loc) · 11.5 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package actions
import (
"context"
"regexp"
"strings"
"time"
"github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/entity"
"github.com/getfider/fider/app/models/enum"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/i18n"
"github.com/gosimple/slug"
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/validate"
)
// CreateNewPost is used to create a new post
type CreateNewPost struct {
Title string `json:"title"`
Description string `json:"description"`
TagSlugs []string `json:"tags"`
Attachments []*dto.ImageUpload `json:"attachments"`
Tags []*entity.Tag
}
// OnPreExecute prefetches Tags for later use
func (input *CreateNewPost) OnPreExecute(ctx context.Context) error {
if env.Config.PostCreationWithTagsEnabled {
input.Tags = make([]*entity.Tag, 0, len(input.TagSlugs))
for _, slug := range input.TagSlugs {
getTag := &query.GetTagBySlug{Slug: slug}
if err := bus.Dispatch(ctx, getTag); err != nil {
break
}
input.Tags = append(input.Tags, getTag.Result)
}
}
return nil
}
// IsAuthorized returns true if current user is authorized to perform this action
func (action *CreateNewPost) IsAuthorized(ctx context.Context, user *entity.User) bool {
if user == nil {
return false
} else if env.Config.PostCreationWithTagsEnabled && !user.IsCollaborator() {
for _, tag := range action.Tags {
if !tag.IsPublic {
return false
}
}
}
return true
}
// Validate if current model is valid
func (action *CreateNewPost) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
re := regexp.MustCompile(`\s+`)
normalizedTitle := strings.TrimSpace(re.ReplaceAllString(action.Title, " "))
if normalizedTitle == "" {
result.AddFieldFailure("title", propertyIsRequired(ctx, "title"))
} else if len(normalizedTitle) < 10 {
result.AddFieldFailure("title", i18n.T(ctx, "validation.custom.descriptivetitle"))
} else if len(normalizedTitle) > 100 {
result.AddFieldFailure("title", propertyMaxStringLen(ctx, "title", 100))
} else if env.Config.PostCreationWithTagsEnabled && len(action.TagSlugs) != len(action.Tags) {
result.AddFieldFailure("tags", propertyIsInvalid(ctx, "tags"))
} else {
err := bus.Dispatch(ctx, &query.GetPostBySlug{Slug: slug.Make(action.Title)})
if err != nil && errors.Cause(err) != app.ErrNotFound {
return validate.Error(err)
} else if err == nil {
result.AddFieldFailure("title", i18n.T(ctx, "validation.custom.duplicatetitle"))
}
}
messages, err := validate.MultiImageUpload(ctx, nil, action.Attachments, validate.MultiImageUploadOpts{
MaxUploads: 3,
MaxKilobytes: 5120,
ExactRatio: false,
})
if err != nil {
return validate.Error(err)
}
result.AddFieldFailure("attachments", messages...)
return result
}
// UpdatePost is used to edit an existing new post
type UpdatePost struct {
Number int `route:"number"`
Title string `json:"title"`
Description string `json:"description"`
Attachments []*dto.ImageUpload `json:"attachments"`
Post *entity.Post
}
// OnPreExecute prefetches Post for later use
func (input *UpdatePost) OnPreExecute(ctx context.Context) error {
getPost := &query.GetPostByNumber{Number: input.Number}
if err := bus.Dispatch(ctx, getPost); err != nil {
return err
}
input.Post = getPost.Result
return nil
}
// IsAuthorized returns true if current user is authorized to perform this action
func (input *UpdatePost) IsAuthorized(ctx context.Context, user *entity.User) bool {
if user.IsCollaborator() {
return true
}
timeAgo := time.Now().UTC().Sub(input.Post.CreatedAt)
return input.Post.User.ID == user.ID && timeAgo <= 1*time.Hour
}
// Validate if current model is valid
func (action *UpdatePost) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
if action.Title == "" {
result.AddFieldFailure("title", propertyIsRequired(ctx, "title"))
} else if len(action.Title) < 10 {
result.AddFieldFailure("title", i18n.T(ctx, "validation.custom.descriptivetitle"))
} else if len(action.Title) > 100 {
result.AddFieldFailure("title", propertyMaxStringLen(ctx, "title", 100))
}
postBySlug := &query.GetPostBySlug{Slug: slug.Make(action.Title)}
err := bus.Dispatch(ctx, postBySlug)
if err != nil && errors.Cause(err) != app.ErrNotFound {
return validate.Error(err)
} else if err == nil && postBySlug.Result.ID != action.Post.ID {
result.AddFieldFailure("title", i18n.T(ctx, "validation.custom.duplicatetitle"))
}
if len(action.Attachments) > 0 {
getAttachments := &query.GetAttachments{Post: action.Post}
err = bus.Dispatch(ctx, getAttachments)
if err != nil {
return validate.Error(err)
}
messages, err := validate.MultiImageUpload(ctx, getAttachments.Result, action.Attachments, validate.MultiImageUploadOpts{
MaxUploads: 3,
MaxKilobytes: 5120,
ExactRatio: false,
})
if err != nil {
return validate.Error(err)
}
result.AddFieldFailure("attachments", messages...)
}
return result
}
type ToggleCommentReaction struct {
Number int `route:"number"`
Comment int `route:"id"`
Reaction string `route:"reaction"`
}
// IsAuthorized returns true if current user is authorized to perform this action
func (action *ToggleCommentReaction) IsAuthorized(ctx context.Context, user *entity.User) bool {
return user != nil
}
// Validate if current model is valid
func (action *ToggleCommentReaction) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
allowedEmojis := []string{"👍", "👎", "😄", "🎉", "😕", "❤️", "🚀", "👀"}
isAllowed := false
for _, emoji := range allowedEmojis {
if action.Reaction == emoji {
isAllowed = true
break
}
}
if !isAllowed {
result.AddFieldFailure("reaction", i18n.T(ctx, "validation.custom.invalidemoji"))
}
return result
}
// AddNewComment represents a new comment to be added
type AddNewComment struct {
Number int `route:"number"`
Content string `json:"content"`
Attachments []*dto.ImageUpload `json:"attachments"`
}
// IsAuthorized returns true if current user is authorized to perform this action
func (action *AddNewComment) IsAuthorized(ctx context.Context, user *entity.User) bool {
return user != nil
}
// Validate if current model is valid
func (action *AddNewComment) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
if action.Content == "" {
result.AddFieldFailure("content", propertyIsRequired(ctx, "comment"))
}
messages, err := validate.MultiImageUpload(ctx, nil, action.Attachments, validate.MultiImageUploadOpts{
MaxUploads: 2,
MaxKilobytes: 5120,
ExactRatio: false,
})
if err != nil {
return validate.Error(err)
}
result.AddFieldFailure("attachments", messages...)
return result
}
// SetResponse represents the action to update a post response
type SetResponse struct {
Number int `route:"number"`
Status enum.PostStatus `json:"status"`
Text string `json:"text"`
OriginalNumber int `json:"originalNumber"`
Original *entity.Post
}
// IsAuthorized returns true if current user is authorized to perform this action
func (action *SetResponse) IsAuthorized(ctx context.Context, user *entity.User) bool {
return user != nil && user.IsCollaborator()
}
// Validate if current model is valid
func (action *SetResponse) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
if action.Status < enum.PostOpen || action.Status > enum.PostDuplicate {
result.AddFieldFailure("status", propertyIsInvalid(ctx, "status"))
}
if action.Status == enum.PostDuplicate {
if action.OriginalNumber == action.Number {
result.AddFieldFailure("originalNumber", i18n.T(ctx, "validation.custom.selfduplicate"))
}
getOriginalPost := &query.GetPostByNumber{Number: action.OriginalNumber}
err := bus.Dispatch(ctx, getOriginalPost)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
result.AddFieldFailure("originalNumber", i18n.T(ctx, "validation.custom.originalpostnotfound"))
} else {
return validate.Error(err)
}
}
if getOriginalPost.Result != nil {
action.Original = getOriginalPost.Result
}
}
return result
}
// DeletePost represents the action of an administrator deleting an existing Post
type DeletePost struct {
Number int `route:"number"`
Text string `json:"text"`
Post *entity.Post
}
// IsAuthorized returns true if current user is authorized to perform this action
func (action *DeletePost) IsAuthorized(ctx context.Context, user *entity.User) bool {
return user != nil && user.IsAdministrator()
}
// Validate if current model is valid
func (action *DeletePost) Validate(ctx context.Context, user *entity.User) *validate.Result {
getPost := &query.GetPostByNumber{Number: action.Number}
if err := bus.Dispatch(ctx, getPost); err != nil {
return validate.Error(err)
}
action.Post = getPost.Result
isReferencedQuery := &query.PostIsReferenced{PostID: action.Post.ID}
if err := bus.Dispatch(ctx, isReferencedQuery); err != nil {
return validate.Error(err)
}
if isReferencedQuery.Result {
return validate.Failed(i18n.T(ctx, "validation.custom.cannotdeleteduplicatepost"))
}
return validate.Success()
}
// EditComment represents the action to update an existing comment
type EditComment struct {
PostNumber int `route:"number"`
ID int `route:"id"`
Content string `json:"content"`
Attachments []*dto.ImageUpload `json:"attachments"`
Post *entity.Post
Comment *entity.Comment
}
// IsAuthorized returns true if current user is authorized to perform this action
func (action *EditComment) IsAuthorized(ctx context.Context, user *entity.User) bool {
postByNumber := &query.GetPostByNumber{Number: action.PostNumber}
commentByID := &query.GetCommentByID{CommentID: action.ID}
if err := bus.Dispatch(ctx, postByNumber, commentByID); err != nil {
return false
}
action.Post = postByNumber.Result
action.Comment = commentByID.Result
return user.ID == action.Comment.User.ID || user.IsCollaborator()
}
// Validate if current model is valid
func (action *EditComment) Validate(ctx context.Context, user *entity.User) *validate.Result {
result := validate.Success()
if action.Content == "" {
result.AddFieldFailure("content", propertyIsRequired(ctx, "comment"))
}
if len(action.Attachments) > 0 {
nonRemovedCount := 0
for _, v := range action.Attachments {
if !v.Remove {
nonRemovedCount++
}
}
if nonRemovedCount > 2 {
result.AddFieldFailure("content", i18n.T(ctx, "validation.custom.maxattachments", i18n.Params{"number": 2}))
}
}
return result
}
// DeleteComment represents the action of deleting an existing comment
type DeleteComment struct {
PostNumber int `route:"number"`
CommentID int `route:"id"`
}
// IsAuthorized returns true if current user is authorized to perform this action
func (action *DeleteComment) IsAuthorized(ctx context.Context, user *entity.User) bool {
commentByID := &query.GetCommentByID{CommentID: action.CommentID}
if err := bus.Dispatch(ctx, commentByID); err != nil {
return false
}
return user.ID == commentByID.Result.User.ID || user.IsCollaborator()
}
// Validate if current model is valid
func (action *DeleteComment) Validate(ctx context.Context, user *entity.User) *validate.Result {
return validate.Success()
}