Files
AskaEth 093f38d986
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
docs(comments): 注释中文化 · internal/services/channelbridge(纯替换)
- 只翻注释/文档字符串:剥掉注释后代码行逐字节不变(机械校验只剩允许的行尾注释形态)
- 术语对齐既有译法(owner→主人、persona→角色、engage→参与度判定、quiet hours→安静时段、backoff→退避)
- 复核:go build 绿 + channelbridge 包测试绿;gofmt 干净
2026-10-04 13:33:06 +08:00

73 lines
2.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package channelbridge
import (
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
)
// 本文件承载按账号的允许/拒绝黑白名单(design/16 §4.6
// §6),在入站流水线的最前端执行,并包含名单匹配所依赖的
// 配置读取函数。
// blacklisted 对非主人的入站消息应用按账号的允许/拒绝黑白名单
// (design/16 §4.6 §6)。群消息按 GroupID
// 匹配 allowGroups/denyGroups;私聊消息按发送者用户
// id 匹配 allowUsers/denyUsers。空的允许名单表示「全部允许」(仅
// 拒绝名单生效);非空允许名单即成为白名单。主人
// 永不被拉黑(调用方对主人消息跳过本函数)。
func (b *Bridge) blacklisted(msg adapter.UnifiedMessage, a adapter.PlatformAdapter) (bool, string) {
if a == nil {
return false, ""
}
allowGroups, denyGroups, allowUsers, denyUsers := channelLists(a.Config())
if msg.IsGroup() {
if sliceContains(denyGroups, msg.GroupID) {
return true, "group_denied"
}
if len(allowGroups) > 0 && !sliceContains(allowGroups, msg.GroupID) {
return true, "group_not_allowed"
}
return false, ""
}
uid := firstNonEmptyStr(msg.SenderID, msg.Sender.UserID)
if sliceContains(denyUsers, uid) {
return true, "user_denied"
}
if len(allowUsers) > 0 && !sliceContains(allowUsers, uid) {
return true, "user_not_allowed"
}
return false, ""
}
// channelLists 从适配器的 Config 快照中读取允许/拒绝字符串列表。
// 列表以普通 []string 值存储(由多账号装配设置);
// 键缺失时得到空列表。
func channelLists(cfg map[string]any) (allowGroups, denyGroups, allowUsers, denyUsers []string) {
if cfg == nil {
return nil, nil, nil, nil
}
return cfgStrings(cfg, "allowGroups"), cfgStrings(cfg, "denyGroups"),
cfgStrings(cfg, "allowUsers"), cfgStrings(cfg, "denyUsers")
}
// cfgStrings 从适配器配置 map 中读取 []string 字段,缺失或
// 类型不符时返回 nil。
func cfgStrings(m map[string]any, key string) []string {
if m == nil {
return nil
}
switch v := m[key].(type) {
case []string:
return v
case []any:
out := make([]string, 0, len(v))
for _, e := range v {
if s, ok := e.(string); ok {
out = append(out, s)
}
}
return out
default:
return nil
}
}