Commit 547cb7e9 authored by 刘基明's avatar 刘基明

评论排序

parent 36c3d6f5
......@@ -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>
......
......@@ -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());
......
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 ;
}
}
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();
}
}
apollo.bootstrap.enabled: false
#app.id: tanpu-community
......@@ -98,3 +97,9 @@ es:
port: 9200
userName: 1
userPasswd: 2
tencent:
cloud:
secretId: AKIDTjjV2IhK4ZKBm8z5g14vPedNSJuFnTIq
secretKey: PaVBZfeQwDVXKr7TZOzM6c9VZNwGJGyA
region: ap-shanghai
\ No newline at end of file
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