新增 filter_rules、admin.content_safety.read、列表/详情/试匹配 API 与 admin-h5「安全」页;禁审核写/UGC/真支付。 Co-authored-by: Cursor <cursoragent@cursor.com>
64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/service/admin"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
func (h *AdminHandler) registerContentSafety(authed *gin.RouterGroup) {
|
|
g := authed.Group("/content-safety")
|
|
g.GET("/filter-rules", middleware.RequireAdminPermission(h.Svc, admin.PermContentSafetyRead), h.ListFilterRules)
|
|
g.GET("/filter-rules/:id", middleware.RequireAdminPermission(h.Svc, admin.PermContentSafetyRead), h.GetFilterRule)
|
|
g.POST("/evaluate", middleware.RequireAdminPermission(h.Svc, admin.PermContentSafetyRead), h.EvaluateContent)
|
|
}
|
|
|
|
func (h *AdminHandler) ListFilterRules(c *gin.Context) {
|
|
items, err := h.Svc.ListFilterRules(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50022, "list filter rules failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) GetFilterRule(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
|
|
return
|
|
}
|
|
row, err := h.Svc.GetFilterRule(c.Request.Context(), id)
|
|
if errors.Is(err, admin.ErrFilterRuleNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40403, "filter rule not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50023, "get filter rule failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) EvaluateContent(c *gin.Context) {
|
|
var body struct {
|
|
Text string `json:"text"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40000, "invalid body")
|
|
return
|
|
}
|
|
matches, err := h.Svc.EvaluateContent(c.Request.Context(), body.Text)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50024, "evaluate failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"matches": matches})
|
|
}
|