feat(yggdrasil): 实现好友系统与 profiles 查询接口
按 MinecraftServices 文档实现好友系四组接口与 profiles 服务:
好友系统(/api/yggdrasil/minecraftservices/*)
- Friends(1.3):列表查询、ADD/REMOVE 操作(发请求/接受/拒绝/撤回/删除)
- 状态枚举+单向记录模型,接受请求时合并两方向避免好友列表重复
- Player Attributes(1.2):friendsPreferences / 脏词过滤 / 聊天偏好读写
- Upsert 用 map 显式写字段规避 gorm 对带 default 零值布尔字段的忽略
- Presence(1.1):Redis key+TTL 上报与好友在线状态批量查询
- Blocklist(1.6):屏蔽列表查询(含 120s Redis 缓存),Block/Unblock 内部方法
Profiles 服务
- getManyByName(2.1):POST /api/profiles/minecraft,返回 [{id,name}]
- toLowerCase 规范化、去重、空名过滤、maxBatch=10 超限拒绝
- getByName(2.2):GET /api/users/profiles/minecraft/:name,返回 {id,name},未找到返回 404
路由与基础设施
- 路由按官方 host 前缀一一转发
- api.mojang.com/* -> /api/yggdrasil/api/*
- api.minecraftservices.com/* -> /api/yggdrasil/minecraftservices/*
- 新增 Friend/PlayerAttributes 模型与 AutoMigrate 注册
- 新增 FriendsService 与 Container 装配
- 抽 extractBearerToken helper,统一 MinecraftServices 错误响应
修复
- texture_service: ToggleFavorite 在 db 为 nil 时降级非事务执行
修复 TestTextureServiceImpl_ToggleFavorite 空指针 panic
- texture_service_test: UploadTexture 用例改用真实 SHA-256 命中 mock
修复 4 个子用例因文件大小/Hash 不匹配的失败
- profile_repository: GetByNames/FindByName 改 LOWER() 大小写不敏感
与客户端 toLowerCase 规范化对齐
测试
- 好友模型、ADD/REMOVE/接受、属性、屏蔽、Bearer 解析单测全绿
- profiles 查询规范化、去重、超限、大小写不敏感单测全绿
- go build ./... && go test ./... 全部通过
This commit is contained in:
@@ -616,12 +616,12 @@ func (h *YggdrasilHandler) HasJoinedServer(c *gin.Context) {
|
||||
|
||||
// GetProfilesByName 批量获取配置文件
|
||||
// @Summary Yggdrasil批量获取档案
|
||||
// @Description Yggdrasil协议: 根据名称批量获取用户档案
|
||||
// @Description Yggdrasil协议: 根据名称批量获取用户档案(文档 2.1,每页最多 10 个,返回 [{id,name}])
|
||||
// @Tags Yggdrasil
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body []string true "用户名列表"
|
||||
// @Success 200 {array} model.Profile "档案列表"
|
||||
// @Success 200 {array} model.NameAndId "档案标识列表"
|
||||
// @Failure 400 {object} map[string]string "参数错误"
|
||||
// @Router /api/yggdrasil/api/profiles/minecraft [post]
|
||||
func (h *YggdrasilHandler) GetProfilesByName(c *gin.Context) {
|
||||
@@ -635,13 +635,51 @@ func (h *YggdrasilHandler) GetProfilesByName(c *gin.Context) {
|
||||
|
||||
h.logger.Info("接收到批量获取配置文件请求", zap.Int("count", len(names)))
|
||||
|
||||
profiles, err := h.container.ProfileService.GetByNames(c.Request.Context(), names)
|
||||
// 文档 2.1:每页最多 10 个用户名
|
||||
const maxBatch = 10
|
||||
results, err := h.container.ProfileService.ProfileSearchByName(c.Request.Context(), names, maxBatch)
|
||||
if err != nil {
|
||||
h.logger.Error("获取配置文件失败", zap.Error(err))
|
||||
h.logger.Warn("批量获取配置文件失败", zap.Error(err), zap.Int("count", len(names)))
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
h.logger.Info("成功获取配置文件", zap.Int("requested", len(names)), zap.Int("returned", len(profiles)))
|
||||
c.JSON(http.StatusOK, profiles)
|
||||
h.logger.Info("成功获取配置文件", zap.Int("requested", len(names)), zap.Int("returned", len(results)))
|
||||
c.JSON(http.StatusOK, results)
|
||||
}
|
||||
|
||||
// GetProfileByName 单个用户名查档案
|
||||
// @Summary Yggdrasil按用户名查档案
|
||||
// @Description Yggdrasil协议: 根据单个用户名查询用户档案(文档 2.2,返回 {id,name})
|
||||
// @Tags Yggdrasil
|
||||
// @Produce json
|
||||
// @Param name path string true "用户名"
|
||||
// @Success 200 {object} model.NameAndId "档案标识"
|
||||
// @Failure 404 {object} map[string]string "档案不存在"
|
||||
// @Router /api/yggdrasil/api/users/profiles/minecraft/{name} [get]
|
||||
func (h *YggdrasilHandler) GetProfileByName(c *gin.Context) {
|
||||
name := c.Param("name")
|
||||
if name == "" {
|
||||
h.logger.Warn("缺少用户名参数")
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.container.ProfileService.ProfileSearchByNameSingle(c.Request.Context(), name)
|
||||
if err != nil {
|
||||
// 按文档契约:任意错误视为未找到
|
||||
h.logger.Warn("按用户名查档失败", zap.String("name", name), zap.Error(err))
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"})
|
||||
return
|
||||
}
|
||||
if result == nil {
|
||||
h.logger.Info("用户名未找到", zap.String("name", name))
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
h.logger.Info("成功获取档案", zap.String("name", name), zap.String("uuid", result.ID))
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// GetMetaData 获取Yggdrasil元数据
|
||||
|
||||
Reference in New Issue
Block a user