fix: make ai task events realtime only
This commit is contained in:
@@ -67,13 +67,14 @@ func executeKafkaWorkbenchEvent(event kafkaWorkbenchEvent) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
notificationEnabled := event.shouldCreateNotification()
|
||||
pushTargets := make(map[types.Uid]push.Recipient)
|
||||
for _, targetID := range event.TargetUserIDs {
|
||||
target := store.EncodeUid(targetID)
|
||||
if target.IsZero() {
|
||||
continue
|
||||
}
|
||||
if target != actor && event.Title != "" {
|
||||
if notificationEnabled && target != actor && event.Title != "" {
|
||||
action := event.notificationAction()
|
||||
if err = createIMSystemNotification(event.TenantID, target, stableIMEventID(event.EventID, "notification", target.UserId()),
|
||||
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
|
||||
}
|
||||
|
||||
func (e kafkaWorkbenchEvent) shouldCreateNotification() bool {
|
||||
switch e.Action {
|
||||
case "workbench.ai.task.updated":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (e kafkaWorkbenchEvent) notificationAction() types.KVMap {
|
||||
action := types.KVMap{
|
||||
"module": e.Module,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user