spark-server/internal/transport/http/handler/friend/handler.go
zhr ae9a71034d feat: implement Spark IM V3 modular monolith
Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 19:52:11 +08:00

132 lines
2.6 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{
Email: c.Query("email"),
SparkID: c.Query("spark_id"),
QRCode: c.Query("qrcode"),
}
if raw := c.Query("uid"); raw != "" {
id, err := strconv.ParseInt(raw, 10, 64)
if err != nil || id <= 0 {
respond.JSONError(c, errs.ErrInvalidArgument)
return
}
in.UID = id
}
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) 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
}