|
|
@@ -46,13 +46,14 @@ import javax.imageio.stream.ImageInputStream;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
-import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.yhlxj.web.wss.MidjourneyServerEndpoint.WSS_SESSION_MAP;
|
|
|
@@ -727,11 +728,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
redisService.set(RedisService.key.MIDJOURNEY_CONVERSATION.getName() + conversation.getTaskId(), conversation,RedisService.key.MIDJOURNEY_CONVERSATION.getTimeout());
|
|
|
|
|
|
TASK_EXECUTOR.execute(()->{
|
|
|
- //WebSocket 直接发送 任务状态
|
|
|
- WssSession wssSession = WSS_SESSION_MAP.get(conversation.getUserId());
|
|
|
- if (wssSession != null) {
|
|
|
- wssSession.sendMessage(WsMessageTypeEnum.SERVER_TASK_INFO, new JSONObject(conversation));
|
|
|
- }
|
|
|
+ handleCallback(conversation);
|
|
|
conversationMapper.update(null, QueryWrapperUtils.buildUpdateWrapper((wrapper)->{
|
|
|
wrapper.eq(MidjourneyUserConversation::getAction,"MODEL").eq(MidjourneyUserConversation::getTaskId, conversation.getTaskId()).set(MidjourneyUserConversation::getAction, conversation.getAction());
|
|
|
}));
|
|
|
@@ -746,6 +743,63 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ public void handleCallback(MidjourneyUserConversation conversation) {
|
|
|
+ String key = RedisService.key.MIDJOURNEY_PROGRESS.getName() + conversation.getTaskId();
|
|
|
+
|
|
|
+ synchronized (key.intern()) {
|
|
|
+ String currentProgressStr = redisService.getStr(key);
|
|
|
+ int currentProgress = currentProgressStr == null ? -1 : Integer.parseInt(currentProgressStr);
|
|
|
+ if (conversation.getProgressNum() > currentProgress) {
|
|
|
+ redisService.set(key, String.valueOf(progress), RedisService.key.MIDJOURNEY_PROGRESS.getTimeout());
|
|
|
+ sendProgressToClient(conversation);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendProgressToClient( MidjourneyUserConversation conversation) {
|
|
|
+ //WebSocket 直接发送 任务状态
|
|
|
+ WssSession wssSession = WSS_SESSION_MAP.get(conversation.getUserId());
|
|
|
+ if (wssSession != null) {
|
|
|
+ conversation.setUserId(null);
|
|
|
+ conversation.setChannelId(null);
|
|
|
+ conversation.setInstanceId(null);
|
|
|
+ if(JSONUtil.isJson(conversation.getProperties())){
|
|
|
+ //cancelComponent
|
|
|
+ //discordChannelId
|
|
|
+ //discordInstanceId
|
|
|
+ //messageContent 这个需要替换中间字符
|
|
|
+ //messageHash
|
|
|
+ //notifyHook
|
|
|
+ JSONObject jsonObject = new JSONObject(conversation.getProperties());
|
|
|
+ jsonObject.remove("cancelComponent");
|
|
|
+ jsonObject.remove("discordChannelId");
|
|
|
+ jsonObject.remove("discordInstanceId");
|
|
|
+ jsonObject.remove("messageHash");
|
|
|
+ jsonObject.remove("notifyHook");
|
|
|
+ jsonObject.remove("nonce");
|
|
|
+ String content = jsonObject.getStr("messageContent");
|
|
|
+ jsonObject.set("messageContent", replaceUserId(content,"mj"));
|
|
|
+ conversation.setProperties(jsonObject.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ wssSession.sendMessage(WsMessageTypeEnum.SERVER_TASK_INFO, new JSONObject(conversation));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String replaceUserId(String messageContent, String newUserId) {
|
|
|
+ // 定义匹配用户ID的正则表达式模式
|
|
|
+ String regex = "<@[^>]+>";
|
|
|
+ Pattern pattern = Pattern.compile(regex);
|
|
|
+ Matcher matcher = pattern.matcher(messageContent);
|
|
|
+
|
|
|
+ // 使用新的用户ID替换第一个匹配的用户ID
|
|
|
+ if (matcher.find()) {
|
|
|
+ messageContent = matcher.replaceFirst("<@" + newUserId + ">");
|
|
|
+ }
|
|
|
+
|
|
|
+ return messageContent;
|
|
|
+ }
|
|
|
+
|
|
|
@Data
|
|
|
public class ImgSize{
|
|
|
/**
|