为 Spark Messenger 主路径补齐 POST /v1/friends/requests/reject,并落地法律协议/反馈/订阅/密码登录及管理页,同步 OpenAPI 文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
138 lines
2.7 KiB
Go
138 lines
2.7 KiB
Go
package friendhandler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
friendlogic "spark-server/internal/module/friend/logic"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/reqctx"
|
|
"spark-server/internal/transport/http/respond"
|
|
)
|
|
|
|
// Handler is the friends HTTP controller.
|
|
type Handler struct {
|
|
friends *friendlogic.Service
|
|
}
|
|
|
|
func New(friends *friendlogic.Service) *Handler {
|
|
return &Handler{friends: friends}
|
|
}
|
|
|
|
func (h *Handler) Lookup(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
in := friendlogic.LookupInput{Q: c.Query("q")}
|
|
view, err := h.friends.Lookup(c.Request.Context(), uid, in)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, view)
|
|
}
|
|
|
|
func (h *Handler) Apply(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var in friendlogic.ApplyInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
out, err := h.friends.Apply(c.Request.Context(), uid, &in)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) ListRequests(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.friends.ListRequests(c.Request.Context(), uid, queryLimit(c))
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
|
|
func (h *Handler) Reject(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var in friendlogic.RejectInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
if err := h.friends.Reject(c.Request.Context(), uid, in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, gin.H{})
|
|
}
|
|
|
|
func (h *Handler) Accept(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var in friendlogic.AcceptInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
out, err := h.friends.Accept(c.Request.Context(), uid, in)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) List(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.friends.List(c.Request.Context(), uid, queryLimit(c))
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func currentUID(c *gin.Context) (int64, bool) {
|
|
rc := reqctx.From(c.Request.Context())
|
|
if rc == nil || rc.UserID <= 0 {
|
|
respond.JSONError(c, errs.ErrUnauthorized)
|
|
return 0, false
|
|
}
|
|
return rc.UserID, true
|
|
}
|
|
|
|
func queryLimit(c *gin.Context) int {
|
|
n, _ := strconv.Atoi(c.Query("limit"))
|
|
return n
|
|
}
|
|
|
|
func pathUID(c *gin.Context) (int64, error) {
|
|
id, err := strconv.ParseInt(c.Param("uid"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
return 0, errs.ErrInvalidArgument
|
|
}
|
|
return id, nil
|
|
}
|