diff --git a/community-service/pom.xml b/community-service/pom.xml index be4ab1359c189b2162941968e59619b38e6a61af..dac0a9e42a9bdb80ce986b90e712c013660c7b8b 100644 --- a/community-service/pom.xml +++ b/community-service/pom.xml @@ -167,6 +167,13 @@ <scope>test</scope> </dependency> + <!--è…¾è®¯äº‘å†…å®¹æ ¡éªŒ--> + <dependency> + <groupId>com.tencentcloudapi</groupId> + <artifactId>tencentcloud-sdk-java</artifactId> + <version>4.0.11</version> + </dependency> + </dependencies> <build> diff --git a/community-service/src/main/java/com/tanpu/community/manager/CommentManager.java b/community-service/src/main/java/com/tanpu/community/manager/CommentManager.java index dcea160286717a494c8b5a7ee2e78e844f117a7e..d6ac7681b850a430897dd5628dec89039902bbcf 100644 --- a/community-service/src/main/java/com/tanpu/community/manager/CommentManager.java +++ b/community-service/src/main/java/com/tanpu/community/manager/CommentManager.java @@ -79,7 +79,7 @@ public class CommentManager { // List<CommentLv2Qo> commentLv2Qos = ConvertUtil.commentLv2Entity2Qos(CommentLv2Entities); // commentQo.setCommentLv2Qos(commentLv2Qos); } - //æŽ’åº + //排åºï¼šç‚¹èµžé™åº+æ—¶é—´é™åº return commentQos.stream().sorted(Comparator.comparing(CommentQo::getLikeCount,Comparator.reverseOrder()). thenComparing(CommentQo::getUpdateTime,Comparator.reverseOrder())) .collect(Collectors.toList()); diff --git a/community-service/src/main/java/com/tanpu/community/manager/ThemeManager.java b/community-service/src/main/java/com/tanpu/community/manager/ThemeManager.java index 25fa7d67f6f71c87bb8523731a95386c063e93e3..0d76809098f0f0239668b9b8354f5d156a4c5e01 100644 --- a/community-service/src/main/java/com/tanpu/community/manager/ThemeManager.java +++ b/community-service/src/main/java/com/tanpu/community/manager/ThemeManager.java @@ -1,5 +1,7 @@ package com.tanpu.community.manager; +import com.google.common.collect.Sets; +import com.tanpu.common.api.CommonResp; import com.tanpu.common.exception.BizException; import com.tanpu.common.util.JsonUtil; import com.tanpu.community.api.beans.qo.ESThemeQo; @@ -13,9 +15,11 @@ import com.tanpu.community.api.beans.resp.ThemeFullSearchResp; import com.tanpu.community.api.enums.*; import com.tanpu.community.dao.entity.community.*; import com.tanpu.community.dao.entity.user.UserInfoEntity; +import com.tanpu.community.feign.fatools.FeignClientForFatools; import com.tanpu.community.service.*; import com.tanpu.community.service.base.ESService; import com.tanpu.community.util.ConvertUtil; +import com.tanpu.community.util.TencentcloudUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -66,6 +70,9 @@ public class ThemeManager { @Autowired private ESService esService; + @Autowired + private FeignClientForFatools feignClientForFatools; + public ThemeFullSearchResp themeFullSearch(String keyword, Integer pageNo, Integer pageSize, List<String> excludeIds, String userId) { Integer from = (pageNo - 1) * pageSize; ThemeFullSearchResp resp = new ThemeFullSearchResp(); @@ -90,8 +97,19 @@ public class ThemeManager { @Transactional public CreateThemeResp publishTheme(CreateThemeReq req, String userId) { - - //TODO æ•æ„Ÿè¯è¿‡æ»¤ + //ç›´æ’类型åšè½¬æ’检查 + List<ThemeContentReq> contents = req.getContent(); + for (ThemeContentReq content : contents) { + if (content != null && content.getType().equals(RelTypeEnum.LIVE.type)) { + CommonResp<Set<String>> notRelayResp = feignClientForFatools.getNotRelaySet(userId, Sets.newHashSet(content.getValue())); + if (!notRelayResp.isSuccess()) { + throw new BizException("转æ’失败"); + } + if (CollectionUtils.isEmpty(notRelayResp.getData())) { + throw new BizException("9999", "很抱æ‰!您需è¦è´ä¹°æˆ–æŠ¥åæˆåŠŸåŽæ‰å¯ä»¥æ·»åŠ è¿™ä¸ªç›´æ’哦~"); + } + } + } //ä¿å˜ä¸»é¢˜è¡¨ ThemeEntity themeEntity = new ThemeEntity(); @@ -99,6 +117,8 @@ public class ThemeManager { themeEntity.setAuthorId(userId); themeEntity.setContent(JsonUtil.toJson(req.getContent())); + //TODO æ•æ„Ÿè¯è¿‡æ»¤ + checkContent(themeEntity.getContent()); if (StringUtils.isEmpty(req.getEditThemeId())) { //新建 @@ -125,6 +145,7 @@ public class ThemeManager { return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build(); } + /** * 返回主题列表 * @@ -138,7 +159,7 @@ public class ThemeManager { // TODO:推è // themeEntities = themeService.selectExcludeUser(userId, req.getLastId(), req.getPageSize()); List<String> recommendThemeIds = rankService.getHotAndNewThemes(100, 100); - themeEntities = themeService.queryByThemeIdsExcludeUser( recommendThemeIds,userId, req.getLastId(), req.getPageSize()); + themeEntities = themeService.queryByThemeIdsExcludeUser(recommendThemeIds, userId, req.getLastId(), req.getPageSize()); } else if (ThemeListTypeEnum.FOLLOW.getCode().equals(req.getType())) { //æ ¹æ®å…³æ³¨åˆ—表查询 @@ -393,4 +414,20 @@ public class ThemeManager { } + private void checkContent(String content) { + // è…¾è®¯äº‘æŽ¥å£æœ€å¤šæ”¯æŒ5000æ–‡å—æ ¡éªŒï¼Œè¶…过5000执行2次 + // æ£€æŸ¥å†…å®¹æ˜¯å¦æ¶‰é»„è¿æ³• + CommonResp commonResp = TencentcloudUtils.textModeration(content.length() > 5000 ? content.substring(0, 5000) : content); + if (!commonResp.isSuccess()) { + throw new BizException("å†…å®¹æ ¡éªŒå¤±è´¥ï¼Œè¯·æ£€æŸ¥å†…å®¹åŽé‡æ–°å‘é€"); + } + if (content.length() > 5000) { + CommonResp commonResp2 = TencentcloudUtils.textModeration(content.substring(5000, content.length())); + if (!commonResp2.isSuccess()) { + throw new BizException("å†…å®¹æ ¡éªŒå¤±è´¥ï¼Œè¯·æ£€æŸ¥å†…å®¹åŽé‡æ–°å‘é€"); + } + } + return ; + } + } diff --git a/community-service/src/main/java/com/tanpu/community/util/TencentcloudUtils.java b/community-service/src/main/java/com/tanpu/community/util/TencentcloudUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..94433efa7f9ba52b81b26a3f9fb9824db7ba73b5 --- /dev/null +++ b/community-service/src/main/java/com/tanpu/community/util/TencentcloudUtils.java @@ -0,0 +1,184 @@ +package com.tanpu.community.util; + +import com.tanpu.common.api.CommonResp; +import com.tanpu.common.constant.ErrorCodeConstant; +import com.tencentcloudapi.cms.v20190321.CmsClient; +import com.tencentcloudapi.cms.v20190321.models.ImageModerationRequest; +import com.tencentcloudapi.cms.v20190321.models.ImageModerationResponse; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.tms.v20200713.TmsClient; +import com.tencentcloudapi.tms.v20200713.models.TextModerationRequest; +import com.tencentcloudapi.tms.v20200713.models.TextModerationResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Base64; + +/** + * 腾讯云工具类 + */ +@Slf4j +@Configuration +public class TencentcloudUtils { + + /** + * 腾讯云的SecretId + */ + private static String secretId; + /** + * 腾讯云的SecretKey + */ + private static String secretKey; + + /** + * 腾讯云地区 + */ + private static String region; + + private static TmsClient tmsClient; + + private static CmsClient cmsClient; + + @Value("${tencent.cloud.secretId}") + public void setSecretId(String secretId) { + TencentcloudUtils.secretId = secretId; + } + + @Value("${tencent.cloud.secretKey}") + public void setSecretKey(String secretKey) { + TencentcloudUtils.secretKey = secretKey; + } + + @Value("${tencent.cloud.region}") + public void setRegion(String region) { + TencentcloudUtils.region = region; + } + + + public static TmsClient getTmsClient() { + if (tmsClient == null) { + tmsClient = new TmsClient(new Credential(secretId, secretKey), region); + } + return tmsClient; + } + + public static CmsClient getCmsClient() { + if (cmsClient == null) { + cmsClient = new CmsClient(new Credential(secretId, secretKey), region); + } + return cmsClient; + } + + /** + * 文本内容安全 + * + * @param text + * @return + */ + public static CommonResp textModeration(String text) { + TextModerationRequest req = new TextModerationRequest(); + req.setContent(Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8))); + TextModerationResponse res = null; + try { + res = getTmsClient().TextModeration(req); + // suggestion Block ä¸é€šè¿‡ + if (res.getSuggestion().equals("Block")) { + // TODO: 2021/7/20 + return CommonResp.error(); +// return CommonResp.error(CommonResp.CONTENT_ILLEGAL, getTextLabel(res.getLabel(), res.getKeywords())); + } + } catch (TencentCloudSDKException e) { + log.error("调用腾文本内容安全异常"); + e.printStackTrace(); + return CommonResp.failed("文本检查异常"); + } + return CommonResp.success(); + } + + /** + * 图片内容检测 + * + * @param imageUrl + * @return + */ + public static CommonResp imageModeration(String imageUrl) { + ImageModerationRequest req = new ImageModerationRequest(); + req.setFileUrl(imageUrl); + ImageModerationResponse res = null; + try { + res = getCmsClient().ImageModeration(req); + if (res.getData() != null && res.getData().getEvilType() != 100) { + return CommonResp.error(ErrorCodeConstant.CONTENT_ILLEGAL, getImageLabel(res.getData().getEvilType())); + } + } catch (TencentCloudSDKException e) { + log.error("调用腾讯图片内容检测异常"); + e.printStackTrace(); + return CommonResp.failed("图片检查异常"); + } + return CommonResp.success(); + } + + + public static void main(String[] args) { + final Base64.Encoder encoder = Base64.getEncoder(); + Credential cred = new Credential("AKIDTjjV2IhK4ZKBm8z5g14vPedNSJuFnTIq", "PaVBZfeQwDVXKr7TZOzM6c9VZNwGJGyA"); + TmsClient client = new TmsClient(cred, "ap-shanghai"); + CmsClient cmsClient = new CmsClient(cred, "ap-shanghai"); + TextModerationRequest req = new TextModerationRequest(); + req.setContent(encoder.encodeToString("å–æ¯’å“".getBytes(StandardCharsets.UTF_8))); + + ImageModerationRequest imgReq = new ImageModerationRequest(); + imgReq.setFileUrl("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.08lr.cn%2Fuploads%2Fallimg%2F170513%2F1-1F513164126.jpg&refer=http%3A%2F%2Fwww.08lr.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617540249&t=2506e9bd61a31ea3bf89d07f4fe49744"); + try { + TextModerationResponse res = client.TextModeration(req); + ImageModerationResponse imgRes = cmsClient.ImageModeration(imgReq); + System.out.println(Arrays.toString(res.getKeywords())); + System.out.println(imgRes.getData()); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public static String getTextLabel(String label, String[] keywords) { + StringBuilder message = new StringBuilder("该内容包å«"); + if (label.equals("Porn")) { + message.append("色情"); + } else if (label.equals("Abuse")) { + message.append("谩骂"); + } else if (label.equals("Ad")) { + message.append("广告"); + } else if (label.equals("Polity")) { + message.append("政治"); + } else if (label.equals("Illegal")) { + message.append("è¿æ³•"); + } + message.append("ç‰æ•感è¯"); + if (keywords != null && keywords.length > 0) { + message.append(" "); + message.append(Arrays.toString(keywords)); + } + return message.toString(); + } + + public static String getImageLabel(Long label) { + StringBuilder message = new StringBuilder("该图片å¯èƒ½åŒ…å«"); + if (label.equals(20002)) { + message.append("色情"); + } else if (label.equals(20007)) { + message.append("谩骂"); + } else if (label.equals(20001) || label.equals(24001)) { + message.append("政治"); + } else if (label.equals(20006)) { + message.append("è¿æ³•"); + } else if (label.equals(20103)) { + message.append("性感"); + } + message.append("æ•æ„Ÿå†…容"); + return message.toString(); + } +} diff --git a/community-service/src/main/resources/application-dev.yml b/community-service/src/main/resources/application-dev.yml index 02678ee6b6f0aa70f4aed7d8a1954c393ba72e27..dc17343ba864f417ac83da3102674c05c76b423c 100644 --- a/community-service/src/main/resources/application-dev.yml +++ b/community-service/src/main/resources/application-dev.yml @@ -1,4 +1,3 @@ - apollo.bootstrap.enabled: false #app.id: tanpu-community @@ -97,4 +96,10 @@ es: host: 42.194.224.208 port: 9200 userName: 1 - userPasswd: 2 \ No newline at end of file + userPasswd: 2 + +tencent: + cloud: + secretId: AKIDTjjV2IhK4ZKBm8z5g14vPedNSJuFnTIq + secretKey: PaVBZfeQwDVXKr7TZOzM6c9VZNwGJGyA + region: ap-shanghai \ No newline at end of file