244 lines
8.3 KiB
Go
244 lines
8.3 KiB
Go
|
|
package handler
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"errors"
|
|||
|
|
"net/http"
|
|||
|
|
|
|||
|
|
apperrors "carrotskin/internal/errors"
|
|||
|
|
"carrotskin/internal/container"
|
|||
|
|
"carrotskin/internal/service"
|
|||
|
|
|
|||
|
|
"github.com/gin-gonic/gin"
|
|||
|
|
"go.uber.org/zap"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// YggdrasilFriendsHandler MinecraftServices 好友系接口处理器(文档 1.1~1.3、1.6)
|
|||
|
|
type YggdrasilFriendsHandler struct {
|
|||
|
|
container *container.Container
|
|||
|
|
logger *zap.Logger
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NewYggdrasilFriendsHandler 创建 YggdrasilFriendsHandler 实例
|
|||
|
|
func NewYggdrasilFriendsHandler(c *container.Container) *YggdrasilFriendsHandler {
|
|||
|
|
return &YggdrasilFriendsHandler{
|
|||
|
|
container: c,
|
|||
|
|
logger: c.Logger,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// extractProfileUUID 解析 Bearer token 得到当前请求者的 Profile UUID
|
|||
|
|
// 失败时已写入 401 响应
|
|||
|
|
func (h *YggdrasilFriendsHandler) extractProfileUUID(c *gin.Context) (string, bool) {
|
|||
|
|
token, ok := extractBearerToken(c)
|
|||
|
|
if !ok {
|
|||
|
|
return "", false
|
|||
|
|
}
|
|||
|
|
uuid, err := h.container.TokenService.GetUUIDByAccessToken(c.Request.Context(), token)
|
|||
|
|
if err != nil || uuid == "" {
|
|||
|
|
h.logger.Warn("好友接口鉴权失败: 无效令牌", zap.Error(err))
|
|||
|
|
c.JSON(http.StatusUnauthorized, apperrors.NewMinecraftServicesErrorResponse(
|
|||
|
|
c.Request.URL.Path, "UNAUTHORIZED", "Invalid token", nil,
|
|||
|
|
))
|
|||
|
|
return "", false
|
|||
|
|
}
|
|||
|
|
return uuid, true
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// respondFriendsError 将 service 错误映射为 MinecraftServices 标准错误响应
|
|||
|
|
func (h *YggdrasilFriendsHandler) respondFriendsError(c *gin.Context, err error) {
|
|||
|
|
var appErr *apperrors.AppError
|
|||
|
|
if errors.As(err, &appErr) {
|
|||
|
|
// 未知 profile / 不能加自己 -> 400
|
|||
|
|
if appErr.Code == 400 {
|
|||
|
|
c.JSON(http.StatusBadRequest, apperrors.NewMinecraftServicesErrorResponse(
|
|||
|
|
c.Request.URL.Path, appErr.Message, appErr.Message, nil,
|
|||
|
|
))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if appErr.Code == 403 {
|
|||
|
|
c.JSON(http.StatusForbidden, apperrors.NewMinecraftServicesErrorResponse(
|
|||
|
|
c.Request.URL.Path, "FORBIDDEN", appErr.Message, nil,
|
|||
|
|
))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
h.logger.Error("好友接口内部错误", zap.Error(err))
|
|||
|
|
c.JSON(http.StatusInternalServerError, apperrors.NewMinecraftServicesErrorResponse(
|
|||
|
|
c.Request.URL.Path, "SERVICE_NOT_AVAILABLE", "Internal server error", nil,
|
|||
|
|
))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetFriends 获取好友列表
|
|||
|
|
// @Summary MinecraftServices 好友列表
|
|||
|
|
// @Description 拉取好友列表与收到/发出的好友请求(文档 1.3.1)
|
|||
|
|
// @Tags Yggdrasil
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param Authorization header string true "Bearer {accessToken}"
|
|||
|
|
// @Success 200 {object} service.FriendsListResponse
|
|||
|
|
// @Failure 401 {object} apperrors.MinecraftServicesErrorResponse "未授权"
|
|||
|
|
// @Failure 500 {object} apperrors.MinecraftServicesErrorResponse "服务器错误"
|
|||
|
|
// @Router /api/yggdrasil/minecraftservices/friends [get]
|
|||
|
|
func (h *YggdrasilFriendsHandler) GetFriends(c *gin.Context) {
|
|||
|
|
uuid, ok := h.extractProfileUUID(c)
|
|||
|
|
if !ok {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
resp, err := h.container.FriendsService.ListFriends(c.Request.Context(), uuid)
|
|||
|
|
if err != nil {
|
|||
|
|
h.respondFriendsError(c, err)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
c.JSON(http.StatusOK, resp)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateFriends 添加/删除好友、接受/拒绝/撤回请求
|
|||
|
|
// @Summary MinecraftServices 好友操作
|
|||
|
|
// @Description 通过 updateType=ADD/REMOVE 进行好友加删、接受/拒绝/撤回请求(文档 1.3.2)
|
|||
|
|
// @Tags Yggdrasil
|
|||
|
|
// @Accept json
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param Authorization header string true "Bearer {accessToken}"
|
|||
|
|
// @Param request body service.FriendActionRequest true "好友操作请求"
|
|||
|
|
// @Success 200 {object} service.FriendsListResponse
|
|||
|
|
// @Failure 400 {object} apperrors.MinecraftServicesErrorResponse "参数错误"
|
|||
|
|
// @Failure 401 {object} apperrors.MinecraftServicesErrorResponse "未授权"
|
|||
|
|
// @Router /api/yggdrasil/minecraftservices/friends [put]
|
|||
|
|
func (h *YggdrasilFriendsHandler) UpdateFriends(c *gin.Context) {
|
|||
|
|
uuid, ok := h.extractProfileUUID(c)
|
|||
|
|
if !ok {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
var req service.FriendActionRequest
|
|||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|||
|
|
c.JSON(http.StatusBadRequest, apperrors.NewMinecraftServicesErrorResponse(
|
|||
|
|
c.Request.URL.Path, "UNKNOWN_PROFILE", "Invalid request body", nil,
|
|||
|
|
))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
resp, err := h.container.FriendsService.PerformFriendAction(c.Request.Context(), uuid, req)
|
|||
|
|
if err != nil {
|
|||
|
|
h.respondFriendsError(c, err)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
c.JSON(http.StatusOK, resp)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetAttributes 获取玩家属性
|
|||
|
|
// @Summary MinecraftServices 玩家属性
|
|||
|
|
// @Description 拉取好友/脏词过滤/聊天偏好属性(文档 1.2.1)
|
|||
|
|
// @Tags Yggdrasil
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param Authorization header string true "Bearer {accessToken}"
|
|||
|
|
// @Success 200 {object} service.PlayerAttributesResponse
|
|||
|
|
// @Failure 401 {object} apperrors.MinecraftServicesErrorResponse "未授权"
|
|||
|
|
// @Router /api/yggdrasil/minecraftservices/player/attributes [get]
|
|||
|
|
func (h *YggdrasilFriendsHandler) GetAttributes(c *gin.Context) {
|
|||
|
|
uuid, ok := h.extractProfileUUID(c)
|
|||
|
|
if !ok {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
resp, err := h.container.FriendsService.GetAttributes(c.Request.Context(), uuid)
|
|||
|
|
if err != nil {
|
|||
|
|
h.respondFriendsError(c, err)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
c.JSON(http.StatusOK, resp)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateAttributes 更新玩家偏好属性
|
|||
|
|
// @Summary MinecraftServices 更新玩家属性
|
|||
|
|
// @Description 更新好友/脏词过滤偏好(文档 1.2.2)
|
|||
|
|
// @Tags Yggdrasil
|
|||
|
|
// @Accept json
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param Authorization header string true "Bearer {accessToken}"
|
|||
|
|
// @Param request body service.PlayerAttributesRequest true "偏好更新请求"
|
|||
|
|
// @Success 200 {object} service.PlayerAttributesResponse
|
|||
|
|
// @Failure 400 {object} apperrors.MinecraftServicesErrorResponse "参数错误"
|
|||
|
|
// @Failure 401 {object} apperrors.MinecraftServicesErrorResponse "未授权"
|
|||
|
|
// @Router /api/yggdrasil/minecraftservices/player/attributes [post]
|
|||
|
|
func (h *YggdrasilFriendsHandler) UpdateAttributes(c *gin.Context) {
|
|||
|
|
uuid, ok := h.extractProfileUUID(c)
|
|||
|
|
if !ok {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
var req service.PlayerAttributesRequest
|
|||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|||
|
|
c.JSON(http.StatusBadRequest, apperrors.NewMinecraftServicesErrorResponse(
|
|||
|
|
c.Request.URL.Path, "UNKNOWN_PROFILE", "Invalid request body", nil,
|
|||
|
|
))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if err := h.container.FriendsService.UpdateAttributes(c.Request.Context(), uuid, req); err != nil {
|
|||
|
|
h.respondFriendsError(c, err)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
// 返回最新属性
|
|||
|
|
resp, err := h.container.FriendsService.GetAttributes(c.Request.Context(), uuid)
|
|||
|
|
if err != nil {
|
|||
|
|
h.respondFriendsError(c, err)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
c.JSON(http.StatusOK, resp)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdatePresence 上报在线状态
|
|||
|
|
// @Summary MinecraftServices 在线状态
|
|||
|
|
// @Description 上报当前在线状态并返回好友在线状态列表(文档 1.1)
|
|||
|
|
// @Tags Yggdrasil
|
|||
|
|
// @Accept json
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param Authorization header string true "Bearer {accessToken}"
|
|||
|
|
// @Param request body service.PresenceRequest true "状态上报请求"
|
|||
|
|
// @Success 200 {object} service.PresenceResponse
|
|||
|
|
// @Failure 400 {object} apperrors.MinecraftServicesErrorResponse "参数错误"
|
|||
|
|
// @Failure 401 {object} apperrors.MinecraftServicesErrorResponse "未授权"
|
|||
|
|
// @Router /api/yggdrasil/minecraftservices/presence [post]
|
|||
|
|
func (h *YggdrasilFriendsHandler) UpdatePresence(c *gin.Context) {
|
|||
|
|
uuid, ok := h.extractProfileUUID(c)
|
|||
|
|
if !ok {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
var req service.PresenceRequest
|
|||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|||
|
|
c.JSON(http.StatusBadRequest, apperrors.NewMinecraftServicesErrorResponse(
|
|||
|
|
c.Request.URL.Path, "INVALID_STATUS", "Invalid request body", nil,
|
|||
|
|
))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if req.Status == "" {
|
|||
|
|
c.JSON(http.StatusBadRequest, apperrors.NewMinecraftServicesErrorResponse(
|
|||
|
|
c.Request.URL.Path, "INVALID_STATUS", "Bad status value", nil,
|
|||
|
|
))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
resp, err := h.container.FriendsService.UpdatePresence(c.Request.Context(), uuid, req.Status)
|
|||
|
|
if err != nil {
|
|||
|
|
h.respondFriendsError(c, err)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
c.JSON(http.StatusOK, resp)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetBlocklist 获取玩家屏蔽列表
|
|||
|
|
// @Summary MinecraftServices 屏蔽列表
|
|||
|
|
// @Description 拉取被屏蔽玩家档案 UUID 列表(文档 1.6)
|
|||
|
|
// @Tags Yggdrasil
|
|||
|
|
// @Produce json
|
|||
|
|
// @Param Authorization header string true "Bearer {accessToken}"
|
|||
|
|
// @Success 200 {object} service.BlockListResponse
|
|||
|
|
// @Failure 401 {object} apperrors.MinecraftServicesErrorResponse "未授权"
|
|||
|
|
// @Router /api/yggdrasil/minecraftservices/privacy/blocklist [get]
|
|||
|
|
func (h *YggdrasilFriendsHandler) GetBlocklist(c *gin.Context) {
|
|||
|
|
uuid, ok := h.extractProfileUUID(c)
|
|||
|
|
if !ok {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
resp, err := h.container.FriendsService.GetBlocklist(c.Request.Context(), uuid)
|
|||
|
|
if err != nil {
|
|||
|
|
h.respondFriendsError(c, err)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
c.JSON(http.StatusOK, resp)
|
|||
|
|
}
|