-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomment_api.go
More file actions
168 lines (150 loc) · 4.09 KB
/
comment_api.go
File metadata and controls
168 lines (150 loc) · 4.09 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
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/security"
)
type comment struct {
Id string `json:"id"`
Created string `json:"created"`
Author string `json:"author"`
Avatar string `json:"avatar"`
Website string `json:"website,omitempty"`
Content string `json:"content"`
IsMod bool `json:"isMod"`
PId string `json:"pid,omitempty"`
RId string `json:"rid,omitempty"`
Replies []comment `json:"replies"`
}
type newComment struct {
Uri string `json:"uri"`
Author string `json:"author"`
Email string `json:"email"`
Website string `json:"website"`
Content string `json:"content"`
PId string `json:"pid"`
RId string `json:"rid"`
}
// SetupCommentAPI 设置评论相关的路由
func SetupCommentAPI(app *pocketbase.PocketBase, se *core.ServeEvent) {
se.Router.GET("/api/comment", func(e *core.RequestEvent) error {
return GetComment(app, e)
})
se.Router.POST("/api/comment", func(e *core.RequestEvent) error {
return PostComment(app, e)
})
}
// GetComments 获取指定URI的评论列表
func GetComment(app *pocketbase.PocketBase, e *core.RequestEvent) error {
uri := e.Request.URL.Query().Get("uri")
pageStr := e.Request.URL.Query().Get("page")
page, err := strconv.Atoi(pageStr)
if err != nil {
page = 1
}
commentsPerPage := 10
offset := (page - 1) * commentsPerPage
comments := []comment{}
count, err := app.CountRecords("comments", dbx.HashExp{"uri": uri})
records, err := app.FindRecordsByFilter(
"comments",
"uri = {:uri} && pid = ''",
"-created",
commentsPerPage,
offset,
dbx.Params{"uri": uri},
)
if err != nil {
return err
}
errs := app.ExpandRecords(records, []string{"comments_via_pid"}, nil)
if len(errs) > 0 {
return fmt.Errorf("Failed to expand: %v", errs)
}
for _, v := range records {
item := comment{
v.Id,
v.GetString("created"),
v.GetString("author"),
security.MD5(v.GetString("email")),
v.GetString("website"),
v.GetString("content"),
v.GetBool("isMod"),
v.GetString("pid"),
v.GetString("rid"),
[]comment{},
}
replies := v.ExpandedAll("comments_via_pid")
if len(replies) > 0 {
for _, v := range replies {
replyItem := comment{
v.Id,
v.GetString("created"),
v.GetString("author"),
security.MD5(v.GetString("email")),
v.GetString("website"),
v.GetString("content"),
v.GetBool("isMod"),
v.GetString("pid"),
v.GetString("rid"),
[]comment{},
}
item.Replies = append(item.Replies, replyItem)
}
}
comments = append(comments, item)
}
body := struct {
Uri string `json:"uri"`
Page int `json:"page"`
CommentsPerPage int `json:"commentsPerPage"`
Count int64 `json:"count"`
Comments []comment `json:"comments"`
}{uri, page, commentsPerPage, count, comments}
return e.JSON(http.StatusOK, body)
}
// PostComment 处理新评论提交
func PostComment(app *pocketbase.PocketBase, e *core.RequestEvent) error {
newComment := new(newComment)
if err := e.BindBody(&newComment); err != nil {
return e.BadRequestError("Failed to read request body", err)
}
collection, err := app.FindCollectionByNameOrId("comments")
if err != nil {
return err
}
isMod := newComment.Email == os.Getenv("COMMENT_ADMIN_EMAIL")
record := core.NewRecord(collection)
record.Load(map[string]any{
"uri": newComment.Uri,
"author": newComment.Author,
"email": newComment.Email,
"website": newComment.Website,
"content": newComment.Content,
"pid": newComment.PId,
"rid": newComment.RId,
"isMod": isMod,
})
err = app.Save(record)
if err != nil {
return err
}
body := comment{
record.Id,
record.GetString("created"),
record.GetString("author"),
security.MD5(record.GetString("email")),
record.GetString("website"),
record.GetString("content"),
record.GetBool("isMod"),
record.GetString("pid"),
record.GetString("rid"),
[]comment{},
}
return e.JSON(http.StatusOK, body)
}