fix: blocklist提前到消息处理最前端 — 拦截消息不浪费任何资源

- 移到 logging/enrichment/routing 之前
- 被拦截消息只记一条 [blocked] 日志,不调ai-core
- 时间戳/群名/昵称等富化处理不再对blocked消息执行

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-06-23 19:53:23 +08:00
parent 4733f9c02a
commit 1eb22f5d06
+26 -23
View File
@@ -71,7 +71,32 @@ func main() {
// Set message handler with logging. // Set message handler with logging.
router.SetMessageHandler(func(msg *bridge.UnifiedMessage) (*bridge.UnifiedResponse, error) { router.SetMessageHandler(func(msg *bridge.UnifiedMessage) (*bridge.UnifiedResponse, error) {
// Log incoming. // Routing decisions.
isAdmin := mapper.IsAdmin(msg.Platform, msg.OriginalSenderUID)
// Blocklist check — MUST run first, before logging/enrichment,
// to avoid wasting resources on blocked users/groups.
if blocked := blocklistStore.IsBlocked(msg.ChannelType, msg.ChannelID, msg.OriginalSenderUID, isAdmin); blocked {
msgLogger.Log(logging.LogEntry{
Timestamp: time.Now(),
Direction: "incoming",
Platform: msg.Platform,
ChannelID: msg.ChannelID,
SenderID: msg.OriginalSenderUID,
SenderName: msg.OriginalSenderName,
GroupName: msg.GroupName,
Content: "[blocked]",
Success: true,
})
return &bridge.UnifiedResponse{
Messages: []bridge.ResponseMessage{
{DisplayType: "silent", Content: "", FormatMode: "plain"},
},
Platform: msg.Platform,
}, nil
}
// Log incoming (only non-blocked messages).
msgLogger.Log(logging.LogEntry{ msgLogger.Log(logging.LogEntry{
Timestamp: time.Now(), Timestamp: time.Now(),
Direction: "incoming", Direction: "incoming",
@@ -85,9 +110,6 @@ func main() {
MessageID: msg.MessageID, MessageID: msg.MessageID,
Success: true, Success: true,
}) })
// Routing decisions.
isAdmin := mapper.IsAdmin(msg.Platform, msg.OriginalSenderUID)
adminNick := cfg.AdminNickname adminNick := cfg.AdminNickname
if isAdmin { if isAdmin {
if id := mapper.ResolveOrNil(msg.Platform, msg.OriginalSenderUID); id != nil && id.Nickname != "" { if id := mapper.ResolveOrNil(msg.Platform, msg.OriginalSenderUID); id != nil && id.Nickname != "" {
@@ -138,25 +160,6 @@ func main() {
msg.Content = fmt.Sprintf("【私聊 %s】%s/%s (%s)\n%s", msg.Platform, adminNick, msg.OriginalSenderName, msg.OriginalSenderUID, msg.Content) msg.Content = fmt.Sprintf("【私聊 %s】%s/%s (%s)\n%s", msg.Platform, adminNick, msg.OriginalSenderName, msg.OriginalSenderUID, msg.Content)
} }
// Blocklist/whitelist check (admin always bypasses).
if blocked := blocklistStore.IsBlocked(msg.ChannelType, msg.ChannelID, msg.OriginalSenderUID, isAdmin); blocked {
msgLogger.Log(logging.LogEntry{
Timestamp: time.Now(),
Direction: "outgoing",
Platform: msg.Platform,
ChannelID: msg.ChannelID,
SenderID: msg.OriginalSenderUID,
SenderName: "Cyrene",
Content: "[blocked]",
Success: true,
})
return &bridge.UnifiedResponse{
Messages: []bridge.ResponseMessage{
{DisplayType: "silent", Content: "", FormatMode: "plain"},
},
Platform: msg.Platform,
}, nil
}
var response *bridge.UnifiedResponse var response *bridge.UnifiedResponse
var routeErr error var routeErr error