Files
tamabox/internal/shared/blockword.go
T
2026-09-08 10:10:44 +08:00

27 lines
930 B
Go

package shared
import "strings"
// BlockWordHit 检查文本是否命中箱主设置的屏蔽词表(逗号分隔),返回首个命中的词。
//
// 屏蔽词语义为「箱主防骚扰词表」:提问、私聊(对方发给我的消息)与投票提交的
// 开放文本都应经过同一份词表过滤,因此把判定收敛到本函数供各入口复用,
// 与账号设置页保存时使用的同一 CSV 格式(半角逗号分隔、已去重、去空白)。
//
// 存储层可能残留历史脏数据(多余空白 / 空项),这里解析时统一 TrimSpace 并跳过空项。
func BlockWordHit(blockWordsCSV, text string) (word string, hit bool) {
if blockWordsCSV == "" || text == "" {
return "", false
}
for _, w := range strings.Split(blockWordsCSV, ",") {
w = strings.TrimSpace(w)
if w == "" {
continue
}
if strings.Contains(text, w) {
return w, true
}
}
return "", false
}