145 lines
6.3 KiB
Go
145 lines
6.3 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/openimsdk/tools/db/pagination"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
// 敏感词相关结构体定义
|
|
type SensitiveWord struct {
|
|
ID string `bson:"_id" json:"id"` // 主键ID
|
|
Word string `bson:"word" json:"word"` // 敏感词内容
|
|
Level int32 `bson:"level" json:"level"` // 敏感词级别 1:低 2:中 3:高
|
|
Type int32 `bson:"type" json:"type"` // 敏感词类型 1:政治 2:色情 3:暴力 4:广告 5:其他
|
|
Action int32 `bson:"action" json:"action"` // 处理动作 1:替换为*** 2:拦截不发
|
|
Status int32 `bson:"status" json:"status"` // 状态 1:启用 0:禁用
|
|
Creator string `bson:"creator" json:"creator"` // 创建者
|
|
Updater string `bson:"updater" json:"updater"` // 更新者
|
|
CreateTime time.Time `bson:"create_time" json:"create_time"` // 创建时间
|
|
UpdateTime time.Time `bson:"update_time" json:"update_time"` // 更新时间
|
|
Remark string `bson:"remark" json:"remark"` // 备注
|
|
}
|
|
|
|
func (SensitiveWord) TableName() string {
|
|
return "sensitive_words"
|
|
}
|
|
|
|
type SensitiveWordLog struct {
|
|
ID primitive.ObjectID `bson:"_id,omitempty"`
|
|
UserID string `bson:"user_id"` // 触发用户ID
|
|
GroupID string `bson:"group_id"` // 触发群组ID (如果是在群聊中)
|
|
Content string `bson:"content"` // 原始消息内容
|
|
MatchedWords []string `bson:"matched_words"` // 匹配到的敏感词
|
|
Action int32 `bson:"action"` // 采取的动作
|
|
ProcessedText string `bson:"processed_text"` // 处理后的文本 (如果被替换)
|
|
CreateTime time.Time `bson:"create_time"`
|
|
}
|
|
|
|
func (SensitiveWordLog) TableName() string {
|
|
return "sensitive_word_logs"
|
|
}
|
|
|
|
type SensitiveWordGroup struct {
|
|
ID primitive.ObjectID `bson:"_id,omitempty"`
|
|
Name string `bson:"name"` // 分组名称
|
|
Remark string `bson:"remark"` // 备注
|
|
CreateTime time.Time `bson:"create_time"`
|
|
UpdateTime time.Time `bson:"update_time"`
|
|
}
|
|
|
|
func (SensitiveWordGroup) TableName() string {
|
|
return "sensitive_word_groups"
|
|
}
|
|
|
|
type SensitiveWordConfig struct {
|
|
ID string `bson:"_id" json:"id"` // 主键ID
|
|
EnableFilter bool `bson:"enable_filter" json:"enable_filter"` // 是否启用过滤
|
|
FilterMode int32 `bson:"filter_mode" json:"filter_mode"` // 过滤模式
|
|
ReplaceChar string `bson:"replace_char" json:"replace_char"` // 替换字符,默认***
|
|
WhitelistUsers []string `bson:"whitelist_users" json:"whitelist_users"` // 白名单用户
|
|
WhitelistGroups []string `bson:"whitelist_groups" json:"whitelist_groups"` // 白名单群组
|
|
LogEnabled bool `bson:"log_enabled" json:"log_enabled"` // 是否记录日志
|
|
AutoApprove bool `bson:"auto_approve" json:"auto_approve"` // 是否自动审核
|
|
UpdateTime time.Time `bson:"update_time" json:"update_time"` // 更新时间
|
|
}
|
|
|
|
func (SensitiveWordConfig) TableName() string {
|
|
return "sensitive_word_configs"
|
|
}
|
|
|
|
// 敏感词级别常量
|
|
const (
|
|
SensitiveLevelLow = 1 // 低级别
|
|
SensitiveLevelMedium = 2 // 中级别
|
|
SensitiveLevelHigh = 3 // 高级别
|
|
)
|
|
|
|
// 敏感词类型常量
|
|
const (
|
|
SensitiveTypePolitical = 1 // 政治
|
|
SensitiveTypePorn = 2 // 色情
|
|
SensitiveTypeViolence = 3 // 暴力
|
|
SensitiveTypeAd = 4 // 广告
|
|
SensitiveTypeOther = 5 // 其他
|
|
)
|
|
|
|
// 处理动作常量
|
|
const (
|
|
SensitiveActionReplace = 1 // 替换为***
|
|
SensitiveActionBlock = 2 // 拦截不发
|
|
)
|
|
|
|
// 状态常量
|
|
const (
|
|
SensitiveStatusDisabled = 0 // 禁用
|
|
SensitiveStatusEnabled = 1 // 启用
|
|
)
|
|
|
|
type SensitiveWordInterface interface {
|
|
// 敏感词管理
|
|
CreateSensitiveWord(ctx context.Context, word *SensitiveWord) error
|
|
UpdateSensitiveWord(ctx context.Context, id string, data map[string]any) error
|
|
DeleteSensitiveWord(ctx context.Context, ids []string) error
|
|
GetSensitiveWord(ctx context.Context, id string) (*SensitiveWord, error)
|
|
SearchSensitiveWords(ctx context.Context, keyword string, action int32, status int32, pagination pagination.Pagination) (int64, []*SensitiveWord, error)
|
|
GetAllSensitiveWords(ctx context.Context) ([]*SensitiveWord, error)
|
|
GetEnabledSensitiveWords(ctx context.Context) ([]*SensitiveWord, error)
|
|
|
|
// 敏感词检测
|
|
CheckSensitiveWords(ctx context.Context, content string) ([]*SensitiveWord, error)
|
|
FilterContent(ctx context.Context, content string) (string, []*SensitiveWord, error)
|
|
|
|
// 敏感词日志
|
|
CreateSensitiveWordLog(ctx context.Context, log *SensitiveWordLog) error
|
|
GetSensitiveWordLogs(ctx context.Context, userID string, groupID string, pagination pagination.Pagination) (int64, []*SensitiveWordLog, error)
|
|
DeleteSensitiveWordLogs(ctx context.Context, ids []string) error
|
|
|
|
// 敏感词分组管理
|
|
CreateSensitiveWordGroup(ctx context.Context, group *SensitiveWordGroup) error
|
|
UpdateSensitiveWordGroup(ctx context.Context, id string, data map[string]any) error
|
|
DeleteSensitiveWordGroup(ctx context.Context, ids []string) error
|
|
GetSensitiveWordGroup(ctx context.Context, id string) (*SensitiveWordGroup, error)
|
|
GetAllSensitiveWordGroups(ctx context.Context) ([]*SensitiveWordGroup, error)
|
|
|
|
// 敏感词配置管理
|
|
GetSensitiveWordConfig(ctx context.Context) (*SensitiveWordConfig, error)
|
|
UpdateSensitiveWordConfig(ctx context.Context, config *SensitiveWordConfig) error
|
|
IsFilterEnabled(ctx context.Context) (bool, error)
|
|
GetFilterMode(ctx context.Context) (int32, error)
|
|
GetReplaceChar(ctx context.Context) (string, error)
|
|
IsUserInWhitelist(ctx context.Context, userID string) (bool, error)
|
|
IsGroupInWhitelist(ctx context.Context, groupID string) (bool, error)
|
|
|
|
// 批量操作
|
|
BatchCreateSensitiveWords(ctx context.Context, words []*SensitiveWord) error
|
|
BatchUpdateSensitiveWords(ctx context.Context, updates map[string]map[string]any) error
|
|
BatchDeleteSensitiveWords(ctx context.Context, ids []string) error
|
|
|
|
// 统计信息
|
|
GetSensitiveWordStats(ctx context.Context) (map[string]int64, error)
|
|
GetSensitiveWordLogStats(ctx context.Context, startTime, endTime time.Time) (map[string]int64, error)
|
|
}
|