Commit d8714078 authored by 刘基明's avatar 刘基明

内容检验fix

parent e38879ab
...@@ -6,6 +6,7 @@ import com.tanpu.biz.common.enums.RelTypeEnum; ...@@ -6,6 +6,7 @@ import com.tanpu.biz.common.enums.RelTypeEnum;
import com.tanpu.biz.common.enums.community.CollectionTypeEnum; import com.tanpu.biz.common.enums.community.CollectionTypeEnum;
import com.tanpu.biz.common.enums.community.ReportTypeEnum; import com.tanpu.biz.common.enums.community.ReportTypeEnum;
import com.tanpu.common.api.CommonResp; import com.tanpu.common.api.CommonResp;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.common.exception.BizException; import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.JsonUtil; import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.qo.ESThemeQo; import com.tanpu.community.api.beans.qo.ESThemeQo;
...@@ -18,7 +19,10 @@ import com.tanpu.community.api.beans.resp.CreateThemeResp; ...@@ -18,7 +19,10 @@ import com.tanpu.community.api.beans.resp.CreateThemeResp;
import com.tanpu.community.api.beans.resp.ThemeFullSearchResp; import com.tanpu.community.api.beans.resp.ThemeFullSearchResp;
import com.tanpu.community.api.beans.resp.ThemeListResp; import com.tanpu.community.api.beans.resp.ThemeListResp;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp; import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.enums.*; import com.tanpu.community.api.enums.BlockTypeEnum;
import com.tanpu.community.api.enums.OperationTypeEnum;
import com.tanpu.community.api.enums.ThemeListTypeEnum;
import com.tanpu.community.api.enums.ThemeTypeEnum;
import com.tanpu.community.cache.RedisCache; import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.*; import com.tanpu.community.dao.entity.community.*;
import com.tanpu.community.feign.fatools.FeignClientForFatools; import com.tanpu.community.feign.fatools.FeignClientForFatools;
...@@ -39,7 +43,6 @@ import org.springframework.stereotype.Service; ...@@ -39,7 +43,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -144,12 +147,9 @@ public class ThemeManager { ...@@ -144,12 +147,9 @@ public class ThemeManager {
ThemeEntity themeEntity = new ThemeEntity(); ThemeEntity themeEntity = new ThemeEntity();
BeanUtils.copyProperties(req, themeEntity); BeanUtils.copyProperties(req, themeEntity);
themeEntity.setAuthorId(userId); themeEntity.setAuthorId(userId);
themeEntity.setContent(JsonUtil.toJson(req.getContent()));
// 腾讯云敏感词校验 // 腾讯云敏感词校验
// TODO 图片校验 checkContent(req);
checkContent(themeEntity.getContent()); themeEntity.setContent(JsonUtil.toJson(req.getContent()));
//附件校验 //附件校验
checkAttachment(req.getContent()); checkAttachment(req.getContent());
...@@ -493,22 +493,28 @@ public class ThemeManager { ...@@ -493,22 +493,28 @@ public class ThemeManager {
/** /**
* 腾讯云-内容检测 * 腾讯云-内容检测
* *
* @param content * @param req
*/ */
private void checkContent(String content) { private void checkContent(CreateThemeReq req) {
StringBuilder sb = new StringBuilder();
for (ThemeContentReq themeContentReq : req.getContent()) {
if (RelTypeEnum.TEXT.type.equals(themeContentReq.getType())){
sb.append(themeContentReq.getValue());
}
}
String content = sb.toString();
// 腾讯云接口最多支持5000文字校验,超过5000执行2次 // 腾讯云接口最多支持5000文字校验,超过5000执行2次
// 检查内容是否涉黄违法 // 检查内容是否涉黄违法
CommonResp commonResp = TencentcloudUtils.textModeration(content.length() > 5000 ? content.substring(0, 5000) : content); boolean b = TencentcloudUtils.textModeration(content.length() > 5000 ? content.substring(0, 5000) : content);
if (!commonResp.isSuccess()) { if (!b) {
throw new BizException("内容校验失败,请检查内容后重新发送"); throw new BizException(ErrorCodeConstant.CONTENT_ILLEGAL);
} }
if (content.length() > 5000) { if (content.length() > 5000) {
CommonResp commonResp2 = TencentcloudUtils.textModeration(content.substring(5000, content.length())); boolean result = TencentcloudUtils.textModeration(content.substring(5000, content.length()));
if (!commonResp2.isSuccess()) { if (!result) {
throw new BizException("内容校验失败,请检查内容后重新发送"); throw new BizException(ErrorCodeConstant.CONTENT_ILLEGAL);
} }
} }
return;
} }
......
...@@ -66,10 +66,7 @@ public class ConvertUtil { ...@@ -66,10 +66,7 @@ public class ConvertUtil {
// 抽取文本内容 // 抽取文本内容
List<ThemeContentQo> themeContentQos = JsonUtil.toBean(entity.getContent(), new TypeReference<List<ThemeContentQo>>() {}); List<ThemeContentQo> themeContentQos = JsonUtil.toBean(entity.getContent(), new TypeReference<List<ThemeContentQo>>() {});
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
themeContentQos.stream().filter(q -> { themeContentQos.stream().filter(q -> RelTypeEnum.TEXT.type.equals(q.getType())).forEach(q -> {
// todo enum
return q.getType().equals("108");
}).forEach(q -> {
sb.append(q.getValue()); sb.append(q.getValue());
}); });
qo.textContent = sb.toString(); qo.textContent = sb.toString();
......
...@@ -79,7 +79,7 @@ public class TencentcloudUtils { ...@@ -79,7 +79,7 @@ public class TencentcloudUtils {
* @param text * @param text
* @return * @return
*/ */
public static CommonResp textModeration(String text) { public static boolean textModeration(String text) {
TextModerationRequest req = new TextModerationRequest(); TextModerationRequest req = new TextModerationRequest();
req.setContent(Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8))); req.setContent(Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8)));
TextModerationResponse res = null; TextModerationResponse res = null;
...@@ -87,16 +87,16 @@ public class TencentcloudUtils { ...@@ -87,16 +87,16 @@ public class TencentcloudUtils {
res = getTmsClient().TextModeration(req); res = getTmsClient().TextModeration(req);
// suggestion Block 不通过 // suggestion Block 不通过
if (res.getSuggestion().equals("Block")) { if (res.getSuggestion().equals("Block")) {
// TODO: 2021/7/20 return false;
return CommonResp.error();
// return CommonResp.error(CommonResp.CONTENT_ILLEGAL, getTextLabel(res.getLabel(), res.getKeywords())); // return CommonResp.error(CommonResp.CONTENT_ILLEGAL, getTextLabel(res.getLabel(), res.getKeywords()));
} }
} catch (TencentCloudSDKException e) { } catch (TencentCloudSDKException e) {
log.error("调用腾文本内容安全异常"); log.error("调用腾文本内容安全异常");
e.printStackTrace(); e.printStackTrace();
return CommonResp.failed("文本检查异常"); //调用失败时,不影响用户发布主题
return true;
} }
return CommonResp.success(); return true;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment