阶段八:完成通知与通信应用服务repository收口
This commit is contained in:
1 parent
0fc7f1b433
commit
f46bdd1c8e
7 files changed
+593
-338
No files matched your search
+289
-289
File diff suppressed because it is too large.
Load diff
@@ -612,3 +612,4 @@ npm --prefix frontend run build
|
||||
| 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 边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第二个 PR 切片“账号任务窄 repository 与凭证门禁收口” | 新增 `automation.AccountTaskRepository`,账号任务协调器只持有账号启停/暂停、任务设置/租约、运行凭证和 Cookie 更新所需的最小接口;自动化中心保留 Store 装配适配器,Session 指纹阻断、续期恢复、任务幂等和 Cookie 同步语义保持不变;Automation 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:通知与应用服务 repository 边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第三个 PR 切片“通知与通信应用服务 repository 边界” | 新增 `notify.Repository`,通知器仅持有渠道、SMTP 系统设置和 outbox 所需的最小接口,保留同步发送、异步租约、重试和测试发送语义;新增通信应用服务窄 repository,账号任务、通知渠道/绑定和聊天历史持久化不再直接访问完整 `db.Store`,实时账号、MTOP 和聊天服务仍由 Server 装配;通知/Server 定向测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:剩余应用服务与事务 repository 边界 |
|
||||
+30
-25
@@ -54,12 +54,12 @@ type NotificationEvent struct {
|
||||
|
||||
// Notifier 通知发送器。
|
||||
type Notifier struct {
|
||||
cookieID string
|
||||
store *db.Store
|
||||
logger *slog.Logger
|
||||
httpc *http.Client
|
||||
started atomic.Bool
|
||||
workers sync.WaitGroup
|
||||
cookieID string
|
||||
repository Repository
|
||||
logger *slog.Logger
|
||||
httpc *http.Client
|
||||
started atomic.Bool
|
||||
workers sync.WaitGroup
|
||||
}
|
||||
|
||||
var newOutboundHTTPClient = func() *http.Client { return netguard.PublicHTTPClient(10 * time.Second) }
|
||||
@@ -67,21 +67,26 @@ var dialPublicSMTP = netguard.DialPublicContext
|
||||
|
||||
// New 构造。
|
||||
func New(cookieID string, store *db.Store, logger *slog.Logger) *Notifier {
|
||||
return NewWithRepository(cookieID, newStoreRepository(store), logger)
|
||||
}
|
||||
|
||||
// NewWithRepository 使用通知器所需的窄 repository 构造通知器。
|
||||
func NewWithRepository(cookieID string, repository Repository, logger *slog.Logger) *Notifier {
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
return &Notifier{
|
||||
cookieID: cookieID,
|
||||
store: store,
|
||||
logger: logger.With("account", cookieID, "subsys", "notify"),
|
||||
httpc: newOutboundHTTPClient(),
|
||||
cookieID: cookieID,
|
||||
repository: repository,
|
||||
logger: logger.With("account", cookieID, "subsys", "notify"),
|
||||
httpc: newOutboundHTTPClient(),
|
||||
}
|
||||
}
|
||||
|
||||
// Start 启动持久化 outbox worker。调用返回前会先标记为异步模式,之后的业务
|
||||
// 通知只写数据库,不在订单/账号处理调用栈中等待外部网络。
|
||||
func (n *Notifier) Start(ctx context.Context) {
|
||||
if n == nil || n.store == nil || !n.started.CompareAndSwap(false, true) {
|
||||
if n == nil || n.repository == nil || !n.started.CompareAndSwap(false, true) {
|
||||
return
|
||||
}
|
||||
n.workers.Add(1)
|
||||
@@ -131,13 +136,13 @@ func (n *Notifier) NotifyAccountEvent(accountID, eventType, level, title, body s
|
||||
|
||||
// NotifyEvent 根据事件类型筛选渠道并发送通知。
|
||||
func (n *Notifier) NotifyEvent(ctx context.Context, ev NotificationEvent) {
|
||||
if n.store == nil {
|
||||
if n == nil || n.repository == nil {
|
||||
return
|
||||
}
|
||||
if ev.Time.IsZero() {
|
||||
ev.Time = time.Now()
|
||||
}
|
||||
channels, err := n.store.Notifications.AccountChannels(ctx, ev.AccountID)
|
||||
channels, err := n.repository.AccountChannels(ctx, ev.AccountID)
|
||||
if err != nil || len(channels) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -159,7 +164,7 @@ func (n *Notifier) NotifyEvent(ctx context.Context, ev NotificationEvent) {
|
||||
for _, ch := range eligible {
|
||||
messages = append(messages, db.NotificationOutboxInput{ChannelID: ch.ID, EventType: ev.Type, Body: full})
|
||||
}
|
||||
if err := n.store.Notifications.EnqueueOutbox(ctx, messages); err != nil {
|
||||
if err := n.repository.EnqueueOutbox(ctx, messages); err != nil {
|
||||
n.logger.Error("持久化通知失败", "event_type", ev.Type, "err", err)
|
||||
}
|
||||
return
|
||||
@@ -192,7 +197,7 @@ func (n *Notifier) drainOutbox(ctx context.Context) {
|
||||
n.logger.Error("生成通知 worker token 失败", "err", err)
|
||||
return
|
||||
}
|
||||
messages, err := n.store.Notifications.ClaimOutbox(ctx, workerToken, time.Now(), 20)
|
||||
messages, err := n.repository.ClaimOutbox(ctx, workerToken, time.Now(), 20)
|
||||
if err != nil {
|
||||
if ctx.Err() == nil {
|
||||
n.logger.Warn("领取通知 outbox 失败", "err", err)
|
||||
@@ -200,13 +205,13 @@ func (n *Notifier) drainOutbox(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
for _, message := range messages {
|
||||
channel, getErr := n.store.Notifications.GetChannel(ctx, message.ChannelID)
|
||||
channel, getErr := n.repository.GetChannel(ctx, message.ChannelID)
|
||||
if getErr != nil {
|
||||
n.retryOutbox(ctx, message, workerToken, getErr)
|
||||
continue
|
||||
}
|
||||
if channel == nil {
|
||||
_, _ = n.store.Notifications.CompleteOutbox(ctx, message.ID, workerToken)
|
||||
_, _ = n.repository.CompleteOutbox(ctx, message.ID, workerToken)
|
||||
continue
|
||||
}
|
||||
if sendErr := n.send(*channel, message.Body); sendErr != nil {
|
||||
@@ -214,7 +219,7 @@ func (n *Notifier) drainOutbox(ctx context.Context) {
|
||||
n.retryOutbox(ctx, message, workerToken, sendErr)
|
||||
continue
|
||||
}
|
||||
if completed, completeErr := n.store.Notifications.CompleteOutbox(ctx, message.ID, workerToken); completeErr != nil {
|
||||
if completed, completeErr := n.repository.CompleteOutbox(ctx, message.ID, workerToken); completeErr != nil {
|
||||
n.logger.Warn("确认通知投递完成失败", "outbox_id", message.ID, "err", completeErr)
|
||||
} else if !completed {
|
||||
n.logger.Warn("通知 outbox 租约已转移", "outbox_id", message.ID)
|
||||
@@ -226,7 +231,7 @@ func (n *Notifier) retryOutbox(ctx context.Context, message db.NotificationOutbo
|
||||
permanent := message.AttemptCount >= 10
|
||||
shift := min(max(message.AttemptCount-1, 0), 7)
|
||||
delay := 5 * time.Second * time.Duration(1<<shift)
|
||||
updated, err := n.store.Notifications.RetryOutbox(ctx, message.ID, workerToken, cause.Error(), time.Now().Add(delay).Unix(), permanent)
|
||||
updated, err := n.repository.RetryOutbox(ctx, message.ID, workerToken, cause.Error(), time.Now().Add(delay).Unix(), permanent)
|
||||
if err != nil {
|
||||
n.logger.Warn("更新通知重试状态失败", "outbox_id", message.ID, "err", err)
|
||||
} else if !updated {
|
||||
@@ -244,10 +249,10 @@ func notificationWorkerToken() (string, error) {
|
||||
|
||||
// SendToChannel 直接向指定渠道发送一条消息(用于前端“测试发送”)。
|
||||
func (n *Notifier) SendToChannel(channelID int64, body string) error {
|
||||
if n.store == nil {
|
||||
if n == nil || n.repository == nil {
|
||||
return fmt.Errorf("通知器未初始化")
|
||||
}
|
||||
ch, err := n.store.Notifications.GetChannel(context.Background(), channelID)
|
||||
ch, err := n.repository.GetChannel(context.Background(), channelID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("查询渠道失败: %w", err)
|
||||
}
|
||||
@@ -743,8 +748,8 @@ func (n *Notifier) configOrSetting(ctx context.Context, cfg map[string]any, key,
|
||||
if v := strings.TrimSpace(strOr(cfg, key, "")); v != "" {
|
||||
return v
|
||||
}
|
||||
if n.store != nil && n.store.Settings != nil {
|
||||
if v, err := n.store.Settings.Get(ctx, key); err == nil && strings.TrimSpace(v) != "" {
|
||||
if n.repository != nil {
|
||||
if v, err := n.repository.GetSetting(ctx, key); err == nil && strings.TrimSpace(v) != "" {
|
||||
return strings.TrimSpace(v)
|
||||
}
|
||||
}
|
||||
@@ -764,8 +769,8 @@ func (n *Notifier) smtpConfigValue(ctx context.Context, cfg map[string]any, key,
|
||||
}
|
||||
return fallbackValue
|
||||
}
|
||||
if n.store != nil && n.store.Settings != nil {
|
||||
if value, err := n.store.Settings.Get(ctx, key); err == nil && strings.TrimSpace(value) != "" {
|
||||
if n.repository != nil {
|
||||
if value, err := n.repository.GetSetting(ctx, key); err == nil && strings.TrimSpace(value) != "" {
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// Repository 定义通知器读取渠道、系统 SMTP 配置并处理持久化 outbox 所需的最小能力。
|
||||
type Repository interface {
|
||||
// AccountChannels 返回账号当前绑定的通知渠道。
|
||||
AccountChannels(ctx context.Context, cookieID string) ([]db.NotificationChannel, error)
|
||||
// EnqueueOutbox 将待发送通知追加到持久化 outbox。
|
||||
EnqueueOutbox(ctx context.Context, messages []db.NotificationOutboxInput) error
|
||||
// ClaimOutbox 抢占一批可发送的 outbox 消息。
|
||||
ClaimOutbox(ctx context.Context, workerToken string, now time.Time, limit int) ([]db.NotificationOutboxMessage, error)
|
||||
// GetChannel 查询通知渠道的完整配置。
|
||||
GetChannel(ctx context.Context, channelID int64) (*db.NotificationChannel, error)
|
||||
// CompleteOutbox 确认当前 worker 已完成 outbox 消息。
|
||||
CompleteOutbox(ctx context.Context, messageID int64, workerToken string) (bool, error)
|
||||
// RetryOutbox 写入 outbox 消息的重试状态。
|
||||
RetryOutbox(ctx context.Context, messageID int64, workerToken, lastError string, nextAttemptAt int64, permanent bool) (bool, error)
|
||||
// GetSetting 读取系统设置中的字符串值。
|
||||
GetSetting(ctx context.Context, key string) (string, error)
|
||||
}
|
||||
|
||||
// storeRepository 将完整 Store 适配为通知器窄 repository。
|
||||
type storeRepository struct {
|
||||
// store 保存数据库聚合入口,仅在适配器内部使用。
|
||||
store *db.Store
|
||||
}
|
||||
|
||||
// AccountChannels 委托账号通知渠道查询。
|
||||
func (r storeRepository) AccountChannels(ctx context.Context, cookieID string) ([]db.NotificationChannel, error) {
|
||||
return r.store.Notifications.AccountChannels(ctx, cookieID)
|
||||
}
|
||||
|
||||
// EnqueueOutbox 委托通知 outbox 写入。
|
||||
func (r storeRepository) EnqueueOutbox(ctx context.Context, messages []db.NotificationOutboxInput) error {
|
||||
return r.store.Notifications.EnqueueOutbox(ctx, messages)
|
||||
}
|
||||
|
||||
// ClaimOutbox 委托通知 outbox 抢占。
|
||||
func (r storeRepository) ClaimOutbox(ctx context.Context, workerToken string, now time.Time, limit int) ([]db.NotificationOutboxMessage, error) {
|
||||
return r.store.Notifications.ClaimOutbox(ctx, workerToken, now, limit)
|
||||
}
|
||||
|
||||
// GetChannel 委托通知渠道查询。
|
||||
func (r storeRepository) GetChannel(ctx context.Context, channelID int64) (*db.NotificationChannel, error) {
|
||||
return r.store.Notifications.GetChannel(ctx, channelID)
|
||||
}
|
||||
|
||||
// CompleteOutbox 委托通知 outbox 完成确认。
|
||||
func (r storeRepository) CompleteOutbox(ctx context.Context, messageID int64, workerToken string) (bool, error) {
|
||||
return r.store.Notifications.CompleteOutbox(ctx, messageID, workerToken)
|
||||
}
|
||||
|
||||
// RetryOutbox 委托通知 outbox 重试状态更新。
|
||||
func (r storeRepository) RetryOutbox(ctx context.Context, messageID int64, workerToken, lastError string, nextAttemptAt int64, permanent bool) (bool, error) {
|
||||
return r.store.Notifications.RetryOutbox(ctx, messageID, workerToken, lastError, nextAttemptAt, permanent)
|
||||
}
|
||||
|
||||
// GetSetting 委托系统设置读取。
|
||||
func (r storeRepository) GetSetting(ctx context.Context, key string) (string, error) {
|
||||
if r.store.Settings == nil {
|
||||
return "", errors.New("系统设置 repository 未初始化")
|
||||
}
|
||||
return r.store.Settings.Get(ctx, key)
|
||||
}
|
||||
|
||||
// newStoreRepository 从完整 Store 构造通知器使用的窄 repository。
|
||||
func newStoreRepository(store *db.Store) Repository {
|
||||
if store == nil || store.Notifications == nil {
|
||||
return nil
|
||||
}
|
||||
return storeRepository{store: store}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ func newApplicationServices(server *Server) *applicationServices {
|
||||
orders: &orderApplicationService{server: server},
|
||||
itemPublish: &itemPublishService{server: server},
|
||||
accountLogin: &accountLoginService{server: server},
|
||||
communication: &communicationService{server: server},
|
||||
communication: &communicationService{server: server, repository: newStoreCommunicationRepository(server.Store)},
|
||||
analytics: &analyticsService{server: server},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// communicationRepository 定义通信应用服务隔离账号任务、通知绑定和聊天历史所需的最小持久化能力。
|
||||
type communicationRepository interface {
|
||||
// GetAccountTaskSettings 读取账号任务设置。
|
||||
GetAccountTaskSettings(ctx context.Context, cookieID string) (db.AccountTaskSettings, error)
|
||||
// UpsertAccountTaskSettings 保存账号任务设置。
|
||||
UpsertAccountTaskSettings(ctx context.Context, settings db.AccountTaskSettings) error
|
||||
// ListAccountTaskRuns 查询账号任务运行记录。
|
||||
ListAccountTaskRuns(ctx context.Context, cookieID string, limit int) ([]db.AccountTaskRun, error)
|
||||
// ListNotificationChannels 查询用户拥有的通知渠道。
|
||||
ListNotificationChannels(ctx context.Context, userID int64) ([]db.NotificationChannelRow, error)
|
||||
// CreateNotificationChannel 创建通知渠道。
|
||||
CreateNotificationChannel(ctx context.Context, row *db.NotificationChannelRow) (int64, error)
|
||||
// UpdateNotificationChannel 更新用户拥有的通知渠道。
|
||||
UpdateNotificationChannel(ctx context.Context, row *db.NotificationChannelRow, userID int64) error
|
||||
// GetNotificationChannel 查询用户拥有的单个通知渠道。
|
||||
GetNotificationChannel(ctx context.Context, channelID, userID int64) (*db.NotificationChannelRow, error)
|
||||
// DeleteNotificationChannel 删除用户拥有的通知渠道。
|
||||
DeleteNotificationChannel(ctx context.Context, channelID, userID int64) error
|
||||
// GetNotificationChannelConfig 查询通知渠道发送配置。
|
||||
GetNotificationChannelConfig(ctx context.Context, channelID int64) (*db.NotificationChannel, error)
|
||||
// ListNotificationBindings 查询用户账号通知绑定。
|
||||
ListNotificationBindings(ctx context.Context, userID int64) ([]db.NotificationBindingRow, error)
|
||||
// SetNotificationBindings 覆盖保存账号通知绑定。
|
||||
SetNotificationBindings(ctx context.Context, cookieID string, channelIDs []int64) error
|
||||
// GetNotificationBindingIDs 查询账号当前启用的通知渠道。
|
||||
GetNotificationBindingIDs(ctx context.Context, cookieID string) ([]int64, error)
|
||||
// SetSingleNotificationBinding 更新单个通知绑定状态。
|
||||
SetSingleNotificationBinding(ctx context.Context, cookieID string, channelID int64, enabled bool) error
|
||||
// DeleteNotificationBinding 删除用户账号的一条通知绑定。
|
||||
DeleteNotificationBinding(ctx context.Context, userID, bindingID int64) error
|
||||
// DeleteAccountNotificationBindings 删除用户账号的全部通知绑定。
|
||||
DeleteAccountNotificationBindings(ctx context.Context, userID int64, cookieID string) error
|
||||
// GetCookieValue 读取账号 Cookie 明文。
|
||||
GetCookieValue(ctx context.Context, cookieID string) (string, error)
|
||||
// UpdateCookieValue 更新已存在账号的 Cookie 明文。
|
||||
UpdateCookieValue(ctx context.Context, cookieID, value string) error
|
||||
// MarkChatRead 将聊天会话标记为已读。
|
||||
MarkChatRead(ctx context.Context, userID int64, accountID, chatID string) error
|
||||
// ListChatMessages 查询聊天历史消息。
|
||||
ListChatMessages(ctx context.Context, userID int64, accountID, chatID string, beforeID int64, limit int) ([]db.ChatMessage, error)
|
||||
// ListChatSessions 查询账号聊天会话列表。
|
||||
ListChatSessions(ctx context.Context, userID int64, accountID string, limit int) ([]db.ChatSession, error)
|
||||
}
|
||||
|
||||
// storeCommunicationRepository 将完整 Store 适配为通信应用服务窄 repository。
|
||||
type storeCommunicationRepository struct {
|
||||
// store 保存数据库聚合入口,仅在适配器内部使用。
|
||||
store *db.Store
|
||||
}
|
||||
|
||||
// GetAccountTaskSettings 委托账号任务设置查询。
|
||||
func (r storeCommunicationRepository) GetAccountTaskSettings(ctx context.Context, cookieID string) (db.AccountTaskSettings, error) {
|
||||
return r.store.AccountTasks.Get(ctx, cookieID)
|
||||
}
|
||||
|
||||
// UpsertAccountTaskSettings 委托账号任务设置写入。
|
||||
func (r storeCommunicationRepository) UpsertAccountTaskSettings(ctx context.Context, settings db.AccountTaskSettings) error {
|
||||
return r.store.AccountTasks.Upsert(ctx, settings)
|
||||
}
|
||||
|
||||
// ListAccountTaskRuns 委托账号任务运行记录查询。
|
||||
func (r storeCommunicationRepository) ListAccountTaskRuns(ctx context.Context, cookieID string, limit int) ([]db.AccountTaskRun, error) {
|
||||
return r.store.AccountTasks.RecentRuns(ctx, cookieID, limit)
|
||||
}
|
||||
|
||||
// ListNotificationChannels 委托用户通知渠道查询。
|
||||
func (r storeCommunicationRepository) ListNotificationChannels(ctx context.Context, userID int64) ([]db.NotificationChannelRow, error) {
|
||||
return r.store.Notifications.AllChannelsForUser(ctx, userID)
|
||||
}
|
||||
|
||||
// CreateNotificationChannel 委托通知渠道创建。
|
||||
func (r storeCommunicationRepository) CreateNotificationChannel(ctx context.Context, row *db.NotificationChannelRow) (int64, error) {
|
||||
return r.store.Notifications.CreateChannel(ctx, row)
|
||||
}
|
||||
|
||||
// UpdateNotificationChannel 委托通知渠道更新。
|
||||
func (r storeCommunicationRepository) UpdateNotificationChannel(ctx context.Context, row *db.NotificationChannelRow, userID int64) error {
|
||||
return r.store.Notifications.UpdateChannelForUser(ctx, row, userID)
|
||||
}
|
||||
|
||||
// GetNotificationChannel 委托带用户归属的通知渠道查询。
|
||||
func (r storeCommunicationRepository) GetNotificationChannel(ctx context.Context, channelID, userID int64) (*db.NotificationChannelRow, error) {
|
||||
return r.store.Notifications.GetChannelRowForUser(ctx, channelID, userID)
|
||||
}
|
||||
|
||||
// DeleteNotificationChannel 委托通知渠道删除。
|
||||
func (r storeCommunicationRepository) DeleteNotificationChannel(ctx context.Context, channelID, userID int64) error {
|
||||
return r.store.Notifications.DeleteChannelForUser(ctx, channelID, userID)
|
||||
}
|
||||
|
||||
// GetNotificationChannelConfig 委托通知渠道发送配置查询。
|
||||
func (r storeCommunicationRepository) GetNotificationChannelConfig(ctx context.Context, channelID int64) (*db.NotificationChannel, error) {
|
||||
return r.store.Notifications.GetChannel(ctx, channelID)
|
||||
}
|
||||
|
||||
// ListNotificationBindings 委托通知绑定查询。
|
||||
func (r storeCommunicationRepository) ListNotificationBindings(ctx context.Context, userID int64) ([]db.NotificationBindingRow, error) {
|
||||
return r.store.Notifications.ListBindingsForUser(ctx, userID)
|
||||
}
|
||||
|
||||
// SetNotificationBindings 委托通知绑定覆盖写入。
|
||||
func (r storeCommunicationRepository) SetNotificationBindings(ctx context.Context, cookieID string, channelIDs []int64) error {
|
||||
return r.store.Notifications.SetBindings(ctx, cookieID, channelIDs)
|
||||
}
|
||||
|
||||
// GetNotificationBindingIDs 委托账号通知绑定查询。
|
||||
func (r storeCommunicationRepository) GetNotificationBindingIDs(ctx context.Context, cookieID string) ([]int64, error) {
|
||||
return r.store.Notifications.AccountBindings(ctx, cookieID)
|
||||
}
|
||||
|
||||
// SetSingleNotificationBinding 委托单个通知绑定状态更新。
|
||||
func (r storeCommunicationRepository) SetSingleNotificationBinding(ctx context.Context, cookieID string, channelID int64, enabled bool) error {
|
||||
return r.store.Notifications.SetSingleBinding(ctx, cookieID, channelID, enabled)
|
||||
}
|
||||
|
||||
// DeleteNotificationBinding 委托通知绑定删除。
|
||||
func (r storeCommunicationRepository) DeleteNotificationBinding(ctx context.Context, userID, bindingID int64) error {
|
||||
return r.store.Notifications.DeleteBinding(ctx, userID, bindingID)
|
||||
}
|
||||
|
||||
// DeleteAccountNotificationBindings 委托账号通知绑定删除。
|
||||
func (r storeCommunicationRepository) DeleteAccountNotificationBindings(ctx context.Context, userID int64, cookieID string) error {
|
||||
return r.store.Notifications.DeleteAccountBindings(ctx, userID, cookieID)
|
||||
}
|
||||
|
||||
// GetCookieValue 委托账号 Cookie 查询。
|
||||
func (r storeCommunicationRepository) GetCookieValue(ctx context.Context, cookieID string) (string, error) {
|
||||
return r.store.Cookies.GetValue(ctx, cookieID)
|
||||
}
|
||||
|
||||
// UpdateCookieValue 委托账号 Cookie 更新。
|
||||
func (r storeCommunicationRepository) UpdateCookieValue(ctx context.Context, cookieID, value string) error {
|
||||
return r.store.Cookies.UpdateValueExisting(ctx, cookieID, value)
|
||||
}
|
||||
|
||||
// MarkChatRead 委托聊天已读状态更新。
|
||||
func (r storeCommunicationRepository) MarkChatRead(ctx context.Context, userID int64, accountID, chatID string) error {
|
||||
return r.store.Chats.MarkRead(ctx, userID, accountID, chatID)
|
||||
}
|
||||
|
||||
// ListChatMessages 委托聊天消息查询。
|
||||
func (r storeCommunicationRepository) ListChatMessages(ctx context.Context, userID int64, accountID, chatID string, beforeID int64, limit int) ([]db.ChatMessage, error) {
|
||||
return r.store.Chats.ListMessages(ctx, userID, accountID, chatID, beforeID, limit)
|
||||
}
|
||||
|
||||
// ListChatSessions 委托聊天会话查询。
|
||||
func (r storeCommunicationRepository) ListChatSessions(ctx context.Context, userID int64, accountID string, limit int) ([]db.ChatSession, error) {
|
||||
return r.store.Chats.ListSessions(ctx, userID, accountID, limit)
|
||||
}
|
||||
|
||||
// newStoreCommunicationRepository 从完整 Store 构造通信应用服务窄 repository。
|
||||
func newStoreCommunicationRepository(store *db.Store) communicationRepository {
|
||||
if store == nil || store.AccountTasks == nil || store.Notifications == nil || store.Cookies == nil || store.Chats == nil {
|
||||
return nil
|
||||
}
|
||||
return storeCommunicationRepository{store: store}
|
||||
}
|
||||
|
||||
// 确保 Store 适配器始终覆盖通信应用服务所需的全部能力。
|
||||
var _ communicationRepository = storeCommunicationRepository{}
|
||||
@@ -27,6 +27,8 @@ var (
|
||||
type communicationService struct {
|
||||
// server 提供数据库、聊天事件中心、账号运行时和通知器依赖。
|
||||
server *Server
|
||||
// repository 提供账号任务、通知绑定和聊天历史的最小持久化能力。
|
||||
repository communicationRepository
|
||||
}
|
||||
|
||||
// accountTaskUpdateInput 是账号任务设置更新的业务输入。
|
||||
@@ -90,7 +92,7 @@ func (s *Server) communicationApplication() *communicationService {
|
||||
|
||||
// GetAccountTaskSettings 读取指定账号的任务设置。
|
||||
func (svc *communicationService) GetAccountTaskSettings(ctx context.Context, cookieID string) (db.AccountTaskSettings, error) {
|
||||
return svc.server.Store.AccountTasks.Get(ctx, cookieID)
|
||||
return svc.repository.GetAccountTaskSettings(ctx, cookieID)
|
||||
}
|
||||
|
||||
// UpdateAccountTaskSettings 校验并保存账号任务设置,然后返回数据库中的最终值。
|
||||
@@ -109,15 +111,15 @@ func (svc *communicationService) UpdateAccountTaskSettings(ctx context.Context,
|
||||
return db.AccountTaskSettings{}, errors.New("擦亮时间格式必须为 HH:mm")
|
||||
}
|
||||
// err 表示任务设置持久化错误。
|
||||
if err := svc.server.Store.AccountTasks.Upsert(ctx, settings); err != nil {
|
||||
if err := svc.repository.UpsertAccountTaskSettings(ctx, settings); err != nil {
|
||||
return db.AccountTaskSettings{}, err
|
||||
}
|
||||
return svc.server.Store.AccountTasks.Get(ctx, input.CookieID)
|
||||
return svc.repository.GetAccountTaskSettings(ctx, input.CookieID)
|
||||
}
|
||||
|
||||
// ListAccountTaskRuns 查询账号最近的任务执行记录。
|
||||
func (svc *communicationService) ListAccountTaskRuns(ctx context.Context, cookieID string, limit int) ([]db.AccountTaskRun, error) {
|
||||
return svc.server.Store.AccountTasks.RecentRuns(ctx, cookieID, limit)
|
||||
return svc.repository.ListAccountTaskRuns(ctx, cookieID, limit)
|
||||
}
|
||||
|
||||
// RunAccountTask 执行一次账号自动化任务并返回执行摘要。
|
||||
@@ -130,27 +132,27 @@ func (svc *communicationService) RunAccountTask(ctx context.Context, cookieID, t
|
||||
|
||||
// ListNotificationChannels 查询用户拥有的通知渠道。
|
||||
func (svc *communicationService) ListNotificationChannels(ctx context.Context, userID int64) ([]db.NotificationChannelRow, error) {
|
||||
return svc.server.Store.Notifications.AllChannelsForUser(ctx, userID)
|
||||
return svc.repository.ListNotificationChannels(ctx, userID)
|
||||
}
|
||||
|
||||
// CreateNotificationChannel 创建用户通知渠道。
|
||||
func (svc *communicationService) CreateNotificationChannel(ctx context.Context, row db.NotificationChannelRow) (int64, error) {
|
||||
return svc.server.Store.Notifications.CreateChannel(ctx, &row)
|
||||
return svc.repository.CreateNotificationChannel(ctx, &row)
|
||||
}
|
||||
|
||||
// UpdateNotificationChannel 更新用户拥有的通知渠道。
|
||||
func (svc *communicationService) UpdateNotificationChannel(ctx context.Context, row db.NotificationChannelRow, userID int64) error {
|
||||
return svc.server.Store.Notifications.UpdateChannelForUser(ctx, &row, userID)
|
||||
return svc.repository.UpdateNotificationChannel(ctx, &row, userID)
|
||||
}
|
||||
|
||||
// GetNotificationChannel 查询用户拥有的单个通知渠道。
|
||||
func (svc *communicationService) GetNotificationChannel(ctx context.Context, channelID, userID int64) (*db.NotificationChannelRow, error) {
|
||||
return svc.server.Store.Notifications.GetChannelRowForUser(ctx, channelID, userID)
|
||||
return svc.repository.GetNotificationChannel(ctx, channelID, userID)
|
||||
}
|
||||
|
||||
// DeleteNotificationChannel 删除用户拥有的通知渠道。
|
||||
func (svc *communicationService) DeleteNotificationChannel(ctx context.Context, channelID, userID int64) error {
|
||||
return svc.server.Store.Notifications.DeleteChannelForUser(ctx, channelID, userID)
|
||||
return svc.repository.DeleteNotificationChannel(ctx, channelID, userID)
|
||||
}
|
||||
|
||||
// TestNotificationChannel 向用户拥有的通知渠道发送测试消息。
|
||||
@@ -159,7 +161,7 @@ func (svc *communicationService) TestNotificationChannel(ctx context.Context, ch
|
||||
return errCommunicationUnavailable
|
||||
}
|
||||
// channel 和 err 保存通知渠道查询结果。
|
||||
channel, err := svc.server.Store.Notifications.GetChannel(ctx, channelID)
|
||||
channel, err := svc.repository.GetNotificationChannelConfig(ctx, channelID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -167,7 +169,7 @@ func (svc *communicationService) TestNotificationChannel(ctx context.Context, ch
|
||||
return db.ErrForbidden
|
||||
}
|
||||
// row 和 err 保存带用户归属的渠道查询结果。
|
||||
row, err := svc.server.Store.Notifications.GetChannelRowForUser(ctx, channelID, userID)
|
||||
row, err := svc.repository.GetNotificationChannel(ctx, channelID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -180,7 +182,7 @@ func (svc *communicationService) TestNotificationChannel(ctx context.Context, ch
|
||||
// ListNotificationBindings 查询用户账号的通知绑定。
|
||||
func (svc *communicationService) ListNotificationBindings(ctx context.Context, userID int64) (map[string][]notificationBindingRow, error) {
|
||||
// rows 和 err 保存绑定查询结果集。
|
||||
rows, err := svc.server.Store.Notifications.ListBindingsForUser(ctx, userID)
|
||||
rows, err := svc.repository.ListNotificationBindings(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -196,30 +198,30 @@ func (svc *communicationService) ListNotificationBindings(ctx context.Context, u
|
||||
|
||||
// SetNotificationBindings 覆盖保存账号的通知渠道绑定。
|
||||
func (svc *communicationService) SetNotificationBindings(ctx context.Context, cookieID string, channelIDs []int64) error {
|
||||
return svc.server.Store.Notifications.SetBindings(ctx, cookieID, channelIDs)
|
||||
return svc.repository.SetNotificationBindings(ctx, cookieID, channelIDs)
|
||||
}
|
||||
|
||||
// GetNotificationBindingIDs 查询账号当前启用的通知渠道标识。
|
||||
func (svc *communicationService) GetNotificationBindingIDs(ctx context.Context, cookieID string) ([]int64, error) {
|
||||
return svc.server.Store.Notifications.AccountBindings(ctx, cookieID)
|
||||
return svc.repository.GetNotificationBindingIDs(ctx, cookieID)
|
||||
}
|
||||
|
||||
// SetSingleNotificationBinding 更新单个账号通知渠道的启用状态。
|
||||
func (svc *communicationService) SetSingleNotificationBinding(ctx context.Context, cookieID string, channelID int64, enabled bool) error {
|
||||
if !enabled {
|
||||
return svc.server.Store.Notifications.SetSingleBinding(ctx, cookieID, channelID, false)
|
||||
return svc.repository.SetSingleNotificationBinding(ctx, cookieID, channelID, false)
|
||||
}
|
||||
return svc.server.Store.Notifications.SetSingleBinding(ctx, cookieID, channelID, true)
|
||||
return svc.repository.SetSingleNotificationBinding(ctx, cookieID, channelID, true)
|
||||
}
|
||||
|
||||
// DeleteNotificationBinding 删除用户账号下的一条通知绑定。
|
||||
func (svc *communicationService) DeleteNotificationBinding(ctx context.Context, userID, bindingID int64) error {
|
||||
return svc.server.Store.Notifications.DeleteBinding(ctx, userID, bindingID)
|
||||
return svc.repository.DeleteNotificationBinding(ctx, userID, bindingID)
|
||||
}
|
||||
|
||||
// DeleteAccountNotificationBindings 删除用户账号的全部通知绑定。
|
||||
func (svc *communicationService) DeleteAccountNotificationBindings(ctx context.Context, userID int64, cookieID string) error {
|
||||
return svc.server.Store.Notifications.DeleteAccountBindings(ctx, userID, cookieID)
|
||||
return svc.repository.DeleteAccountNotificationBindings(ctx, userID, cookieID)
|
||||
}
|
||||
|
||||
// SendChatText 创建并发送一条文字消息,失败时保留可重试的本地失败状态。
|
||||
@@ -266,7 +268,7 @@ func (svc *communicationService) SendChatImage(ctx context.Context, input chatIm
|
||||
return nil, errChatOffline
|
||||
}
|
||||
// cookies 和 err 保存图片上传使用的账号凭证。
|
||||
cookies, err := s.Store.Cookies.GetValue(ctx, input.Session.CookieID)
|
||||
cookies, err := svc.repository.GetCookieValue(ctx, input.Session.CookieID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取账号凭证失败: %w", err)
|
||||
}
|
||||
@@ -284,7 +286,7 @@ func (svc *communicationService) SendChatImage(ctx context.Context, input chatIm
|
||||
}
|
||||
if upload.UpdatedCookies != "" && upload.UpdatedCookies != cookies {
|
||||
// err 表示保存图片上传后刷新 Cookie 的错误,仅忽略并继续发送。
|
||||
_ = s.Store.Cookies.UpdateValueExisting(ctx, input.Session.CookieID, upload.UpdatedCookies)
|
||||
_ = svc.repository.UpdateCookieValue(ctx, input.Session.CookieID, upload.UpdatedCookies)
|
||||
sender.UpdateCookie(upload.UpdatedCookies)
|
||||
}
|
||||
// message 和 err 保存图片消息落库结果。
|
||||
@@ -308,20 +310,20 @@ func (svc *communicationService) SendChatImage(ctx context.Context, input chatIm
|
||||
|
||||
// MarkChatRead 将指定聊天会话标记为已读。
|
||||
func (svc *communicationService) MarkChatRead(ctx context.Context, userID int64, accountID, chatID string) error {
|
||||
return svc.server.Store.Chats.MarkRead(ctx, userID, accountID, chatID)
|
||||
return svc.repository.MarkChatRead(ctx, userID, accountID, chatID)
|
||||
}
|
||||
|
||||
// ListStoredChatMessages 查询本地聊天历史并返回会话摘要。
|
||||
func (svc *communicationService) ListStoredChatMessages(ctx context.Context, userID int64, accountID, chatID string, beforeID int64, limit int) (chatMessagePageResult, error) {
|
||||
// messages 和 err 保存本地聊天历史查询结果。
|
||||
messages, err := svc.server.Store.Chats.ListMessages(ctx, userID, accountID, chatID, beforeID, limit)
|
||||
messages, err := svc.repository.ListChatMessages(ctx, userID, accountID, chatID, beforeID, limit)
|
||||
if err != nil {
|
||||
return chatMessagePageResult{}, err
|
||||
}
|
||||
// session 保存当前聊天会话摘要。
|
||||
var session db.ChatSession
|
||||
// sessions 和 sessionErr 保存当前用户的聊天会话列表及查询错误。
|
||||
if sessions, sessionErr := svc.server.Store.Chats.ListSessions(ctx, userID, accountID, 500); sessionErr == nil {
|
||||
if sessions, sessionErr := svc.repository.ListChatSessions(ctx, userID, accountID, 500); sessionErr == nil {
|
||||
// candidate 是当前遍历到的会话摘要。
|
||||
for _, candidate := range sessions {
|
||||
if candidate.ChatID == chatID {
|
||||
|
||||
Reference in new issue
Block a user