108 lines
3.2 KiB
Go
108 lines
3.2 KiB
Go
// Copyright © 2023 OpenIM open source community. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package chat
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
chatdb "git.imall.cloud/openim/chat/pkg/common/db/table/chat"
|
|
"git.imall.cloud/openim/chat/pkg/common/mctx"
|
|
"git.imall.cloud/openim/chat/pkg/protocol/chat"
|
|
)
|
|
|
|
// ==================== 敏感词检测相关 RPC ====================
|
|
|
|
// GetSensitiveWords 获取敏感词列表
|
|
func (o *chatSvr) GetSensitiveWords(ctx context.Context, req *chat.GetSensitiveWordsReq) (*chat.GetSensitiveWordsResp, error) {
|
|
// 验证用户身份
|
|
if _, _, err := mctx.Check(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 获取启用的敏感词列表
|
|
words, err := o.Database.GetSensitiveWords(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 转换为响应格式(客户端只需要基本信息)
|
|
var wordInfos []*chat.SensitiveWordInfo
|
|
for _, word := range words {
|
|
wordInfos = append(wordInfos, &chat.SensitiveWordInfo{
|
|
Word: word.Word,
|
|
Action: word.Action,
|
|
ReplaceChar: "", // 敏感词本身没有替换字符,使用配置中的默认值
|
|
})
|
|
}
|
|
|
|
// 获取敏感词配置
|
|
config, err := o.Database.GetSensitiveWordConfig(ctx)
|
|
if err != nil {
|
|
// 如果配置不存在,使用默认值
|
|
config = &chatdb.SensitiveWordConfig{
|
|
EnableFilter: true,
|
|
ReplaceChar: "***",
|
|
}
|
|
}
|
|
|
|
return &chat.GetSensitiveWordsResp{
|
|
Words: wordInfos,
|
|
EnableFilter: config.EnableFilter,
|
|
DefaultReplaceChar: config.ReplaceChar,
|
|
}, nil
|
|
}
|
|
|
|
// CheckSensitiveWords 检测敏感词
|
|
func (o *chatSvr) CheckSensitiveWords(ctx context.Context, req *chat.CheckSensitiveWordsReq) (*chat.CheckSensitiveWordsResp, error) {
|
|
// 验证用户身份
|
|
if _, _, err := mctx.Check(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 检测敏感词
|
|
matchedWords, hasSensitive, err := o.Database.CheckSensitiveWords(ctx, req.Content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 如果检测到敏感词,进行内容过滤
|
|
filteredContent := req.Content
|
|
var matchedWordStrings []string
|
|
|
|
if hasSensitive {
|
|
for _, word := range matchedWords {
|
|
matchedWordStrings = append(matchedWordStrings, word.Word)
|
|
|
|
// 根据处理动作进行替换
|
|
if word.Action == 1 { // 替换模式
|
|
// 获取配置中的替换字符
|
|
config, err := o.Database.GetSensitiveWordConfig(ctx)
|
|
replaceChar := "***"
|
|
if err == nil && config.ReplaceChar != "" {
|
|
replaceChar = config.ReplaceChar
|
|
}
|
|
filteredContent = strings.ReplaceAll(filteredContent, word.Word, replaceChar)
|
|
}
|
|
}
|
|
}
|
|
|
|
return &chat.CheckSensitiveWordsResp{
|
|
HasSensitive: hasSensitive,
|
|
FilteredContent: filteredContent,
|
|
MatchedWords: matchedWordStrings,
|
|
}, nil
|
|
}
|