[Change] BotCommandProcess 将isNoSafe方法公开;

[Change] MiraiMessageEvent 将UploadImage方法公开;
[Add] MiraiMessageEvent 增加MessageSource枚举类;
This commit is contained in:
LamGC 2020-04-19 00:31:24 +08:00
parent dbfed874c0
commit b328def8f9
2 changed files with 52 additions and 9 deletions

View File

@ -554,7 +554,15 @@ public class BotCommandProcess {
标签....标签支持搜索吧 标签....标签支持搜索吧
*/ */
private static boolean isNoSafe(int illustId, Properties settingProp, boolean returnRaw) throws IOException { /**
* 检查指定作品是否为r18
* @param illustId 作品Id
* @param settingProp 配置项
* @param returnRaw 是否返回原始值
* @return 如果为true, 则不为全年龄
* @throws IOException 获取数据时发生异常时抛出
*/
public static boolean isNoSafe(int illustId, Properties settingProp, boolean returnRaw) throws IOException {
boolean rawValue = getIllustInfo(illustId, false).getAsJsonArray("tags").contains(new JsonPrimitive("R-18")); boolean rawValue = getIllustInfo(illustId, false).getAsJsonArray("tags").contains(new JsonPrimitive("R-18"));
return returnRaw || settingProp == null ? rawValue : rawValue && !settingProp.getProperty("image.allowR18", "false").equalsIgnoreCase("true"); return returnRaw || settingProp == null ? rawValue : rawValue && !settingProp.getProperty("image.allowR18", "false").equalsIgnoreCase("true");
} }

View File

@ -19,13 +19,14 @@ import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class MiraiMessageEvent extends MessageEvent { public class MiraiMessageEvent extends MessageEvent {
private final ContactMessage messageObject; private final ContactMessage messageObject;
private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode())); private final static Logger log = LoggerFactory.getLogger(MiraiMessageEvent.class.getSimpleName());
private final static CacheStore<String> imageIdCache = new HotDataCacheStore<>( private final static CacheStore<String> imageIdCache = new HotDataCacheStore<>(
new StringRedisCacheStore(BotEventHandler.redisServer, "mirai.imageId"), new StringRedisCacheStore(BotEventHandler.redisServer, "mirai.imageId"),
new LocalHashCacheStore<>(), new LocalHashCacheStore<>(),
@ -111,6 +112,17 @@ public class MiraiMessageEvent extends MessageEvent {
} }
private Image uploadImage(BotCode code) { private Image uploadImage(BotCode code) {
return uploadImage(getMessageSource(this.messageObject), code, this::uploadImage0);
}
/**
* 存在缓存的上传图片.
* @param sourceType 消息来源
* @param code 图片BotCode
* @param imageUploader 图片上传器
* @return Image对象
*/
public static Image uploadImage(MessageSource sourceType, BotCode code, Function<File, Image> imageUploader) {
log.debug("传入BotCode信息:\n{}", code); log.debug("传入BotCode信息:\n{}", code);
String absolutePath = code.getParameter("absolutePath"); String absolutePath = code.getParameter("absolutePath");
if(Strings.isNullOrEmpty(absolutePath)) { if(Strings.isNullOrEmpty(absolutePath)) {
@ -120,14 +132,14 @@ public class MiraiMessageEvent extends MessageEvent {
String imageName = code.getParameter("imageName"); String imageName = code.getParameter("imageName");
if(!Strings.isNullOrEmpty(imageName)) { if(!Strings.isNullOrEmpty(imageName)) {
Image image = null; Image image = null;
imageName = (getMessageSource() + "." + imageName).intern(); imageName = (sourceType + "." + imageName).intern();
if(!imageIdCache.exists(imageName) || if(!imageIdCache.exists(imageName) ||
Strings.nullToEmpty(code.getParameter("updateCache")).equalsIgnoreCase("true")) { Strings.nullToEmpty(code.getParameter("updateCache")).equalsIgnoreCase("true")) {
synchronized (imageName) { synchronized (imageName) {
if(!imageIdCache.exists(imageName) || if(!imageIdCache.exists(imageName) ||
Strings.nullToEmpty(code.getParameter("updateCache")) .equalsIgnoreCase("true")) { Strings.nullToEmpty(code.getParameter("updateCache")) .equalsIgnoreCase("true")) {
log.debug("imageName [{}] 缓存失效或强制更新, 正在更新缓存...", imageName); log.debug("imageName [{}] 缓存失效或强制更新, 正在更新缓存...", imageName);
image = uploadImage0(new File(absolutePath)); image = imageUploader.apply(new File(absolutePath));
if(Objects.isNull(image)) { if(Objects.isNull(image)) {
return null; return null;
} }
@ -159,7 +171,7 @@ public class MiraiMessageEvent extends MessageEvent {
return image; return image;
} else { } else {
log.debug("未设置imageName, 无法使用缓存."); log.debug("未设置imageName, 无法使用缓存.");
return uploadImage0(new File(absolutePath)); return imageUploader.apply(new File(absolutePath));
} }
} }
@ -174,15 +186,38 @@ public class MiraiMessageEvent extends MessageEvent {
} }
} }
private String getMessageSource() { public static MessageSource getMessageSource(ContactMessage messageObject) {
if(messageObject instanceof FriendMessage) { if(messageObject instanceof FriendMessage) {
return "Private"; return MessageSource.Private;
} else if(messageObject instanceof GroupMessage) { } else if(messageObject instanceof GroupMessage) {
return "Group"; return MessageSource.Group;
} else { } else {
log.warn("未知的ContactMessage类型: " + messageObject.toString()); log.warn("未知的ContactMessage类型: " + messageObject.toString());
return "Unknown"; return MessageSource.Unknown;
} }
} }
/**
* 消息来源
*/
public enum MessageSource {
/**
* 私聊消息
*/
Private,
/**
* 群组消息
*/
Group,
/**
* 讨论组消息
* @deprecated 已被QQ取消
*/
Discuss,
/**
* 未知来源
*/
Unknown
}
} }