refactor: reuse global aliyun moderation config
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
api "hotgo/api/admin/imconfiginternal"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/immoderation"
|
||||
"hotgo/internal/library/imtenant"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/service"
|
||||
@@ -112,29 +113,36 @@ func (c *cConfig) ModerationProvider(ctx context.Context, req *api.ModerationPro
|
||||
}
|
||||
var row struct {
|
||||
Provider string `json:"provider"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Enabled int `json:"enabled"`
|
||||
ConfigJSON string `json:"config_json"`
|
||||
SecretJSON string `json:"secret_json"`
|
||||
}
|
||||
if err = imtenant.RegistryDB().Model("im_external_config").Safe().Ctx(ctx).
|
||||
Fields("provider", "endpoint", "enabled", "CAST(config_json AS CHAR) config_json", "CAST(secret_json AS CHAR) secret_json").
|
||||
Where("tenant_id", tenant.Id).Where("service", consts.ImExternalServiceModeration).Scan(&row); err != nil {
|
||||
record, err := imtenant.RegistryDB().Model("im_external_config").Safe().Ctx(ctx).
|
||||
Fields("provider", "enabled", "CAST(config_json AS CHAR) config_json").
|
||||
Where("tenant_id", tenant.Id).Where("service", consts.ImExternalServiceModeration).One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "获取内容审核服务配置失败")
|
||||
}
|
||||
if !record.IsEmpty() {
|
||||
if err = record.Struct(&row); err != nil {
|
||||
return nil, gerror.Wrap(err, "解析内容审核服务配置失败")
|
||||
}
|
||||
}
|
||||
globalConfig, err := immoderation.LoadAliyunGlobalConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config := map[string]any{}
|
||||
secret := map[string]any{}
|
||||
_ = json.Unmarshal([]byte(row.ConfigJSON), &config)
|
||||
_ = json.Unmarshal([]byte(row.SecretJSON), &secret)
|
||||
res = &api.ModerationProviderRes{
|
||||
Provider: row.Provider, Endpoint: row.Endpoint, Enabled: row.Enabled == 1,
|
||||
RegionID: stringValue(config, "region_id", "cn-shanghai"),
|
||||
Provider: stringValue(map[string]any{"provider": row.Provider}, "provider", "aliyun_green"),
|
||||
Endpoint: globalConfig.Endpoint, Enabled: row.Enabled == 1,
|
||||
RegionID: globalConfig.RegionID,
|
||||
NicknameService: stringValue(config, "nickname_service", "nickname_detection_pro"),
|
||||
ChatService: stringValue(config, "chat_service", "chat_detection_pro"),
|
||||
TimeoutMS: intValue(config, "timeout_ms", 1500),
|
||||
FailureMode: moderationFailureMode(stringValue(config, "failure_mode", "allow")),
|
||||
AccessKeyID: stringValue(secret, "access_key_id", ""),
|
||||
AccessKeySecret: stringValue(secret, "access_key_secret", ""),
|
||||
AccessKeyID: globalConfig.AccessKeyID,
|
||||
AccessKeySecret: globalConfig.AccessKeySecret,
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package immoderation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model/entity"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
const (
|
||||
AliyunConfigGroup = "im_third_party"
|
||||
AliyunAccessKeyIDKey = "aliyun_access_key_id"
|
||||
AliyunAccessKeySecretKey = "aliyun_access_key_secret"
|
||||
AliyunGreenRegionIDKey = "aliyun_green_region_id"
|
||||
AliyunGreenEndpointKey = "aliyun_green_endpoint"
|
||||
DefaultAliyunGreenRegionID = "cn-shanghai"
|
||||
DefaultAliyunGreenEndpoint = "green-cip.cn-shanghai.aliyuncs.com"
|
||||
)
|
||||
|
||||
type AliyunGlobalConfig struct {
|
||||
AccessKeyID string
|
||||
AccessKeySecret string
|
||||
RegionID string
|
||||
Endpoint string
|
||||
}
|
||||
|
||||
func LoadAliyunGlobalConfig(ctx context.Context) (config AliyunGlobalConfig, err error) {
|
||||
columns := dao.SysConfig.Columns()
|
||||
keys := []string{
|
||||
AliyunAccessKeyIDKey,
|
||||
AliyunAccessKeySecretKey,
|
||||
AliyunGreenRegionIDKey,
|
||||
AliyunGreenEndpointKey,
|
||||
}
|
||||
var rows []*entity.SysConfig
|
||||
if err = dao.SysConfig.Ctx(ctx).
|
||||
Fields(columns.Key, columns.Value).
|
||||
Where(columns.Group, AliyunConfigGroup).
|
||||
Where(columns.Status, 1).
|
||||
WhereIn(columns.Key, keys).
|
||||
Scan(&rows); err != nil {
|
||||
return config, gerror.Wrap(err, "获取阿里云内容审核公共配置失败")
|
||||
}
|
||||
values := make(map[string]string, len(rows))
|
||||
for _, row := range rows {
|
||||
values[row.Key] = strings.TrimSpace(row.Value)
|
||||
}
|
||||
return AliyunGlobalConfig{
|
||||
AccessKeyID: values[AliyunAccessKeyIDKey],
|
||||
AccessKeySecret: values[AliyunAccessKeySecretKey],
|
||||
RegionID: firstValue(values[AliyunGreenRegionIDKey], DefaultAliyunGreenRegionID),
|
||||
Endpoint: firstValue(values[AliyunGreenEndpointKey], DefaultAliyunGreenEndpoint),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func firstValue(value, fallback string) string {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fallback
|
||||
}
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
@@ -116,11 +116,9 @@ func (s *sSysImExternalConfig) Update(ctx context.Context, in *sysin.ImExternalC
|
||||
if in.Provider != "aliyun_green" {
|
||||
return gerror.New("内容审核服务商必须为aliyun_green")
|
||||
}
|
||||
if strings.TrimSpace(in.Endpoint) == "" {
|
||||
in.Endpoint = "green-cip.cn-shanghai.aliyuncs.com"
|
||||
}
|
||||
in.Endpoint = ""
|
||||
if in.ConfigJson == nil {
|
||||
in.ConfigJson = gjson.New(map[string]any{"region_id": "cn-shanghai", "nickname_service": "nickname_detection_pro", "chat_service": "chat_detection_pro", "timeout_ms": 1500, "failure_mode": "allow"})
|
||||
in.ConfigJson = gjson.New(map[string]any{"nickname_service": "nickname_detection_pro", "chat_service": "chat_detection_pro", "timeout_ms": 1500, "failure_mode": "allow"})
|
||||
}
|
||||
config := in.ConfigJson.Map()
|
||||
if timeout := gconv.Int(config["timeout_ms"]); timeout < 200 || timeout > 10000 {
|
||||
@@ -129,12 +127,12 @@ func (s *sSysImExternalConfig) Update(ctx context.Context, in *sysin.ImExternalC
|
||||
if mode := gconv.String(config["failure_mode"]); mode != "allow" && mode != "block" {
|
||||
return gerror.New("内容审核异常策略不合法")
|
||||
}
|
||||
if in.SecretJson != nil && in.SecretJson.String() != "" && in.SecretJson.String() != "null" && in.SecretJson.String() != "{}" {
|
||||
secret := in.SecretJson.Map()
|
||||
if strings.TrimSpace(gconv.String(secret["access_key_id"])) == "" || strings.TrimSpace(gconv.String(secret["access_key_secret"])) == "" {
|
||||
return gerror.New("AccessKey ID和AccessKey Secret必须同时填写")
|
||||
}
|
||||
}
|
||||
in.ConfigJson = gjson.New(map[string]any{
|
||||
"nickname_service": firstExternalValue(gconv.String(config["nickname_service"]), "nickname_detection_pro"),
|
||||
"chat_service": firstExternalValue(gconv.String(config["chat_service"]), "chat_detection_pro"),
|
||||
"timeout_ms": gconv.Int(config["timeout_ms"]),
|
||||
"failure_mode": gconv.String(config["failure_mode"]),
|
||||
})
|
||||
}
|
||||
configJson := "{}"
|
||||
if in.ConfigJson != nil && in.ConfigJson.String() != "" && in.ConfigJson.String() != "null" {
|
||||
@@ -151,6 +149,9 @@ func (s *sSysImExternalConfig) Update(ctx context.Context, in *sysin.ImExternalC
|
||||
if in.SecretJson != nil && in.SecretJson.String() != "" && in.SecretJson.String() != "null" && in.SecretJson.String() != "{}" {
|
||||
data["secret_json"] = in.SecretJson.String()
|
||||
}
|
||||
if in.Service == consts.ImExternalServiceModeration {
|
||||
data["secret_json"] = "{}"
|
||||
}
|
||||
if _, err = imtenant.RegistryDB().Model("im_external_config").Safe().Ctx(ctx).
|
||||
Where("tenant_id", tenant.Id).Where("service", in.Service).Data(data).Update(); err != nil {
|
||||
return gerror.Wrap(err, "更新IM外部服务配置失败,请稍后重试!")
|
||||
@@ -211,16 +212,18 @@ func (s *sSysImExternalConfig) checkModeration(ctx context.Context, row gdb.Reco
|
||||
return consts.ImExternalConfigStatusUnconfigured, "内容审核服务商必须为aliyun_green"
|
||||
}
|
||||
config := map[string]any{}
|
||||
secret := map[string]any{}
|
||||
_ = json.Unmarshal([]byte(row["config_json"].String()), &config)
|
||||
_ = json.Unmarshal([]byte(row["secret_json"].String()), &secret)
|
||||
globalConfig, err := immoderation.LoadAliyunGlobalConfig(ctx)
|
||||
if err != nil {
|
||||
return consts.ImExternalConfigStatusUnconfigured, sanitizeExternalError(err)
|
||||
}
|
||||
timeoutMS := gconv.Int(config["timeout_ms"])
|
||||
if timeoutMS <= 0 {
|
||||
timeoutMS = 1500
|
||||
}
|
||||
check, err := immoderation.CheckAliyunText(ctx, immoderation.AliyunConfig{
|
||||
AccessKeyID: gconv.String(secret["access_key_id"]), AccessKeySecret: gconv.String(secret["access_key_secret"]),
|
||||
RegionID: firstExternalValue(gconv.String(config["region_id"]), "cn-shanghai"), Endpoint: row["endpoint"].String(),
|
||||
AccessKeyID: globalConfig.AccessKeyID, AccessKeySecret: globalConfig.AccessKeySecret,
|
||||
RegionID: globalConfig.RegionID, Endpoint: globalConfig.Endpoint,
|
||||
Service: firstExternalValue(gconv.String(config["nickname_service"]), "nickname_detection_pro"), Timeout: time.Duration(timeoutMS) * time.Millisecond,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user