-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreview.go
More file actions
76 lines (62 loc) · 1.7 KB
/
review.go
File metadata and controls
76 lines (62 loc) · 1.7 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
package main
import (
"fmt"
"net/http"
"github.com/gofrs/uuid/v5"
"app/csrf"
)
func (h *handler) handleReview(w http.ResponseWriter, r *http.Request) error {
if err := r.ParseForm(); err != nil {
http.Error(w, "must be a valid form", http.StatusBadRequest)
return nil
}
if !csrf.Verify(r, r.PostForm.Get(csrf.FormName)) {
csrf.Clear(w, r)
http.Error(w, "csrf failed, please go-back and retry", http.StatusBadRequest)
return nil
}
patchID := r.PathValue("patch-id")
if patchID == "" {
http.Error(w, "missing patch id", http.StatusBadRequest)
return nil
}
id, err := uuid.FromString(patchID)
if err != nil {
http.Error(w, "invalid patch id, must be uuid", http.StatusBadRequest)
return nil
}
react := r.PostForm.Get("react")
switch react {
case "comment", "approve", "reject":
default:
http.Error(w, "invalid react type", http.StatusBadRequest)
return nil
}
patchType := r.PathValue("patch-type")
s, err := h.GetFreshSession(w, r, fmt.Sprintf("/%s/%s", patchType, patchID))
if err != nil {
return err
}
text := r.PostForm.Get("text")
if patchType == "review" {
if text == "" {
return &HttpError{
StatusCode: http.StatusBadRequest,
Message: "review text can't be empty",
}
}
}
switch patchType {
case "subject":
return h.handleSubjectReview(w, r, id, react, text, s)
case "episode":
return h.handleEpisodeReview(w, r, id, react, text, s)
}
http.Error(w, "invalid patch type", http.StatusBadRequest)
return nil
}
const ErrCodeValidationError = "REQUEST_VALIDATION_ERROR"
const ErrCodeWikiChanged = "WIKI_CHANGED"
const ErrCodeInvalidWikiSyntax = "INVALID_SYNTAX_ERROR"
const ErrCodeInvalidToken = "TOKEN_INVALID"
const ErrCodeItemLocked = "ITEM_LOCKED"