diff --git a/src/main/java/io/github/doocs/im/core/Audit.java b/src/main/java/io/github/doocs/im/core/Audit.java index e7305aa..4769dc9 100644 --- a/src/main/java/io/github/doocs/im/core/Audit.java +++ b/src/main/java/io/github/doocs/im/core/Audit.java @@ -1,8 +1,24 @@ package io.github.doocs.im.core; import io.github.doocs.im.ImClient; -import io.github.doocs.im.model.request.*; -import io.github.doocs.im.model.response.*; +import io.github.doocs.im.model.request.AddCloudAuditKeywordsRequest; +import io.github.doocs.im.model.request.AddLocalAuditKeywordsRequest; +import io.github.doocs.im.model.request.BatchContentModerationRequest; +import io.github.doocs.im.model.request.ContentModerationRequest; +import io.github.doocs.im.model.request.DeleteCloudAuditKeywordsRequest; +import io.github.doocs.im.model.request.DeleteLocalAuditKeywordsRequest; +import io.github.doocs.im.model.request.GetCloudAuditKeyWordsIdsRequest; +import io.github.doocs.im.model.request.GetCloudAuditKeyWordsRequest; +import io.github.doocs.im.model.request.GetLocalAuditKeywordsRequest; +import io.github.doocs.im.model.response.AddCloudAuditKeywordsResult; +import io.github.doocs.im.model.response.AddLocalAuditKeywordsResult; +import io.github.doocs.im.model.response.BatchContentModerationResult; +import io.github.doocs.im.model.response.ContentModerationResult; +import io.github.doocs.im.model.response.DeleteCloudAuditKeywordsResult; +import io.github.doocs.im.model.response.DeleteLocalAuditKeywordsResult; +import io.github.doocs.im.model.response.GetCloudAuditKeywordsIdsResult; +import io.github.doocs.im.model.response.GetCloudAuditKeywordsResult; +import io.github.doocs.im.model.response.GetLocalAuditKeywordsResult; import io.github.doocs.im.util.HttpUtil; import java.io.IOException; @@ -31,6 +47,7 @@ public class Audit { * 云端审核 */ public static final String CONTENT_MODERATION_COMMAND = "content_moderation"; + public static final String BATCH_CONTENT_MODERATION_COMMAND = "batch_content_moderation"; public static final String GET_CLOUD_AUDIT_KEYWORDS_IDS_COMMAND = "get_cloud_audit_keywords_ids"; public static final String GET_CLOUD_AUDIT_KEYWORDS_COMMAND = "get_cloud_audit_keywords"; public static final String ADD_CLOUD_AUDIT_KEYWORDS_COMMAND = "add_cloud_audit_keywords"; @@ -110,6 +127,23 @@ public ContentModerationResult contentModeration(ContentModerationRequest conten return HttpUtil.post(url, contentModerationRequest, ContentModerationResult.class, imClient.getConfig()); } + /** + * 云端审核批量审核 + * + * @param batchContentModerationRequest 请求参数 + * @return 结果 + * @throws IOException 异常 + */ + public BatchContentModerationResult batchContentModeration(BatchContentModerationRequest batchContentModerationRequest) throws IOException { + String url = imClient.getUrl(SERVICE_NAME, BATCH_CONTENT_MODERATION_COMMAND); + return HttpUtil.post(url, batchContentModerationRequest, BatchContentModerationResult.class, imClient.getConfig()); + } + + public BatchContentModerationResult batchContentModeration(BatchContentModerationRequest batchContentModerationRequest, long random) throws IOException { + String url = imClient.getUrl(SERVICE_NAME, BATCH_CONTENT_MODERATION_COMMAND, random); + return HttpUtil.post(url, batchContentModerationRequest, BatchContentModerationResult.class, imClient.getConfig()); + } + /** * 云端审核获取词库列表 * diff --git a/src/main/java/io/github/doocs/im/model/request/AuditContentItem.java b/src/main/java/io/github/doocs/im/model/request/AuditContentItem.java new file mode 100644 index 0000000..204df38 --- /dev/null +++ b/src/main/java/io/github/doocs/im/model/request/AuditContentItem.java @@ -0,0 +1,111 @@ +package io.github.doocs.im.model.request; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.io.Serializable; + +/** + * 云端审核主动批量审核内容项 + * + * @author bingo + * @since 2023/10/31 10:25 + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +public class AuditContentItem implements Serializable { + private static final long serialVersionUID = -4862840041361119218L; + + /** + * 必填 + * 内容 ID,数组里各个成员,不能重复的 ContentId。 + */ + @JsonProperty("ContentId") + private Integer contentId; + + /** + * 必填 + * 送审类型,批量审核接口里,限制取值:Text/Image,音视频送审请参见 主动审核接口 + * {@link io.github.doocs.im.constant.AuditContentType} + */ + @JsonProperty("ContentType") + private String contentType; + + /** + * 必填 + * 送审内容,最大限制8KB,当审核文件时,填对应 URL。其中图片审核最大不超过5MB。 + */ + @JsonProperty("Content") + private String content; + + public AuditContentItem() { + } + + public AuditContentItem(Integer contentId, String contentType, String content) { + this.contentId = contentId; + this.contentType = contentType; + this.content = content; + } + + private AuditContentItem(Builder builder) { + this.contentId = builder.contentId; + this.contentType = builder.contentType; + this.content = builder.content; + } + + public static Builder builder() { + return new Builder(); + } + + public Integer getContentId() { + return contentId; + } + + public void setContentId(Integer contentId) { + this.contentId = contentId; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + + public static final class Builder { + private Integer contentId; + private String contentType; + private String content; + + private Builder() { + } + + public AuditContentItem build() { + return new AuditContentItem(this); + } + + public Builder contentId(Integer contentId) { + this.contentId = contentId; + return this; + } + + public Builder contentType(String contentType) { + this.contentType = contentType; + return this; + } + + public Builder content(String content) { + this.content = content; + return this; + } + } +} diff --git a/src/main/java/io/github/doocs/im/model/request/BatchContentModerationRequest.java b/src/main/java/io/github/doocs/im/model/request/BatchContentModerationRequest.java new file mode 100644 index 0000000..69bae0a --- /dev/null +++ b/src/main/java/io/github/doocs/im/model/request/BatchContentModerationRequest.java @@ -0,0 +1,87 @@ +package io.github.doocs.im.model.request; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.io.Serializable; +import java.util.List; + +/** + * 云端审核主动批量审核-请求参数 + * + * @author bingo + * @since 2023/10/31 10:25 + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +public class BatchContentModerationRequest extends GenericRequest implements Serializable { + private static final long serialVersionUID = 1971966887350107478L; + + /** + * 表明送审策略,取值:C2C/Group/UserInfo/GroupInfo/GroupMemberInfo/RelationChain + * {@link io.github.doocs.im.constant.AuditNameType} + */ + @JsonProperty("AuditName") + private String auditName; + + /** + * 批量送审数组,最多支持10个内容批量送审。 + */ + @JsonProperty("Contents") + private List contents; + + public BatchContentModerationRequest() { + } + + public BatchContentModerationRequest(String auditName, List contents) { + this.auditName = auditName; + this.contents = contents; + } + + private BatchContentModerationRequest(Builder builder) { + this.auditName = builder.auditName; + this.contents = builder.contents; + } + + public static Builder builder() { + return new Builder(); + } + + public String getAuditName() { + return auditName; + } + + public void setAuditName(String auditName) { + this.auditName = auditName; + } + + public List getContents() { + return contents; + } + + public void setContents(List contents) { + this.contents = contents; + } + + + public static final class Builder { + private String auditName; + private List contents; + + private Builder() { + } + + public BatchContentModerationRequest build() { + return new BatchContentModerationRequest(this); + } + + public Builder auditName(String auditName) { + this.auditName = auditName; + return this; + } + + public Builder contents(List contents) { + this.contents = contents; + return this; + } + } +} diff --git a/src/main/java/io/github/doocs/im/model/request/ContentModerationRequest.java b/src/main/java/io/github/doocs/im/model/request/ContentModerationRequest.java index a152f31..60dfcaa 100644 --- a/src/main/java/io/github/doocs/im/model/request/ContentModerationRequest.java +++ b/src/main/java/io/github/doocs/im/model/request/ContentModerationRequest.java @@ -17,6 +17,7 @@ public class ContentModerationRequest extends GenericRequest implements Serializ /** * 表明送审策略 + * {@link io.github.doocs.im.constant.AuditNameType} */ @JsonProperty("AuditName") private String auditName; diff --git a/src/main/java/io/github/doocs/im/model/response/BatchContentModerationResult.java b/src/main/java/io/github/doocs/im/model/response/BatchContentModerationResult.java new file mode 100644 index 0000000..f33d17f --- /dev/null +++ b/src/main/java/io/github/doocs/im/model/response/BatchContentModerationResult.java @@ -0,0 +1,37 @@ +package io.github.doocs.im.model.response; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.io.Serializable; +import java.util.List; + +/** + * 云端审核主动批量审核结果 + * + * @author bingo + * @since 2023/10/31 10:25 + */ +public class BatchContentModerationResult extends GenericResult implements Serializable { + private static final long serialVersionUID = 2158637502606683308L; + + @JsonProperty("AuditResults") + private List auditResults; + + public List getAuditResults() { + return auditResults; + } + + public void setAuditResults(List auditResults) { + this.auditResults = auditResults; + } + + @Override + public String toString() { + return "BatchContentModerationResult{" + + "auditResults=" + auditResults + + ", actionStatus='" + actionStatus + '\'' + + ", errorInfo='" + errorInfo + '\'' + + ", errorCode=" + errorCode + + '}'; + } +} diff --git a/src/main/java/io/github/doocs/im/model/response/BatchContentModerationResultItem.java b/src/main/java/io/github/doocs/im/model/response/BatchContentModerationResultItem.java new file mode 100644 index 0000000..8588fca --- /dev/null +++ b/src/main/java/io/github/doocs/im/model/response/BatchContentModerationResultItem.java @@ -0,0 +1,159 @@ +package io.github.doocs.im.model.response; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.io.Serializable; +import java.util.List; + +/** + * 云端审核主动批量审核结果项 + * + * @author bingo + * @since 2023/10/31 10:25 + */ +public class BatchContentModerationResultItem implements Serializable { + private static final long serialVersionUID = -1502718880898835836L; + + /** + * 错误码: + * 0:表示成功 + * 非0:表示失败 + */ + @JsonProperty("ErrorCode") + private Integer errorCode; + + /** + * 错误信息 + */ + @JsonProperty("ErrorInfo") + private String errorInfo; + + /** + * 内容 ID,同步请求里面的 ContentId。 + */ + @JsonProperty("ContentId") + private Integer contentId; + + /** + * 图文审核建议,Pass/Review/Block + */ + @JsonProperty("Result") + private String result; + + /** + * 违规时,命中的关键词,如果模型命中不会有关键词返回。 + */ + @JsonProperty("Keywords") + private List keywords; + + /** + * 图文审核恶意值,0-100,恶意程度与 Score 成正比。 + */ + @JsonProperty("Score") + private Integer score; + + /** + * 送审内容命中的标签 Normal/Polity/Porn/Illegal/Abuse/Terror/Ad/Sexy/Composite + */ + @JsonProperty("Label") + private String label; + + /** + * 命中的二级标签 + */ + @JsonProperty("SubLabel") + private String subLabel; + + /** + * 审核唯一标示 + */ + @JsonProperty("RequestId") + private String requestId; + + public Integer getErrorCode() { + return errorCode; + } + + public void setErrorCode(Integer errorCode) { + this.errorCode = errorCode; + } + + public String getErrorInfo() { + return errorInfo; + } + + public void setErrorInfo(String errorInfo) { + this.errorInfo = errorInfo; + } + + public Integer getContentId() { + return contentId; + } + + public void setContentId(Integer contentId) { + this.contentId = contentId; + } + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } + + public List getKeywords() { + return keywords; + } + + public void setKeywords(List keywords) { + this.keywords = keywords; + } + + public Integer getScore() { + return score; + } + + public void setScore(Integer score) { + this.score = score; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getSubLabel() { + return subLabel; + } + + public void setSubLabel(String subLabel) { + this.subLabel = subLabel; + } + + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } + + @Override + public String toString() { + return "BatchContentModerationResultItem{" + + "errorCode=" + errorCode + + ", errorInfo='" + errorInfo + '\'' + + ", contentId=" + contentId + + ", result='" + result + '\'' + + ", keywords=" + keywords + + ", score=" + score + + ", label='" + label + '\'' + + ", subLabel='" + subLabel + '\'' + + ", requestId='" + requestId + '\'' + + '}'; + } +} diff --git a/src/main/java/io/github/doocs/im/model/response/ContentModerationResult.java b/src/main/java/io/github/doocs/im/model/response/ContentModerationResult.java index eed8ff5..194540c 100644 --- a/src/main/java/io/github/doocs/im/model/response/ContentModerationResult.java +++ b/src/main/java/io/github/doocs/im/model/response/ContentModerationResult.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.io.Serializable; +import java.util.List; /** * 云端审核主动审核-结果 @@ -37,6 +38,12 @@ public class ContentModerationResult extends GenericResult implements Serializab @JsonProperty("Label") private String label; + /** + * 违规时,命中的关键词,如果模型命中不会有关键词返回。 + */ + @JsonProperty("Keywords") + private List keywords; + public String getRequestId() { return requestId; } @@ -69,6 +76,14 @@ public void setLabel(String label) { this.label = label; } + public List getKeywords() { + return keywords; + } + + public void setKeywords(List keywords) { + this.keywords = keywords; + } + @Override public String toString() { return "ContentModerationResult{" + @@ -76,6 +91,7 @@ public String toString() { ", result='" + result + '\'' + ", score=" + score + ", label='" + label + '\'' + + ", keywords=" + keywords + ", actionStatus='" + actionStatus + '\'' + ", errorInfo='" + errorInfo + '\'' + ", errorCode=" + errorCode + diff --git a/src/test/java/io/github/doocs/im/core/AuditTest.java b/src/test/java/io/github/doocs/im/core/AuditTest.java index 9996f07..840e9e7 100644 --- a/src/test/java/io/github/doocs/im/core/AuditTest.java +++ b/src/test/java/io/github/doocs/im/core/AuditTest.java @@ -5,6 +5,7 @@ import io.github.doocs.im.constant.*; import io.github.doocs.im.model.request.*; import io.github.doocs.im.model.response.*; +import io.github.doocs.im.util.JsonUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -80,6 +81,25 @@ void testContentModeration() throws IOException { Assertions.assertEquals(ErrorCode.SUCCESS.getCode(), result.getErrorCode()); } + @Test + void testBatchContentModeration() throws IOException { + BatchContentModerationRequest request = new BatchContentModerationRequest(); + request.setAuditName(AuditNameType.GROUP); + AuditContentItem item1 = new AuditContentItem(); + item1.setContent("f*ck uuu"); + item1.setContentId(323245334); + item1.setContentType(AuditContentType.TEXT); + AuditContentItem item2 = AuditContentItem.builder() + .contentId(435545) + .contentType(AuditContentType.TEXT) + .content("cnm").build(); + request.setContents(Arrays.asList(item1, item2)); + System.out.println(JsonUtil.obj2Str(request)); + BatchContentModerationResult result = client.audit.batchContentModeration(request); + System.out.println(result); + Assertions.assertEquals(ErrorCode.SUCCESS.getCode(), result.getErrorCode()); + } + @Test void testGetCloudAuditKeywordsIds() throws IOException { GetCloudAuditKeyWordsIdsRequest request = GetCloudAuditKeyWordsIdsRequest.builder()