refactor: reuse global aliyun moderation config

This commit is contained in:
richjohnrambo
2026-09-17 00:25:04 +08:00
parent 4ed54ca392
commit 9ed8aaf142
6 changed files with 147 additions and 55 deletions
@@ -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
}