fix: make ai task events realtime only

This commit is contained in:
richjohnrambo
2026-09-30 11:37:49 +08:00
parent d75c6fa0b4
commit dcb131dcea
2 changed files with 56 additions and 1 deletions
+11 -1
View File
@@ -67,13 +67,14 @@ func executeKafkaWorkbenchEvent(event kafkaWorkbenchEvent) error {
if err != nil { if err != nil {
return err return err
} }
notificationEnabled := event.shouldCreateNotification()
pushTargets := make(map[types.Uid]push.Recipient) pushTargets := make(map[types.Uid]push.Recipient)
for _, targetID := range event.TargetUserIDs { for _, targetID := range event.TargetUserIDs {
target := store.EncodeUid(targetID) target := store.EncodeUid(targetID)
if target.IsZero() { if target.IsZero() {
continue continue
} }
if target != actor && event.Title != "" { if notificationEnabled && target != actor && event.Title != "" {
action := event.notificationAction() action := event.notificationAction()
if err = createIMSystemNotification(event.TenantID, target, stableIMEventID(event.EventID, "notification", target.UserId()), if err = createIMSystemNotification(event.TenantID, target, stableIMEventID(event.EventID, "notification", target.UserId()),
event.Action, event.Title, event.Summary, action); err != nil && err != types.ErrDuplicate { event.Action, event.Title, event.Summary, action); err != nil && err != types.ErrDuplicate {
@@ -133,6 +134,15 @@ func (e kafkaWorkbenchEvent) realtimePayload() (map[string]any, error) {
return payload, nil return payload, nil
} }
func (e kafkaWorkbenchEvent) shouldCreateNotification() bool {
switch e.Action {
case "workbench.ai.task.updated":
return false
default:
return true
}
}
func (e kafkaWorkbenchEvent) notificationAction() types.KVMap { func (e kafkaWorkbenchEvent) notificationAction() types.KVMap {
action := types.KVMap{ action := types.KVMap{
"module": e.Module, "module": e.Module,
+45
View File
@@ -0,0 +1,45 @@
package main
import (
"testing"
"github.com/tinode/chat/server/store"
)
func TestExecuteKafkaWorkbenchAITaskEventSkipsNotification(t *testing.T) {
oldSecurity := store.Security
security := &testUserSecurityStore{}
store.Security = security
t.Cleanup(func() {
store.Security = oldSecurity
})
event := kafkaWorkbenchEvent{
EventID: "wbe_ai_1",
TenantID: 7,
ActorUserID: 1,
Module: "ai",
Action: "workbench.ai.task.updated",
ResourceID: "aitask_1",
TargetUserIDs: []int64{2},
Title: "AI 任务完成",
Summary: "表格分析已完成",
Data: map[string]any{
"task_id": "aitask_1",
"feature": "smart_table",
"status": "succeeded",
"result_type": "record",
"record_id": "airec_1",
},
}
if event.shouldCreateNotification() {
t.Fatal("ai task event must be realtime-only")
}
err := executeKafkaWorkbenchEvent(event)
if err != nil {
t.Fatalf("executeKafkaWorkbenchEvent returned error: %v", err)
}
if len(security.notifications) != 0 {
t.Fatalf("ai task event should not create offline notification: %#v", security.notifications)
}
}