From 3638d702d073f491c07afcac080e361ebbbe4510 Mon Sep 17 00:00:00 2001 From: Christ Date: Sat, 15 Aug 2026 02:37:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B6=E6=AE=B5=E5=85=AB=EF=BC=9A=E5=AE=8C?= =?UTF-8?q?=E6=88=90Chat=E7=AA=84repository=E6=8E=A5=E5=8F=A3=E4=B8=8E?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E4=BE=9D=E8=B5=96=E6=94=B6=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .commentlint/go-baseline.json | 306 ++++++++++--------- docs/architecture/refactoring-master-plan.md | 1 + internal/chat/repository.go | 74 +++++ internal/chat/repository_test.go | 55 ++++ internal/chat/service.go | 40 +-- 5 files changed, 315 insertions(+), 161 deletions(-) create mode 100644 internal/chat/repository.go create mode 100644 internal/chat/repository_test.go diff --git a/.commentlint/go-baseline.json b/.commentlint/go-baseline.json index ca41a0c..556305f 100644 --- a/.commentlint/go-baseline.json +++ b/.commentlint/go-baseline.json @@ -22589,6 +22589,24 @@ "kind": "local-variable", "name": "err" }, + { + "file": "internal/chat/repository_test.go", + "line": 49, + "kind": "local-variable", + "name": "service" + }, + { + "file": "internal/chat/repository_test.go", + "line": 50, + "kind": "local-variable", + "name": "cancel" + }, + { + "file": "internal/chat/repository_test.go", + "line": 50, + "kind": "local-variable", + "name": "err" + }, { "file": "internal/chat/service.go", "line": 19, @@ -22897,457 +22915,439 @@ }, { "file": "internal/chat/service.go", - "line": 189, + "line": 190, "kind": "function", "name": "New" }, { "file": "internal/chat/service.go", - "line": 193, + "line": 199, "kind": "function", "name": "Subscribe" }, { "file": "internal/chat/service.go", - "line": 198, + "line": 204, "kind": "local-variable", "name": "allowed" }, { "file": "internal/chat/service.go", - "line": 204, + "line": 210, "kind": "local-variable", "name": "id" }, { "file": "internal/chat/service.go", - "line": 205, + "line": 211, "kind": "local-variable", "name": "ch" }, { "file": "internal/chat/service.go", - "line": 208, + "line": 214, "kind": "var", "name": "once" }, { "file": "internal/chat/service.go", - "line": 209, + "line": 215, "kind": "local-variable", "name": "cancel" }, { "file": "internal/chat/service.go", - "line": 212, + "line": 218, "kind": "local-variable", "name": "ok" }, { "file": "internal/chat/service.go", - "line": 212, + "line": 218, "kind": "local-variable", "name": "sub" }, { "file": "internal/chat/service.go", - "line": 222, + "line": 228, "kind": "function", "name": "Publish" }, { "file": "internal/chat/service.go", - "line": 225, + "line": 231, "kind": "range-variable", "name": "sub" }, { "file": "internal/chat/service.go", - "line": 226, + "line": 232, "kind": "local-variable", "name": "ok" }, { "file": "internal/chat/service.go", - "line": 238, + "line": 244, "kind": "function", "name": "RecordIncoming" }, { "file": "internal/chat/service.go", - "line": 242, + "line": 248, "kind": "local-variable", "name": "sentAt" }, { "file": "internal/chat/service.go", - "line": 246, + "line": 252, "kind": "local-variable", "name": "key" }, { "file": "internal/chat/service.go", - "line": 248, + "line": 254, "kind": "local-variable", "name": "raw" }, { "file": "internal/chat/service.go", - "line": 249, + "line": 255, "kind": "local-variable", "name": "digest" }, { "file": "internal/chat/service.go", - "line": 252, + "line": 258, "kind": "local-variable", "name": "session" }, { "file": "internal/chat/service.go", - "line": 255, + "line": 261, "kind": "local-variable", "name": "content" }, { "file": "internal/chat/service.go", - "line": 255, + "line": 261, "kind": "local-variable", "name": "messageType" }, { "file": "internal/chat/service.go", - "line": 264, + "line": 270, "kind": "local-variable", "name": "message" }, { "file": "internal/chat/service.go", - "line": 266, + "line": 272, "kind": "local-variable", "name": "err" }, { "file": "internal/chat/service.go", - "line": 266, + "line": 272, "kind": "local-variable", "name": "inserted" }, { "file": "internal/chat/service.go", - "line": 266, + "line": 272, "kind": "local-variable", "name": "stored" }, { "file": "internal/chat/service.go", - "line": 274, + "line": 280, "kind": "function", "name": "RecordHistoryPage" }, { "file": "internal/chat/service.go", - "line": 275, + "line": 281, "kind": "local-variable", "name": "page" }, { "file": "internal/chat/service.go", - "line": 276, + "line": 282, "kind": "local-variable", "name": "models" }, { "file": "internal/chat/service.go", - "line": 277, + "line": 283, "kind": "local-variable", "name": "i" }, { "file": "internal/chat/service.go", - "line": 278, + "line": 284, "kind": "local-variable", "name": "model" }, { "file": "internal/chat/service.go", - "line": 279, + "line": 285, "kind": "local-variable", "name": "message" }, { "file": "internal/chat/service.go", - "line": 279, + "line": 285, "kind": "local-variable", "name": "ok" }, { "file": "internal/chat/service.go", - "line": 289, + "line": 295, "kind": "local-variable", "name": "err" }, { "file": "internal/chat/service.go", - "line": 289, + "line": 295, "kind": "local-variable", "name": "stored" }, { "file": "internal/chat/service.go", - "line": 294, + "line": 300, "kind": "local-variable", "name": "err" }, { "file": "internal/chat/service.go", - "line": 304, + "line": 310, "kind": "function", "name": "parseHistoryMessage" }, { "file": "internal/chat/service.go", - "line": 305, + "line": 311, "kind": "local-variable", "name": "message" }, { "file": "internal/chat/service.go", - "line": 309, + "line": 315, "kind": "local-variable", "name": "extension" }, { "file": "internal/chat/service.go", - "line": 310, + "line": 316, "kind": "local-variable", "name": "senderID" }, { "file": "internal/chat/service.go", - "line": 311, + "line": 317, "kind": "local-variable", "name": "senderName" }, { "file": "internal/chat/service.go", - "line": 315, + "line": 321, "kind": "local-variable", "name": "key" }, { "file": "internal/chat/service.go", - "line": 319, + "line": 325, "kind": "local-variable", "name": "contentMap" }, { "file": "internal/chat/service.go", - "line": 320, + "line": 326, "kind": "local-variable", "name": "custom" }, { "file": "internal/chat/service.go", - "line": 321, + "line": 327, "kind": "local-variable", "name": "rawContent" }, { "file": "internal/chat/service.go", - "line": 322, + "line": 328, "kind": "local-variable", "name": "encoded" }, { "file": "internal/chat/service.go", - "line": 323, + "line": 329, "kind": "local-variable", "name": "decoded" }, { "file": "internal/chat/service.go", - "line": 323, + "line": 329, "kind": "local-variable", "name": "err" }, { "file": "internal/chat/service.go", - "line": 327, + "line": 333, "kind": "local-variable", "name": "fallback" }, { "file": "internal/chat/service.go", - "line": 331, + "line": 337, "kind": "local-variable", "name": "ok" }, { "file": "internal/chat/service.go", - "line": 331, + "line": 337, "kind": "local-variable", "name": "textBlock" }, { "file": "internal/chat/service.go", - "line": 332, + "line": 338, "kind": "local-variable", "name": "text" }, { "file": "internal/chat/service.go", - "line": 336, + "line": 342, "kind": "local-variable", "name": "content" }, { "file": "internal/chat/service.go", - "line": 336, + "line": 342, "kind": "local-variable", "name": "messageType" }, { "file": "internal/chat/service.go", - "line": 346, + "line": 352, "kind": "local-variable", "name": "direction" }, { "file": "internal/chat/service.go", - "line": 346, + "line": 352, "kind": "local-variable", "name": "status" }, { "file": "internal/chat/service.go", - "line": 355, + "line": 361, "kind": "function", "name": "mapValue" }, { "file": "internal/chat/service.go", - "line": 356, + "line": 362, "kind": "local-variable", "name": "ok" }, { "file": "internal/chat/service.go", - "line": 356, + "line": 362, "kind": "local-variable", "name": "result" }, { "file": "internal/chat/service.go", - "line": 359, + "line": 365, "kind": "local-variable", "name": "ok" }, { "file": "internal/chat/service.go", - "line": 359, + "line": 365, "kind": "local-variable", "name": "text" }, { "file": "internal/chat/service.go", - "line": 360, + "line": 366, "kind": "var", "name": "result" }, { "file": "internal/chat/service.go", - "line": 368, + "line": 374, "kind": "function", "name": "int64Value" }, { "file": "internal/chat/service.go", - "line": 369, + "line": 375, "kind": "local-variable", "name": "typed" }, { "file": "internal/chat/service.go", - "line": 377, + "line": 383, "kind": "local-variable", "name": "result" }, { "file": "internal/chat/service.go", - "line": 380, + "line": 386, "kind": "var", "name": "result" }, { "file": "internal/chat/service.go", - "line": 387, + "line": 393, "kind": "function", "name": "boolValue" }, { "file": "internal/chat/service.go", - "line": 388, + "line": 394, "kind": "local-variable", "name": "typed" }, { "file": "internal/chat/service.go", - "line": 401, + "line": 407, "kind": "function", "name": "extractMessageContent" }, { "file": "internal/chat/service.go", - "line": 402, + "line": 408, "kind": "var", "name": "inspect" }, { "file": "internal/chat/service.go", - "line": 404, + "line": 410, "kind": "local-variable", "name": "typed" }, { "file": "internal/chat/service.go", - "line": 406, + "line": 412, "kind": "var", "name": "nested" }, { "file": "internal/chat/service.go", - "line": 411, + "line": 417, "kind": "local-variable", "name": "contentType" }, { "file": "internal/chat/service.go", - "line": 413, + "line": 419, "kind": "local-variable", "name": "mediaURL" }, { "file": "internal/chat/service.go", - "line": 418, - "kind": "local-variable", - "name": "mediaURL" - }, - { - "file": "internal/chat/service.go", - "line": 422, - "kind": "range-variable", - "name": "child" - }, - { - "file": "internal/chat/service.go", - "line": 423, - "kind": "local-variable", - "name": "kind" - }, - { - "file": "internal/chat/service.go", - "line": 423, + "line": 424, "kind": "local-variable", "name": "mediaURL" }, @@ -23371,232 +23371,238 @@ }, { "file": "internal/chat/service.go", - "line": 436, + "line": 434, + "kind": "range-variable", + "name": "child" + }, + { + "file": "internal/chat/service.go", + "line": 435, "kind": "local-variable", "name": "kind" }, { "file": "internal/chat/service.go", - "line": 436, + "line": 435, "kind": "local-variable", "name": "mediaURL" }, { "file": "internal/chat/service.go", - "line": 446, + "line": 442, + "kind": "local-variable", + "name": "kind" + }, + { + "file": "internal/chat/service.go", + "line": 442, + "kind": "local-variable", + "name": "mediaURL" + }, + { + "file": "internal/chat/service.go", + "line": 452, "kind": "function", "name": "isOfficialSystemMessage" }, { "file": "internal/chat/service.go", - "line": 450, + "line": 456, "kind": "local-variable", "name": "contentType" }, { "file": "internal/chat/service.go", - "line": 459, + "line": 465, "kind": "function", "name": "findOfficialContentType" }, { "file": "internal/chat/service.go", - "line": 460, + "line": 466, "kind": "var", "name": "found" }, { "file": "internal/chat/service.go", - "line": 461, + "line": 467, "kind": "var", "name": "walk" }, { "file": "internal/chat/service.go", - "line": 466, + "line": 472, "kind": "local-variable", "name": "typed" }, { "file": "internal/chat/service.go", - "line": 468, + "line": 474, "kind": "var", "name": "nested" }, { "file": "internal/chat/service.go", - "line": 473, + "line": 479, "kind": "local-variable", "name": "candidate" }, { "file": "internal/chat/service.go", - "line": 477, + "line": 483, "kind": "range-variable", "name": "child" }, { "file": "internal/chat/service.go", - "line": 481, + "line": 487, "kind": "range-variable", "name": "child" }, { "file": "internal/chat/service.go", - "line": 490, + "line": 496, "kind": "function", "name": "CreateOutgoing" }, { "file": "internal/chat/service.go", - "line": 494, + "line": 500, "kind": "function", "name": "CreateOutgoingMedia" }, { "file": "internal/chat/service.go", - "line": 495, + "line": 501, "kind": "local-variable", "name": "key" }, { "file": "internal/chat/service.go", - "line": 496, + "line": 502, "kind": "local-variable", "name": "message" }, { "file": "internal/chat/service.go", - "line": 499, + "line": 505, "kind": "local-variable", "name": "err" }, { "file": "internal/chat/service.go", - "line": 499, + "line": 505, "kind": "local-variable", "name": "stored" }, { "file": "internal/chat/service.go", - "line": 508, + "line": 514, "kind": "function", "name": "RecordOutgoingSent" }, { "file": "internal/chat/service.go", - "line": 512, + "line": 518, "kind": "local-variable", "name": "message" }, { "file": "internal/chat/service.go", - "line": 515, + "line": 521, "kind": "local-variable", "name": "err" }, { "file": "internal/chat/service.go", - "line": 515, + "line": 521, "kind": "local-variable", "name": "stored" }, { "file": "internal/chat/service.go", - "line": 522, + "line": 528, "kind": "function", "name": "SetOutgoingStatus" }, { "file": "internal/chat/service.go", - "line": 523, + "line": 529, "kind": "local-variable", "name": "err" }, { "file": "internal/chat/service.go", - "line": 523, + "line": 529, "kind": "local-variable", "name": "message" }, { "file": "internal/chat/service.go", - "line": 530, + "line": 536, "kind": "function", "name": "randomID" }, { "file": "internal/chat/service.go", - "line": 531, + "line": 537, "kind": "var", "name": "value" }, { "file": "internal/chat/service.go", - "line": 532, + "line": 538, "kind": "local-variable", "name": "err" }, { "file": "internal/chat/service.go", - "line": 538, + "line": 544, "kind": "function", "name": "extractString" }, { "file": "internal/chat/service.go", - "line": 539, + "line": 545, "kind": "local-variable", "name": "wanted" }, { "file": "internal/chat/service.go", - "line": 540, + "line": 546, "kind": "range-variable", "name": "key" }, { "file": "internal/chat/service.go", - "line": 543, + "line": 549, "kind": "var", "name": "walk" }, { "file": "internal/chat/service.go", - "line": 545, + "line": 551, "kind": "local-variable", "name": "x" }, { "file": "internal/chat/service.go", - "line": 547, + "line": 553, "kind": "range-variable", "name": "child" }, { "file": "internal/chat/service.go", - "line": 547, + "line": 553, "kind": "range-variable", "name": "key" }, { "file": "internal/chat/service.go", - "line": 548, + "line": 554, "kind": "local-variable", "name": "ok" }, - { - "file": "internal/chat/service.go", - "line": 549, - "kind": "local-variable", - "name": "text" - }, - { - "file": "internal/chat/service.go", - "line": 554, - "kind": "range-variable", - "name": "child" - }, { "file": "internal/chat/service.go", "line": 555, @@ -23617,19 +23623,31 @@ }, { "file": "internal/chat/service.go", - "line": 571, - "kind": "function", - "name": "extractUnixMilli" + "line": 566, + "kind": "range-variable", + "name": "child" }, { "file": "internal/chat/service.go", - "line": 572, + "line": 567, "kind": "local-variable", "name": "text" }, { "file": "internal/chat/service.go", - "line": 573, + "line": 577, + "kind": "function", + "name": "extractUnixMilli" + }, + { + "file": "internal/chat/service.go", + "line": 578, + "kind": "local-variable", + "name": "text" + }, + { + "file": "internal/chat/service.go", + "line": 579, "kind": "var", "name": "value" }, diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index 2be053a..bddc9ac 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -610,3 +610,4 @@ npm --prefix frontend run build | 2026-08-15 | 完成阶段 7 第十个 PR 切片“React AccountAutomation feature 化与任务设置行为边界” | 按 `app/features/accountAutomation` 提取账号任务 API、默认设置与重复执行状态模型、任务设置 Hook;账号切换支持取消和请求代次保护,保存/立即执行支持重复提交阻断、失败重试和结果刷新;新增默认值、动作门禁、API 取消信号测试,保留旧弹窗入口和 API 路径 | 阶段 7:React AccountList 子模块收口与页面组合边界 | | 2026-08-15 | 完成阶段 7 第十一个 PR 切片“React AccountList 子模块收口与页面组合边界” | 新增 `useAccountSubmodules`,集中管理账号编辑、长登录、通知绑定、AI 设置和密码登录状态;编辑弹窗仅保留页面组合与二维码/删除职责,子模块请求支持并行加载、取消和账号/代次隔离;新增跨子模块过期响应、密码登录取消信号和路由边界测试;前端类型检查、全量测试和构建门禁通过并合并为一个可回滚提交 | 阶段 7:React feature 依赖门禁与旧页面入口瘦身 | | 2026-08-15 | 完成阶段 7 第十二个 PR 切片“React feature 依赖门禁与旧页面入口瘦身” | 新增 feature 依赖架构测试,禁止生产页面绕过 API 适配层直接使用共享网络客户端或 `fetch`;会话与健康检查纳入 `session/system` feature API,Sidebar 和 App 不再直接访问共享网络层;账号、卡密、订单和规则的旧 `components/*State` 转发入口及其测试迁移/删除,状态测试归属各自 feature;前端类型检查、184 个测试、注释门禁和构建门禁通过 | 阶段 8:DB 与事务治理第一批窄 repository 边界 | +| 2026-08-15 | 完成阶段 8 第一个 PR 切片“Chat 窄 repository 接口与服务依赖收口” | 新增 `chat.Repository`,聊天服务只持有会话、消息和账号归属所需的最小持久化接口;完整 `db.Store` 仅在构造适配器时出现,新增 `NewWithRepository` 和内存替身测试,保持聊天消息幂等、会话归属与订阅语义不变;Chat 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:通知与账号任务窄 repository 边界 | diff --git a/internal/chat/repository.go b/internal/chat/repository.go new file mode 100644 index 0000000..d57c357 --- /dev/null +++ b/internal/chat/repository.go @@ -0,0 +1,74 @@ +package chat + +import ( + "context" + + "xianyu-go/internal/db" +) + +// Repository 定义聊天服务需要的最小持久化能力,避免业务层持有完整 db.Store。 +type Repository interface { + // ListOwnedIDs 返回用户拥有的账号 ID。 + ListOwnedIDs(ctx context.Context, userID int64) ([]string, error) + // DeleteSession 删除指定账号下的聊天会话。 + DeleteSession(ctx context.Context, cookieID, chatID string) error + // UpsertSession 写入或更新聊天会话摘要。 + UpsertSession(ctx context.Context, session db.ChatSession) error + // SyncSessionSummary 按服务端时间更新聊天会话摘要。 + SyncSessionSummary(ctx context.Context, cookieID, chatID, summary string, sentAt, observedModifyAt int64, unread int) error + // SaveMessage 幂等保存聊天消息并返回落库结果。 + SaveMessage(ctx context.Context, session db.ChatSession, message db.ChatMessage, unread bool) (*db.ChatMessage, bool, error) + // UpdateMessageType 更新历史消息的展示类型。 + UpdateMessageType(ctx context.Context, cookieID, key, messageType string) error + // UpdateMessageStatus 更新外发消息状态并返回最新消息。 + UpdateMessageStatus(ctx context.Context, cookieID, key, status string) (*db.ChatMessage, error) +} + +// storeRepository 将聚合 Store 的聊天相关 repository 适配为窄接口。 +type storeRepository struct { + // store 保存数据库聚合入口,仅用于构造适配器,不进入聊天服务状态。 + store *db.Store +} + +// ListOwnedIDs 委托账号归属查询。 +func (r storeRepository) ListOwnedIDs(ctx context.Context, userID int64) ([]string, error) { + return r.store.Cookies.ListOwnedIDs(ctx, userID) +} + +// DeleteSession 委托聊天会话删除。 +func (r storeRepository) DeleteSession(ctx context.Context, cookieID, chatID string) error { + return r.store.Chats.DeleteSession(ctx, cookieID, chatID) +} + +// UpsertSession 委托聊天会话写入。 +func (r storeRepository) UpsertSession(ctx context.Context, session db.ChatSession) error { + return r.store.Chats.UpsertSession(ctx, session) +} + +// SyncSessionSummary 委托聊天会话摘要同步。 +func (r storeRepository) SyncSessionSummary(ctx context.Context, cookieID, chatID, summary string, sentAt, observedModifyAt int64, unread int) error { + return r.store.Chats.SyncSessionSummary(ctx, cookieID, chatID, summary, sentAt, observedModifyAt, unread) +} + +// SaveMessage 委托聊天消息幂等保存。 +func (r storeRepository) SaveMessage(ctx context.Context, session db.ChatSession, message db.ChatMessage, unread bool) (*db.ChatMessage, bool, error) { + return r.store.Chats.SaveMessage(ctx, session, message, unread) +} + +// UpdateMessageType 委托历史消息类型更新。 +func (r storeRepository) UpdateMessageType(ctx context.Context, cookieID, key, messageType string) error { + return r.store.Chats.UpdateMessageType(ctx, cookieID, key, messageType) +} + +// UpdateMessageStatus 委托外发消息状态更新。 +func (r storeRepository) UpdateMessageStatus(ctx context.Context, cookieID, key, status string) (*db.ChatMessage, error) { + return r.store.Chats.UpdateMessageStatus(ctx, cookieID, key, status) +} + +// newStoreRepository 从完整 Store 构造聊天服务使用的窄 repository。 +func newStoreRepository(store *db.Store) Repository { + if store == nil || store.Cookies == nil || store.Chats == nil { + return nil + } + return storeRepository{store: store} +} diff --git a/internal/chat/repository_test.go b/internal/chat/repository_test.go new file mode 100644 index 0000000..b523c6d --- /dev/null +++ b/internal/chat/repository_test.go @@ -0,0 +1,55 @@ +package chat + +import ( + "context" + "testing" + + "xianyu-go/internal/db" +) + +// fakeRepository 是验证聊天服务窄 repository 依赖的内存替身。 +type fakeRepository struct { + // accountIDs 保存模拟的用户账号归属结果。 + accountIDs []string +} + +// ListOwnedIDs 返回内存替身中的账号归属结果。 +func (r *fakeRepository) ListOwnedIDs(context.Context, int64) ([]string, error) { + return r.accountIDs, nil +} + +// DeleteSession 模拟删除聊天会话。 +func (*fakeRepository) DeleteSession(context.Context, string, string) error { return nil } + +// UpsertSession 模拟写入聊天会话。 +func (*fakeRepository) UpsertSession(context.Context, db.ChatSession) error { return nil } + +// SyncSessionSummary 模拟同步聊天会话摘要。 +func (*fakeRepository) SyncSessionSummary(context.Context, string, string, string, int64, int64, int) error { + return nil +} + +// SaveMessage 模拟幂等保存聊天消息。 +func (*fakeRepository) SaveMessage(_ context.Context, _ db.ChatSession, message db.ChatMessage, _ bool) (*db.ChatMessage, bool, error) { + return &message, true, nil +} + +// UpdateMessageType 模拟更新历史消息类型。 +func (*fakeRepository) UpdateMessageType(context.Context, string, string, string) error { return nil } + +// UpdateMessageStatus 模拟更新外发消息状态。 +func (*fakeRepository) UpdateMessageStatus(_ context.Context, _ string, _ string, status string) (*db.ChatMessage, error) { + return &db.ChatMessage{Status: status}, nil +} + +// TestServiceUsesNarrowRepository 验证聊天服务可以脱离完整 db.Store 运行。 +func TestServiceUsesNarrowRepository(t *testing.T) { + // repository 是只实现聊天所需方法的内存替身。 + repository := &fakeRepository{accountIDs: []string{"account-1", "account-2"}} + service := NewWithRepository(repository) + _, cancel, err := service.Subscribe(context.Background(), 42) + if err != nil { + t.Fatalf("Subscribe() error = %v", err) + } + cancel() +} diff --git a/internal/chat/service.go b/internal/chat/service.go index ed3bf85..a4165c7 100644 --- a/internal/chat/service.go +++ b/internal/chat/service.go @@ -42,7 +42,7 @@ func (s *Service) RecordConversationPage(ctx context.Context, accountID, myID st cid := strings.Split(strings.TrimSpace(fmt.Sprint(single["cid"])), "@")[0] if visible, exists := conv["visible"]; exists && int64Value(visible) == 0 { if cid != "" && cid != "" { - _ = s.store.Chats.DeleteSession(ctx, accountID, cid) + _ = s.repository.DeleteSession(ctx, accountID, cid) } continue } @@ -50,7 +50,7 @@ func (s *Service) RecordConversationPage(ctx context.Context, accountID, myID st second := strings.Split(strings.TrimSpace(fmt.Sprint(single["pairSecond"])), "@")[0] ext := mapValue(single["extension"]) if second == "0" && cleanNilString(ext["extUserId"]) != "1400" { - _ = s.store.Chats.DeleteSession(ctx, accountID, cid) + _ = s.repository.DeleteSession(ctx, accountID, cid) continue } peerID := first @@ -116,10 +116,10 @@ func (s *Service) RecordConversationPage(ctx context.Context, accountID, myID st session := db.ChatSession{CookieID: accountID, ChatID: cid, BuyerID: peerID, BuyerName: peerName, BuyerAvatar: avatar, ItemID: cleanNilString(ext["itemId"]), ItemTitle: cleanNilString(ext["itemTitle"]), LastMessage: summary, LastMessageAt: lastMessageAt, UnreadCount: int(int64Value(conv["redPoint"]))} - if err := s.store.Chats.UpsertSession(ctx, session); err != nil { + if err := s.repository.UpsertSession(ctx, session); err != nil { return page, err } - if err := s.store.Chats.SyncSessionSummary(ctx, accountID, cid, summary, lastMessageAt, modifyTime, session.UnreadCount); err != nil { + if err := s.repository.SyncSessionSummary(ctx, accountID, cid, summary, lastMessageAt, modifyTime, session.UnreadCount); err != nil { return page, err } } @@ -180,18 +180,24 @@ type subscriber struct { } type Service struct { - store *db.Store - mu sync.RWMutex - next uint64 - subs map[uint64]subscriber + // repository 提供聊天服务所需的最小持久化能力。 + repository Repository + mu sync.RWMutex + next uint64 + subs map[uint64]subscriber } func New(store *db.Store) *Service { - return &Service{store: store, subs: make(map[uint64]subscriber)} + return NewWithRepository(newStoreRepository(store)) +} + +// NewWithRepository 使用窄 repository 构造聊天服务,便于应用层和测试隔离数据库聚合器。 +func NewWithRepository(repository Repository) *Service { + return &Service{repository: repository, subs: make(map[uint64]subscriber)} } func (s *Service) Subscribe(ctx context.Context, userID int64) (<-chan Event, func(), error) { - accountIDs, err := s.store.Cookies.ListOwnedIDs(ctx, userID) // accountIDs 和 err 是用户账号 ID 列表及查询错误。 + accountIDs, err := s.repository.ListOwnedIDs(ctx, userID) // accountIDs 和 err 是用户账号 ID 列表及查询错误。 if err != nil { return nil, nil, err } @@ -236,7 +242,7 @@ func (s *Service) Publish(accountID string, event Event) { } func (s *Service) RecordIncoming(ctx context.Context, in Incoming) (*db.ChatMessage, bool, error) { - if s == nil || s.store == nil || s.store.Chats == nil { + if s == nil || s.repository == nil { return nil, false, fmt.Errorf("聊天服务未初始化") } sentAt := extractUnixMilli(in.Raw) @@ -263,7 +269,7 @@ func (s *Service) RecordIncoming(ctx context.Context, in Incoming) (*db.ChatMess } message := db.ChatMessage{MessageKey: key, Direction: "incoming", SenderID: in.BuyerID, SenderName: in.BuyerName, MessageType: messageType, Content: content, Status: "received", SentAt: sentAt} - stored, inserted, err := s.store.Chats.SaveMessage(ctx, session, message, true) + stored, inserted, err := s.repository.SaveMessage(ctx, session, message, true) if err == nil && inserted { s.Publish(in.AccountID, Event{Type: "message.created", Message: stored, Session: &session}) } @@ -286,12 +292,12 @@ func (s *Service) RecordHistoryPage(ctx context.Context, accountID, chatID, myID session.BuyerID = message.SenderID } } - stored, _, err := s.store.Chats.SaveMessage(ctx, session, message, false) + stored, _, err := s.repository.SaveMessage(ctx, session, message, false) if err != nil { return page, err } if message.MessageType == "system" { - if err := s.store.Chats.UpdateMessageType(ctx, accountID, message.MessageKey, "system"); err != nil { + if err := s.repository.UpdateMessageType(ctx, accountID, message.MessageKey, "system"); err != nil { return page, err } stored.MessageType = "system" @@ -496,7 +502,7 @@ func (s *Service) CreateOutgoingMedia(ctx context.Context, session db.ChatSessio message := db.ChatMessage{MessageKey: key, Direction: "outgoing", SenderID: session.CookieID, SenderName: "我", MessageType: messageType, Content: strings.TrimSpace(content), Status: "sending", SentAt: time.Now().UTC().UnixMilli()} - stored, _, err := s.store.Chats.SaveMessage(ctx, session, message, false) + stored, _, err := s.repository.SaveMessage(ctx, session, message, false) if err == nil { s.Publish(session.CookieID, Event{Type: "message.created", Message: stored, Session: &session}) } @@ -512,7 +518,7 @@ func (s *Service) RecordOutgoingSent(ctx context.Context, session db.ChatSession message := db.ChatMessage{MessageKey: "sent-" + randomID(), Direction: "outgoing", SenderID: session.CookieID, SenderName: "我", MessageType: "text", Content: strings.TrimSpace(text), Status: "sent", SentAt: time.Now().UTC().UnixMilli()} - stored, _, err := s.store.Chats.SaveMessage(ctx, session, message, false) + stored, _, err := s.repository.SaveMessage(ctx, session, message, false) if err == nil { s.Publish(session.CookieID, Event{Type: "message.created", Message: stored, Session: &session}) } @@ -520,7 +526,7 @@ func (s *Service) RecordOutgoingSent(ctx context.Context, session db.ChatSession } func (s *Service) SetOutgoingStatus(ctx context.Context, accountID, key, status string) (*db.ChatMessage, error) { - message, err := s.store.Chats.UpdateMessageStatus(ctx, accountID, key, status) + message, err := s.repository.UpdateMessageStatus(ctx, accountID, key, status) if err == nil { s.Publish(accountID, Event{Type: "message.updated", Message: message}) }