合并 main:同步聊天、风控诊断与迁移基线
This commit is contained in:
commit
a6c0d36ee9
89 files changed
+2974
-365
No files matched your search
@@ -366,10 +366,8 @@ func newApplicationServices(server *Server) *applicationServices {
|
||||
panic(itemCatalogMutationErr)
|
||||
}
|
||||
// itemPublishPort 是单商品与批量发布共享的平台凭证适配器。
|
||||
itemPublishPort := server.itemDependencies.NewItemPublishPort(server.mtopClient, server.Logger, server.updateRunningCookie, func(ctx context.Context, cookieID string, err error) {
|
||||
if sessionRecovery != nil {
|
||||
sessionRecovery(ctx, cookieID, err)
|
||||
}
|
||||
itemPublishPort := server.itemDependencies.NewItemPublishPort(server.mtopClient, server.Logger, server.updateRunningCookie, func(ctx context.Context, cookieID string, err error) bool {
|
||||
return sessionRecovery != nil && sessionRecovery(ctx, cookieID, err)
|
||||
})
|
||||
// itemCategoryRecommendation 复用商品发布端口承载类目推荐和响应会话写回。
|
||||
itemCategoryRecommendation, itemCategoryRecommendationErr := itemapp.NewCategoryRecommendationService(itemPublishPort)
|
||||
|
||||
@@ -64,6 +64,9 @@ func (s *Server) listCards(w http.ResponseWriter, r *http.Request) {
|
||||
// cards、err 保存应用服务返回的用户卡券列表及查询错误。
|
||||
cards, err := s.cardsApplication().List(r.Context(), session.UserID)
|
||||
if err != nil {
|
||||
if s.Logger != nil {
|
||||
s.Logger.Error("查询卡密失败", "user_id", session.UserID, "err", err)
|
||||
}
|
||||
writeErr(w, http.StatusInternalServerError, "查询失败")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -147,3 +147,43 @@ func TestChatAndTaskEndpointsEnforceOwnershipAndValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestFindChatPlatformMessageID 验证历史推送关联 ID 只在同一会话内映射为 PNM 已读 ID。
|
||||
func TestFindChatPlatformMessageID(t *testing.T) {
|
||||
// raw 模拟持久化的解密 WS 消息,包含推送关联 ID 与平台 PNM ID。
|
||||
raw := map[string]any{
|
||||
"1": map[string]any{
|
||||
"2": "64725235816@goofish",
|
||||
"3": "4263141580162.PNM",
|
||||
"10": map[string]any{
|
||||
"extJson": `{"messageId":"f87f8f6dabca4eff940863ef72a393f7"}`,
|
||||
},
|
||||
},
|
||||
}
|
||||
// got 保存同会话匹配后可向平台上报的 PNM ID。
|
||||
if got := findChatPlatformMessageID(raw, "64725235816", "f87f8f6dabca4eff940863ef72a393f7"); got != "4263141580162.PNM" {
|
||||
t.Fatalf("platform message id=%q", got)
|
||||
}
|
||||
// got 保存跨会话查询结果,必须为空以禁止错误标记其他会话已读。
|
||||
if got := findChatPlatformMessageID(raw, "other-chat", "f87f8f6dabca4eff940863ef72a393f7"); got != "" {
|
||||
t.Fatalf("跨会话错误匹配: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveChatReadMessageIDsMigratesLegacyID 验证历史关联 ID 会转换为平台接受的 PNM ID。
|
||||
func TestResolveChatReadMessageIDsMigratesLegacyID(t *testing.T) {
|
||||
// srv 提供待测解析路径;store 写入历史 WS 消息;cleanup 释放测试服务器和数据库。
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// raw 是保存到数据库的解密信封 JSON,含旧关联 ID 及其对应的 PNM ID。
|
||||
raw := `{"1":{"2":"64725235816@goofish","3":"4263141580162.PNM","10":{"extJson":"{\"messageId\":\"f87f8f6dabca4eff940863ef72a393f7\"}"}}}`
|
||||
// err 保存写入历史 WS 消息失败的原因,写入失败会使后续解析夹具无效。
|
||||
if err := store.WSMessages.Add(context.Background(), db.WSMessage{CookieID: "acc1", Direction: "in", ParsedJSON: raw, ParseStatus: "decrypted"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// got 是兼容解析后的上报参数,旧关联 ID 必须被替换为 PNM ID。
|
||||
got := srv.resolveChatReadMessageIDs(context.Background(), "acc1", "64725235816", []map[string]any{{"messageId": "f87f8f6dabca4eff940863ef72a393f7"}})
|
||||
if len(got) != 1 || got[0]["messageId"] != "4263141580162.PNM" {
|
||||
t.Fatalf("resolved=%+v", got)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -284,8 +285,9 @@ func (s *Server) sendChatMessage(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) markChatRead(w http.ResponseWriter, r *http.Request) {
|
||||
// input 保存input,供当前处理流程使用
|
||||
var input struct {
|
||||
AccountID string `json:"account_id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
AccountID string `json:"account_id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
MessageIDs []map[string]any `json:"message_ids"`
|
||||
}
|
||||
if decodeJSON(r, &input) != nil || input.ChatID == "" {
|
||||
writeErr(w, http.StatusBadRequest, "请求格式错误")
|
||||
@@ -295,17 +297,90 @@ func (s *Server) markChatRead(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusForbidden, "无权操作该账号")
|
||||
return
|
||||
}
|
||||
// sess 保存当前认证用户,用于应用层归属隔离。
|
||||
// sess 保存当前认证用户,用于本地未读状态归属隔离。
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
slog.Info("收到聊天已读请求", "account", input.AccountID, "chat_id", input.ChatID, "message_count", len(input.MessageIDs))
|
||||
if len(input.MessageIDs) == 0 {
|
||||
// page 保存应用层返回的本地消息页,只含非敏感字段。
|
||||
page, listErr := s.chatApplication().ListStoredMessages(r.Context(), sess.UserID, input.AccountID, input.ChatID, 0, 200)
|
||||
if listErr == nil {
|
||||
// message 是当前用于补全平台已读消息标识的入站消息。
|
||||
for _, message := range page.Messages {
|
||||
if message.Direction == "incoming" && message.MessageType != "system" {
|
||||
input.MessageIDs = append(input.MessageIDs, map[string]any{"messageId": message.MessageKey})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 旧版本把实时 WS 通知里的 bizTag/extJson messageId 当成了平台消息
|
||||
// ID,数据库里会留下 32 位关联 ID。闲鱼的 read 接口实际要求 1.3 的
|
||||
// PNM ID;这里从已保存的解密 WS 诊断帧把旧 ID 转回 PNM,避免升级后
|
||||
// 仍有历史实时消息无法被标记已读。
|
||||
input.MessageIDs = s.resolveChatReadMessageIDs(r.Context(), input.AccountID, input.ChatID, input.MessageIDs)
|
||||
if // err 保存应用层已读状态更新错误。
|
||||
err := s.chatApplication().MarkRead(r.Context(), sess.UserID, input.AccountID, input.ChatID); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "更新已读状态失败")
|
||||
return
|
||||
}
|
||||
if s.Manager != nil {
|
||||
// sender、ok 保存当前账号运行时及其在线状态。
|
||||
if sender, ok := s.Manager.GetInstance(input.AccountID); ok {
|
||||
// reader、ok 保存运行时是否支持平台已读上报。
|
||||
if reader, ok := sender.(interface {
|
||||
MarkChatRead(context.Context, string, []map[string]any) error
|
||||
}); ok {
|
||||
// err 保存平台已读上报失败,失败不影响本地状态已落库。
|
||||
if err := reader.MarkChatRead(r.Context(), input.ChatID, input.MessageIDs); err != nil {
|
||||
slog.Warn("上报闲鱼已读状态失败", "account", input.AccountID, "chat_id", input.ChatID, "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, operationResponse{Success: true})
|
||||
}
|
||||
|
||||
// chatWebSocket 负责聊天WebSocket相关处理。
|
||||
// resolveChatReadMessageIDs 将旧版关联标识交给应用服务解析,并移除无效或重复的已读项。
|
||||
func (s *Server) resolveChatReadMessageIDs(ctx context.Context, accountID, chatID string, messageIDs []map[string]any) []map[string]any {
|
||||
// resolved 保存可安全提交给平台的去重消息标识列表。
|
||||
resolved := make([]map[string]any, 0, len(messageIDs))
|
||||
// seen 保存已加入结果的平台 PNM 标识,避免重复上报。
|
||||
seen := make(map[string]struct{}, len(messageIDs))
|
||||
// item 是当前待转换的已读消息参数。
|
||||
for _, item := range messageIDs {
|
||||
// rawID 保存请求携带的原始消息标识。
|
||||
rawID, ok := item["messageId"].(string)
|
||||
if !ok || strings.TrimSpace(rawID) == "" {
|
||||
continue
|
||||
}
|
||||
// id 保存应用层解析后的平台消息标识。
|
||||
id := s.chatApplication().ResolveReadMessageID(ctx, accountID, chatID, rawID)
|
||||
if !strings.HasSuffix(id, ".PNM") {
|
||||
slog.Warn("未找到旧聊天消息对应的 PNM,跳过已读上报", "account", accountID, "chat_id", chatID, "message_id", rawID)
|
||||
continue
|
||||
}
|
||||
// exists 表示该平台消息标识是否已经加入本次上报请求。
|
||||
if _, exists := seen[id]; exists {
|
||||
continue
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
// copyItem 保存保留其他平台参数、只替换消息标识的请求副本。
|
||||
copyItem := make(map[string]any, len(item)+1)
|
||||
// key、value 保存当前平台参数名及其原始值。
|
||||
for key, value := range item {
|
||||
copyItem[key] = value
|
||||
}
|
||||
copyItem["messageId"] = id
|
||||
resolved = append(resolved, copyItem)
|
||||
}
|
||||
return resolved
|
||||
}
|
||||
|
||||
// findChatPlatformMessageID 保留既有包内测试入口,实际解析由聊天应用服务拥有。
|
||||
func findChatPlatformMessageID(value any, chatID, legacyID string) string {
|
||||
return chatapp.FindPlatformMessageID(value, chatID, legacyID)
|
||||
}
|
||||
|
||||
// chatWebSocket 将应用层聊天事件转发到当前认证用户的 WebSocket 连接。
|
||||
func (s *Server) chatWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
// sess 保存sess,供当前处理流程使用
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
|
||||
@@ -111,7 +111,6 @@ func (s *Server) publishItem(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
selectedLocation = &location
|
||||
}
|
||||
// outcome、callErr 保存outcome、callErr,供当前处理流程使用
|
||||
// applicationLocation 是 HTTP DTO 转换后的应用发货地模型。
|
||||
var applicationLocation *itemapp.Location
|
||||
if selectedLocation != nil {
|
||||
@@ -154,8 +153,6 @@ func (s *Server) publishItem(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusConflict, callErr.Error())
|
||||
return
|
||||
}
|
||||
writeErr(w, http.StatusBadGateway, callErr.Error())
|
||||
return
|
||||
}
|
||||
if res == nil || strings.TrimSpace(res.ItemID) == "" {
|
||||
writeErrCode(w, http.StatusBadGateway, "publish_result_missing_item_id", "平台返回发布成功,但缺少商品 ID,无法确认发布结果", "")
|
||||
|
||||
@@ -84,7 +84,7 @@ type itemDependencyFactory interface {
|
||||
// NewItemBatchPublishPort 创建批量远端发布端口。
|
||||
NewItemBatchPublishPort(func() adapter.MTOPClient, *slog.Logger, func(context.Context, string, string), func(context.Context, string, error), adapter.ReadPublishImageFile, adapter.DownloadPublishImageURL) *adapter.ItemBatchPublishPort
|
||||
// NewItemPublishPort 创建单商品发布端口。
|
||||
NewItemPublishPort(func() adapter.MTOPClient, *slog.Logger, func(context.Context, string, string), func(context.Context, string, error)) *adapter.ItemPublishPort
|
||||
NewItemPublishPort(func() adapter.MTOPClient, *slog.Logger, func(context.Context, string, string), func(context.Context, string, error) bool) *adapter.ItemPublishPort
|
||||
// NewItemPublishRepository 创建单商品发布结果仓储。
|
||||
NewItemPublishRepository() *adapter.ItemPublishRepository
|
||||
// NewItemCatalogRepository 创建商品目录仓储。
|
||||
|
||||
Reference in new issue
Block a user