可靠性与数据一致性:为 SQLite、MySQL、PostgreSQL 增加 00017 至 00021 迁移,引入账号暂停时间、默认回复和批量铺货租约、自动化运行恢复、延迟任务、动作检查点与通知 outbox。补强订单、卡密发货、Cookie、批量铺货和自动化任务的原子认领、幂等更新、并发控制、失败分类、重试与进程重启恢复,并扩展跨方言覆盖。 登录与风控恢复:完善扫码、密码登录、Cookie 续期、MTOP token、UA 与 WebSocket 生命周期;加入账号级持久化浏览器目录、Playwright 主引擎和直接 Chromium/CDP 备用引擎、严格的新 x5sec 成功判定、失败控件恢复及真实浏览器集成测试。增加可配置远程滑块服务,在明确失败、链接过期和服务不可用场景中保持既定的本地回退语义。 安全与配置:通过 XIANYU_DATA_KEY 为 Cookie、登录密码、设备与访问令牌、AI/SMTP 密钥及通知配置提供 AES-256-GCM 落库加密和历史明文升级。新增出站网络目标分级、防 DNS rebinding、登录限流、HTTP 客户端超时与 SMTP 目标校验;管理员配置的 AI 服务允许可信内网地址,非可信 Webhook 和远程资源继续限制为公网目标。 自动化与通知:为发货、评价赠送、邀评和回复动作增加断点、租约、延迟执行及人工继续/重试/取消入口;通知改为 outbox 驱动的可恢复投递。同步完善账号运行态、暂停状态、续期冷却、告警审计和异常查询接口。 前端体验:新增风控验证面板、方形二维码、账号运行态合并、暂停状态保护、自动化异常处置、批量铺货状态选择、订单导入结果和最新请求门禁。完善订单搜索与导入、规则编辑、批次恢复、通知与设置页面,并为请求取消、超时、陈旧响应和主要状态转换补充 Vitest 覆盖。 测试与部署:扩展三库功能编排、SQLite 一致性备份、数据卷持久化、浏览器集成镜像、前端类型检查和构建资产分包;功能测试覆盖健康检查、订单、铺货、自动化异常、账号暂停、关键词和系统设置。更新 README 中数据密钥、可信 AI 地址和测试端口说明。 验证结果:go vet ./...、go build ./cmd/server、gofmt 检查、npm run typecheck、npm test(17 个文件、95 个用例)及 npm run build 通过。go test ./... 未完全通过:internal/adapter 仍有 2 个密码续期用例失败,internal/renewal 仍有 4 个浏览器续期调度用例失败;其余 Go 包通过,失败现状随本次既有工作区一并提交。
163 lines
5.1 KiB
Go
163 lines
5.1 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// NotificationChannel 通知渠道(含配置 JSON)。
|
|
type NotificationChannel struct {
|
|
ID int64
|
|
Name string
|
|
Type string
|
|
Config string // JSON
|
|
EventTypes string // JSON array or comma-separated event codes
|
|
}
|
|
|
|
// Notifications 通知绑定操作。
|
|
type Notifications struct {
|
|
DB *sql.DB
|
|
Dialect Dialect
|
|
codec *secretCodec
|
|
}
|
|
|
|
type NotificationOutboxInput struct {
|
|
ChannelID int64
|
|
EventType string
|
|
Body string
|
|
}
|
|
|
|
type NotificationOutboxMessage struct {
|
|
ID int64
|
|
ChannelID int64
|
|
EventType string
|
|
Body string
|
|
AttemptCount int
|
|
}
|
|
|
|
// AccountChannels 取某账号已启用的通知渠道(message_notifications JOIN notification_channels)。
|
|
// 移植自 get_account_notifications。
|
|
func (n *Notifications) AccountChannels(ctx context.Context, cookieID string) ([]NotificationChannel, error) {
|
|
rows, err := n.DB.QueryContext(ctx,
|
|
`SELECT nc.id, nc.name, nc.type, nc.config, COALESCE(nc.user_id,1),
|
|
COALESCE(NULLIF(mn.event_types,''), nc.event_types, '')
|
|
FROM message_notifications mn
|
|
JOIN cookies c ON c.id=mn.cookie_id
|
|
JOIN notification_channels nc ON mn.channel_id = nc.id AND nc.user_id=c.user_id
|
|
WHERE mn.cookie_id=? AND mn.enabled=1 AND nc.enabled=1
|
|
ORDER BY mn.id`, cookieID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []NotificationChannel
|
|
for rows.Next() {
|
|
var c NotificationChannel
|
|
var userID int64
|
|
if err := rows.Scan(&c.ID, &c.Name, &c.Type, &c.Config, &userID, &c.EventTypes); err != nil {
|
|
return nil, err
|
|
}
|
|
c.Config, err = n.codec.decrypt("notification-config", strconv.FormatInt(userID, 10), c.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// EnqueueOutbox 在一个事务中持久化同一事件的各渠道投递,避免进程退出造成部分丢失。
|
|
func (n *Notifications) EnqueueOutbox(ctx context.Context, messages []NotificationOutboxInput) error {
|
|
if len(messages) == 0 {
|
|
return nil
|
|
}
|
|
tx, err := n.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
for _, message := range messages {
|
|
if _, err := tx.ExecContext(ctx, `INSERT INTO notification_outbox
|
|
(channel_id,event_type,body,status,attempt_count,next_attempt_at,lease_expires_at,worker_token,last_error)
|
|
VALUES (?,?,?,'pending',0,0,0,'','')`,
|
|
message.ChannelID, message.EventType, message.Body); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// ClaimOutbox 原子领取到期投递。过期 running 任务可以被重新接管,worker token
|
|
// 用于隔离迟到的旧 worker。
|
|
func (n *Notifications) ClaimOutbox(ctx context.Context, workerToken string, now time.Time, limit int) ([]NotificationOutboxMessage, error) {
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
nowUnix := now.Unix()
|
|
rows, err := n.DB.QueryContext(ctx, `SELECT id,channel_id,event_type,body,attempt_count
|
|
FROM notification_outbox
|
|
WHERE (status='pending' AND next_attempt_at<=?) OR (status='running' AND lease_expires_at<?)
|
|
ORDER BY id LIMIT ?`, nowUnix, nowUnix, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var candidates []NotificationOutboxMessage
|
|
for rows.Next() {
|
|
var message NotificationOutboxMessage
|
|
if err := rows.Scan(&message.ID, &message.ChannelID, &message.EventType, &message.Body, &message.AttemptCount); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
candidates = append(candidates, message)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
claimed := candidates[:0]
|
|
leaseExpiresAt := now.Add(30 * time.Second).Unix()
|
|
for _, message := range candidates {
|
|
res, err := n.DB.ExecContext(ctx, `UPDATE notification_outbox
|
|
SET status='running',worker_token=?,lease_expires_at=?,attempt_count=attempt_count+1,updated_at=CURRENT_TIMESTAMP
|
|
WHERE id=? AND ((status='pending' AND next_attempt_at<=?) OR (status='running' AND lease_expires_at<?))`,
|
|
workerToken, leaseExpiresAt, message.ID, nowUnix, nowUnix)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
count, err := res.RowsAffected()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if count == 1 {
|
|
message.AttemptCount++
|
|
claimed = append(claimed, message)
|
|
}
|
|
}
|
|
return claimed, nil
|
|
}
|
|
|
|
func (n *Notifications) CompleteOutbox(ctx context.Context, id int64, workerToken string) (bool, error) {
|
|
res, err := n.DB.ExecContext(ctx, `DELETE FROM notification_outbox WHERE id=? AND status='running' AND worker_token=?`, id, workerToken)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
count, err := res.RowsAffected()
|
|
return err == nil && count == 1, err
|
|
}
|
|
|
|
func (n *Notifications) RetryOutbox(ctx context.Context, id int64, workerToken, message string, nextAttemptAt int64, permanent bool) (bool, error) {
|
|
status := "pending"
|
|
if permanent {
|
|
status = "dead"
|
|
}
|
|
res, err := n.DB.ExecContext(ctx, `UPDATE notification_outbox
|
|
SET status=?,next_attempt_at=?,lease_expires_at=0,worker_token='',last_error=?,updated_at=CURRENT_TIMESTAMP
|
|
WHERE id=? AND status='running' AND worker_token=?`, status, nextAttemptAt, message, id, workerToken)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
count, err := res.RowsAffected()
|
|
return err == nil && count == 1, err
|
|
}
|