修复聊天状态并完善风控浏览器诊断

修复内容:
- 持久化聊天消息已读状态与未读边界,过滤系统通知红点,修正实时消息 ID、已读回执和前端徽标显示
- 完善账号级任务、商品接口和消息调度联动,补充相关服务端与引擎测试
- 新增 token 风控诊断包,保存页面、截图、Frame、选择器、网络事件和完整指纹信息
- 规范化无头 Chromium 的 User-Agent 与 Client Hints,覆盖 Playwright 主引擎和直接 CDP 备用引擎
- 增加可选证书错误兼容开关、真实 Chromium 指纹集成测试,并同步冻结规范
- 新增 SQLite、MySQL、Postgres 聊天状态迁移并重新构建嵌入式前端
This commit is contained in:
Christ committed 2026-08-17 18:41:49 +08:00
1 parent 1185551018
commit ecfe43b5a4
43 files changed
+2244 -113

No files matched your search

+58 -8
View File
@@ -33,6 +33,8 @@ type ChatMessage struct {
MessageType string `json:"message_type"`
Content string `json:"content"`
Status string `json:"status"`
ReadStatus int `json:"read_status"`
ReadAt int64 `json:"read_at,omitempty"`
SentAt int64 `json:"sent_at"`
}
@@ -135,6 +137,13 @@ func (s *ChatStore) SaveMessage(ctx context.Context, session ChatSession, messag
if message.SentAt <= 0 {
message.SentAt = time.Now().UTC().UnixMilli()
}
// read_status is also used for incoming messages: only a newly received
// real-user message starts unread. Imported history and official system
// notices must never contribute to the chat badge.
if message.Direction == "incoming" && (!unread || message.MessageType == "system") {
message.ReadStatus = 2
message.ReadAt = time.Now().UTC().UnixMilli()
}
message.CookieID, message.ChatID = session.CookieID, session.ChatID
now := time.Now().UTC().Unix()
tx, err := s.DB.BeginTx(ctx, nil)
@@ -155,11 +164,11 @@ func (s *ChatStore) SaveMessage(ctx context.Context, session ChatSession, messag
prefix := dialectInsertIgnorePrefix(s.Dialect)
query := prefix + ` INTO chat_messages
(cookie_id,chat_id,message_key,direction,sender_id,sender_name,message_type,content,status,sent_at,created_at)
VALUES(?,?,?,?,?,?,?,?,?,?,?)` + dialectInsertIgnore(s.Dialect, []string{"cookie_id", "message_key"})
(cookie_id,chat_id,message_key,direction,sender_id,sender_name,message_type,content,status,read_status,read_at,sent_at,created_at)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)` + dialectInsertIgnore(s.Dialect, []string{"cookie_id", "message_key"})
res, err := tx.ExecContext(ctx, query, message.CookieID, message.ChatID, message.MessageKey,
message.Direction, message.SenderID, message.SenderName, message.MessageType, message.Content,
message.Status, message.SentAt, now)
message.Status, message.ReadStatus, message.ReadAt, message.SentAt, now)
if err != nil {
return nil, false, fmt.Errorf("保存聊天消息: %w", err)
}
@@ -188,10 +197,10 @@ func (s *ChatStore) SaveMessage(ctx context.Context, session ChatSession, messag
func (s *ChatStore) GetMessageByKey(ctx context.Context, cookieID, key string) (*ChatMessage, error) {
var m ChatMessage
err := s.DB.QueryRowContext(ctx, `SELECT id,cookie_id,chat_id,message_key,direction,sender_id,sender_name,message_type,content,status,sent_at
err := s.DB.QueryRowContext(ctx, `SELECT id,cookie_id,chat_id,message_key,direction,sender_id,sender_name,message_type,content,status,read_status,read_at,sent_at
FROM chat_messages WHERE cookie_id=? AND message_key=?`, cookieID, key).Scan(
&m.ID, &m.CookieID, &m.ChatID, &m.MessageKey, &m.Direction, &m.SenderID, &m.SenderName,
&m.MessageType, &m.Content, &m.Status, &m.SentAt)
&m.MessageType, &m.Content, &m.Status, &m.ReadStatus, &m.ReadAt, &m.SentAt)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
}
@@ -234,7 +243,7 @@ func (s *ChatStore) ListMessages(ctx context.Context, userID int64, cookieID, ch
if limit <= 0 || limit > 200 {
limit = 50
}
query := `SELECT m.id,m.cookie_id,m.chat_id,m.message_key,m.direction,m.sender_id,m.sender_name,m.message_type,m.content,m.status,m.sent_at
query := `SELECT m.id,m.cookie_id,m.chat_id,m.message_key,m.direction,m.sender_id,m.sender_name,m.message_type,m.content,m.status,m.read_status,m.read_at,m.sent_at
FROM chat_messages m JOIN cookies c ON c.id=m.cookie_id
WHERE c.user_id=? AND m.cookie_id=? AND m.chat_id=?`
args := []any{userID, cookieID, chatID}
@@ -254,7 +263,7 @@ func (s *ChatStore) ListMessages(ctx context.Context, userID int64, cookieID, ch
for rows.Next() {
var m ChatMessage
if err := rows.Scan(&m.ID, &m.CookieID, &m.ChatID, &m.MessageKey, &m.Direction, &m.SenderID,
&m.SenderName, &m.MessageType, &m.Content, &m.Status, &m.SentAt); err != nil {
&m.SenderName, &m.MessageType, &m.Content, &m.Status, &m.ReadStatus, &m.ReadAt, &m.SentAt); err != nil {
return nil, err
}
result = append(result, m)
@@ -267,15 +276,56 @@ func (s *ChatStore) ListMessages(ctx context.Context, userID int64, cookieID, ch
}
func (s *ChatStore) MarkRead(ctx context.Context, userID int64, cookieID, chatID string) error {
now := time.Now().UTC()
if _, err := s.DB.ExecContext(ctx, `UPDATE chat_messages SET read_status=2,read_at=?
WHERE cookie_id=? AND chat_id=? AND direction='incoming' AND message_type<>'system' AND read_status<>2`,
now.UnixMilli(), cookieID, chatID); err != nil {
return err
}
_, err := s.DB.ExecContext(ctx, `UPDATE chat_sessions SET unread_count=0,updated_at=?
WHERE cookie_id=? AND chat_id=? AND EXISTS(SELECT 1 FROM cookies c WHERE c.id=chat_sessions.cookie_id AND c.user_id=?)`,
time.Now().UTC().Unix(), cookieID, chatID, userID)
now.Unix(), cookieID, chatID, userID)
return err
}
// CountUnreadUserMessages returns the message-level unread count used by the
// UI badge. It deliberately excludes official/system messages and relies on
// read_status rather than the conversation summary counter.
func (s *ChatStore) CountUnreadUserMessages(ctx context.Context, cookieID, chatID string) (int, error) {
var count int
err := s.DB.QueryRowContext(ctx, `SELECT COUNT(*) FROM chat_messages
WHERE cookie_id=? AND chat_id=? AND direction='incoming' AND message_type<>'system' AND read_status<>2`, cookieID, chatID).Scan(&count)
return count, err
}
func (s *ChatStore) UpdateMessageStatus(ctx context.Context, cookieID, key, status string) (*ChatMessage, error) {
if _, err := s.DB.ExecContext(ctx, `UPDATE chat_messages SET status=? WHERE cookie_id=? AND message_key=?`, status, cookieID, key); err != nil {
return nil, err
}
return s.GetMessageByKey(ctx, cookieID, key)
}
func (s *ChatStore) MarkMessageRead(ctx context.Context, cookieID, key string, readAt int64) (*ChatMessage, error) {
if readAt <= 0 {
readAt = time.Now().UTC().UnixMilli()
}
if _, err := s.DB.ExecContext(ctx, `UPDATE chat_messages SET read_status=2,read_at=? WHERE cookie_id=? AND message_key=? AND direction='outgoing'`, readAt, cookieID, key); err != nil {
return nil, err
}
return s.GetMessageByKey(ctx, cookieID, key)
}
func (s *ChatStore) MarkLatestOutgoingRead(ctx context.Context, cookieID, chatID string, readAt int64) (*ChatMessage, error) {
if readAt <= 0 {
readAt = time.Now().UTC().UnixMilli()
}
var key string
err := s.DB.QueryRowContext(ctx, `SELECT message_key FROM chat_messages WHERE cookie_id=? AND chat_id=? AND direction='outgoing' AND status='sent' AND read_status<>2 ORDER BY sent_at DESC,id DESC LIMIT 1`, cookieID, chatID).Scan(&key)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
}
if err != nil {
return nil, err
}
return s.MarkMessageRead(ctx, cookieID, key, readAt)
}
+3 -2
View File
@@ -128,8 +128,9 @@ func TestLatestMigrationsDownUpSQLite(t *testing.T) {
t.Fatalf("goose dialect: %v", err)
}
goose.SetBaseFS(migrationsFS)
// 依次回滚最新版本到 13,再整体升级。
for i := 0; i < 15; i++ {
// 依次回滚最新版本到 13,再整体升级。00029/00030 是聊天已读
// 状态迁移,因此需要额外回滚两次才能到达原有的 migration 14 检查点。
for i := 0; i < 17; i++ {
if err := goose.Down(d, "migrations/sqlite"); err != nil {
t.Fatalf("down migration #%d: %v", i+1, err)
}
@@ -0,0 +1,6 @@
-- +goose Up
ALTER TABLE chat_messages ADD COLUMN read_status INT NOT NULL DEFAULT 0;
ALTER TABLE chat_messages ADD COLUMN read_at BIGINT NOT NULL DEFAULT 0;
-- +goose Down
ALTER TABLE chat_messages DROP COLUMN read_at;
ALTER TABLE chat_messages DROP COLUMN read_status;
@@ -0,0 +1,6 @@
-- +goose Up
UPDATE chat_messages SET read_status=2, read_at=CASE WHEN read_at=0 THEN UNIX_TIMESTAMP(CURRENT_TIMESTAMP) * 1000 ELSE read_at END
WHERE direction='incoming' AND read_status=0;
-- +goose Down
-- No safe reverse: the previous schema had no per-message read boundary.
@@ -0,0 +1,6 @@
-- +goose Up
ALTER TABLE chat_messages ADD COLUMN read_status INTEGER NOT NULL DEFAULT 0;
ALTER TABLE chat_messages ADD COLUMN read_at BIGINT NOT NULL DEFAULT 0;
-- +goose Down
ALTER TABLE chat_messages DROP COLUMN read_at;
ALTER TABLE chat_messages DROP COLUMN read_status;
@@ -0,0 +1,6 @@
-- +goose Up
UPDATE chat_messages SET read_status=2, read_at=CASE WHEN read_at=0 THEN (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000)::BIGINT ELSE read_at END
WHERE direction='incoming' AND read_status=0;
-- +goose Down
-- No safe reverse: the previous schema had no per-message read boundary.
@@ -0,0 +1,6 @@
-- +goose Up
ALTER TABLE chat_messages ADD COLUMN read_status INTEGER NOT NULL DEFAULT 0;
ALTER TABLE chat_messages ADD COLUMN read_at INTEGER NOT NULL DEFAULT 0;
-- +goose Down
ALTER TABLE chat_messages DROP COLUMN read_at;
ALTER TABLE chat_messages DROP COLUMN read_status;
@@ -0,0 +1,8 @@
-- +goose Up
-- Messages already stored before message-level read tracking was introduced
-- are history snapshots, not newly received unread messages.
UPDATE chat_messages SET read_status=2, read_at=CASE WHEN read_at=0 THEN strftime('%s','now') * 1000 ELSE read_at END
WHERE direction='incoming' AND read_status=0;
-- +goose Down
-- No safe reverse: the previous schema had no per-message read boundary.
+1 -1
View File
@@ -771,7 +771,7 @@ func TestMultiDB_LatestMigrationsDownUp(t *testing.T) {
t.Fatalf("set goose dialect: %v", err)
}
goose.SetBaseFS(migrationsFS)
for i := 0; i < 15; i++ {
for i := 0; i < 17; i++ {
if err := goose.Down(tg.store.DB, "migrations/"+subdir); err != nil {
t.Fatalf("migration down #%d: %v", i+1, err)
}