forked from carl-mastrangelo/pixur
/
comment.go
125 lines (109 loc) · 2.81 KB
/
comment.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
package handlers
import (
"net/http"
"pixur.org/pixur/api"
"pixur.org/pixur/fe/server"
ptpl "pixur.org/pixur/fe/tpl"
)
var commentDisplayTpl = parseTpl(ptpl.Base, ptpl.Pane, ptpl.Comment, ptpl.CommentReply)
type commentDisplayData struct {
*paneData
Pic *api.Pic
PicComment *picComment
// CommentText is the initial comment after a failed write
CommentText string
}
type commentDisplayHandler struct {
pt *paths
c api.PixurServiceClient
}
func (h *commentDisplayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
commentParentId := r.FormValue(h.pt.pr.CommentParentId())
picId := r.FormValue(h.pt.pr.PicId())
req := &api.LookupPicDetailsRequest{
PicId: picId,
}
details, err := h.c.LookupPicDetails(ctx, req)
if err != nil {
httpReadError(ctx, w, err)
return
}
pd := newPaneData(ctx, "Add Comment", h.pt)
var root *picComment
if details.PicCommentTree != nil && len(details.PicCommentTree.Comment) > 0 {
m := make(map[string][]*picComment)
for _, c := range details.PicCommentTree.Comment {
pc := &picComment{
PicComment: c,
Child: m[c.CommentId],
Paths: pd.Paths,
XsrfToken: pd.XsrfToken,
}
if pc.CommentId == commentParentId {
root = pc
break
}
m[c.CommentParentId] = append(m[c.CommentParentId], pc)
}
}
if root == nil {
httpReadError(ctx, w, &HTTPErr{
Message: "Can't find comment id",
Code: http.StatusBadRequest,
})
return
}
data := &commentDisplayData{
paneData: pd,
Pic: details.Pic,
PicComment: root,
CommentText: r.PostFormValue(h.pt.pr.CommentText()),
}
if err := commentDisplayTpl.Execute(w, data); err != nil {
httpCleanupError(w, err)
return
}
}
type addPicCommentHandler struct {
pt *paths
c api.PixurServiceClient
display http.Handler
}
func (h *addPicCommentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req := &api.AddPicCommentRequest{
PicId: r.PostFormValue(h.pt.pr.PicId()),
CommentParentId: r.PostFormValue(h.pt.pr.CommentParentId()),
Text: r.PostFormValue(h.pt.pr.CommentText()),
}
ctx := r.Context()
res, err := h.c.AddPicComment(ctx, req)
if err != nil {
httpWriteError(w, err)
ctx = ctxFromWriteErr(ctx, err)
r = r.WithContext(ctx)
h.display.ServeHTTP(w, r)
return
}
http.Redirect(w, r, h.pt.ViewerComment(res.Comment.PicId, res.Comment.CommentId).String(), http.StatusSeeOther)
}
func init() {
register(func(s *server.Server) error {
pt := &paths{r: s.HTTPRoot}
cdh := readWrapper(s)(&commentDisplayHandler{
c: s.Client,
pt: pt,
})
apch := writeWrapper(s)(&addPicCommentHandler{
c: s.Client,
pt: pt,
display: cdh,
})
h := compressHtmlHandler(&methodHandler{
Get: cdh,
Post: apch,
})
s.HTTPMux.Handle(pt.Comment().Path, h)
return nil
})
}