API契约:收口回复规则与账号任务响应
This commit is contained in:
1 parent
fe6fc3adb9
commit
f76fb08cbe
9 files changed
+535
-60
No files matched your search
@@ -45,30 +45,33 @@ func (s *Server) getDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
dr, err := s.Store.DefaultReps.Get(r.Context(), cid)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"enabled": false, "reply_content": "", "reply_once": false})
|
||||
writeJSON(w, http.StatusOK, defaultReplyResponse{Enabled: false, ReplyContent: "", ReplyOnce: false})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"enabled": dr.Enabled, "reply_content": dr.ReplyContent,
|
||||
"reply_image_url": dr.ReplyImageURL, "reply_once": dr.ReplyOnce,
|
||||
})
|
||||
// 已保存默认回复通过具名 DTO 返回,单账号查询不填充 cookie_id。
|
||||
writeJSON(w, http.StatusOK, newDefaultReplyResponse("", *dr))
|
||||
}
|
||||
|
||||
// setDefaultReply 保存指定账号的默认回复配置。
|
||||
func (s *Server) setDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
// cid 是当前操作的账号标识。
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
// req 是默认回复配置请求体。
|
||||
var req struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
ReplyContent string `json:"reply_content"`
|
||||
ReplyImageURL string `json:"reply_image_url"`
|
||||
ReplyOnce bool `json:"reply_once"`
|
||||
}
|
||||
// err 是请求体解码错误。
|
||||
if err := decodeJSON(r, &req); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
// err 是默认回复写入错误。
|
||||
_, err := s.Store.DB.ExecContext(r.Context(),
|
||||
`INSERT INTO default_replies (cookie_id, enabled, reply_content, reply_image_url, reply_once, updated_at)
|
||||
VALUES (?,?,?,?,?,CURRENT_TIMESTAMP)`+db.DialectUpsert(s.Store.Dialect, []string{"cookie_id"}, map[string]string{
|
||||
@@ -83,11 +86,14 @@ func (s *Server) setDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusInternalServerError, "保存失败")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true})
|
||||
writeJSON(w, http.StatusOK, operationResponse{Success: true})
|
||||
}
|
||||
|
||||
// listDefaultReplies 查询当前用户的默认回复列表。
|
||||
func (s *Server) listDefaultReplies(w http.ResponseWriter, r *http.Request) {
|
||||
// sess 是当前登录用户会话。
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
// err 是默认回复列表查询错误,rows 是查询结果游标。
|
||||
rows, err := s.Store.DB.QueryContext(r.Context(),
|
||||
`SELECT dr.cookie_id, dr.enabled, COALESCE(dr.reply_content,''), dr.reply_once, COALESCE(dr.reply_image_url,'')
|
||||
FROM default_replies dr
|
||||
@@ -98,18 +104,24 @@ func (s *Server) listDefaultReplies(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []map[string]any
|
||||
// out 是默认回复列表响应。
|
||||
var out []defaultReplyResponse
|
||||
for rows.Next() {
|
||||
// cid、content 和 imageURL 是当前默认回复的文本字段。
|
||||
var cid, content, imageURL string
|
||||
// enabled 和 replyOnce 是数据库中的布尔数值字段。
|
||||
var enabled, replyOnce int
|
||||
// err 是当前默认回复行扫描错误。
|
||||
if err := rows.Scan(&cid, &enabled, &content, &replyOnce, &imageURL); err != nil {
|
||||
continue
|
||||
}
|
||||
out = append(out, map[string]any{
|
||||
"cookie_id": cid, "enabled": enabled != 0, "reply_content": content,
|
||||
"reply_once": replyOnce != 0, "reply_image_url": imageURL,
|
||||
out = append(out, defaultReplyResponse{
|
||||
CookieID: cid, Enabled: enabled != 0, ReplyContent: content, ReplyOnce: replyOnce != 0,
|
||||
ReplyImageURL: imageURL,
|
||||
// 列表 DTO 保留账号标识,前端可直接建立按账号索引。
|
||||
})
|
||||
}
|
||||
// err 是默认回复游标迭代错误。
|
||||
if err := rows.Err(); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "查询失败")
|
||||
return
|
||||
@@ -117,8 +129,11 @@ func (s *Server) listDefaultReplies(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// listDefaultRepliesMap 查询按账号索引的默认回复映射。
|
||||
func (s *Server) listDefaultRepliesMap(w http.ResponseWriter, r *http.Request) {
|
||||
// sess 是当前登录用户会话。
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
// err 是默认回复映射查询错误,rows 是查询结果游标。
|
||||
rows, err := s.Store.DB.QueryContext(r.Context(),
|
||||
`SELECT dr.cookie_id, dr.enabled, COALESCE(dr.reply_content, ''), dr.reply_once, COALESCE(dr.reply_image_url, '')
|
||||
FROM default_replies dr
|
||||
@@ -129,21 +144,27 @@ func (s *Server) listDefaultRepliesMap(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make(map[string]any)
|
||||
// out 是按账号标识索引的默认回复映射。
|
||||
out := make(map[string]defaultReplyResponse)
|
||||
for rows.Next() {
|
||||
// cid、content 和 imageURL 是当前默认回复的文本字段。
|
||||
var cid, content, imageURL string
|
||||
// enabled 和 replyOnce 是数据库中的布尔数值字段。
|
||||
var enabled, replyOnce int
|
||||
// err 是当前默认回复行扫描错误。
|
||||
if err := rows.Scan(&cid, &enabled, &content, &replyOnce, &imageURL); err != nil {
|
||||
continue
|
||||
}
|
||||
out[cid] = map[string]any{
|
||||
"cookie_id": cid,
|
||||
"enabled": enabled != 0,
|
||||
"reply_content": content,
|
||||
"reply_once": replyOnce != 0,
|
||||
"reply_image_url": imageURL,
|
||||
out[cid] = defaultReplyResponse{
|
||||
CookieID: cid,
|
||||
Enabled: enabled != 0,
|
||||
ReplyContent: content,
|
||||
ReplyOnce: replyOnce != 0,
|
||||
ReplyImageURL: imageURL,
|
||||
// map 键与 cookie_id 同时保留,兼容旧前端索引方式。
|
||||
}
|
||||
}
|
||||
// err 是默认回复映射游标迭代错误。
|
||||
if err := rows.Err(); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "查询失败")
|
||||
return
|
||||
@@ -151,27 +172,33 @@ func (s *Server) listDefaultRepliesMap(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// deleteDefaultReply 删除指定账号的默认回复配置。
|
||||
func (s *Server) deleteDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
// cid 是当前操作的账号标识。
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
// err 是默认回复删除错误。
|
||||
_, err := s.Store.DB.ExecContext(r.Context(), `DELETE FROM default_replies WHERE cookie_id=?`, cid)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "删除失败")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true})
|
||||
writeJSON(w, http.StatusOK, operationResponse{Success: true})
|
||||
}
|
||||
|
||||
// clearDefaultReplyRecords 清空指定账号的默认回复发送记录。
|
||||
func (s *Server) clearDefaultReplyRecords(w http.ResponseWriter, r *http.Request) {
|
||||
// cid 是当前操作的账号标识。
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
// err 是默认回复记录清理错误。
|
||||
if _, err := s.Store.DB.ExecContext(r.Context(), `DELETE FROM default_reply_records WHERE cookie_id=?`, cid); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "清空失败")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true})
|
||||
writeJSON(w, http.StatusOK, operationResponse{Success: true})
|
||||
}
|
||||
Reference in new issue
Block a user