sync messgae to web

This commit is contained in:
SAM2O2O 2020-10-12 23:53:28 +08:00
parent eea2f92046
commit a8895d5c99
2 changed files with 158 additions and 66 deletions

View File

@ -1,17 +1,17 @@
/**
/**
* Copyright 2018-2028 Akaxin Group
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the License.
*/
package com.windchat.im.message.executor;
@ -36,79 +36,68 @@ import com.windchat.im.message.sync.handler.SyncFinishHandler;
import com.windchat.im.message.sync.handler.SyncGroupMessageHandler;
import com.windchat.im.message.sync.handler.SyncMsgStatusHandler;
import com.windchat.im.message.sync.handler.SyncU2MessageHandler;
import com.windchat.im.message.user2.handler.U2MessageImageHandler;
import com.windchat.im.message.user2.handler.U2MessageImageSecretHandler;
import com.windchat.im.message.user2.handler.U2MessageNoticeHandler;
import com.windchat.im.message.user2.handler.U2MessageTextHandler;
import com.windchat.im.message.user2.handler.U2MessageTextSecretHandler;
import com.windchat.im.message.user2.handler.U2MessageVoiceHandler;
import com.windchat.im.message.user2.handler.U2MessageVoiceSecretHandler;
import com.windchat.im.message.user2.handler.U2MessageWebHandler;
import com.windchat.im.message.user2.handler.U2MessageWebNoticeHandler;
import com.windchat.im.message.user2.handler.UserDetectionHandler;
import com.windchat.im.message.user2.handler.UserPsnHandler;
import com.windchat.im.message.user2.handler.UserPushHandler;
import com.windchat.im.message.user2.handler.*;
import com.windchat.im.message.notice.handler.NoticeHandler;
import com.windchat.im.message.notice.handler.NoticePushHandler;
/**
*
* @author Sam{@link an.guoyue254@gmail.com}
* @since 2018-02-05 11:48:39
*/
public class MessageExecutor {
private static AbstracteExecutor<Command, Boolean> executor = new SimpleExecutor<Command, Boolean>();
private static AbstracteExecutor<Command, Boolean> executor = new SimpleExecutor<Command, Boolean>();
static {
AbstractHandlerChain<Command, Boolean> u2MessageChain = new SimpleHandlerChain<Command>();
u2MessageChain.addHandler(new UserDetectionHandler());
u2MessageChain.addHandler(new U2MessageTextHandler());
u2MessageChain.addHandler(new U2MessageTextSecretHandler());
u2MessageChain.addHandler(new U2MessageImageHandler());
u2MessageChain.addHandler(new U2MessageImageSecretHandler());
u2MessageChain.addHandler(new U2MessageVoiceHandler());
u2MessageChain.addHandler(new U2MessageVoiceSecretHandler());
u2MessageChain.addHandler(new U2MessageNoticeHandler());
u2MessageChain.addHandler(new U2MessageWebHandler());
u2MessageChain.addHandler(new U2MessageWebNoticeHandler());
u2MessageChain.addHandler(new UserPsnHandler());
u2MessageChain.addHandler(new UserPushHandler());
static {
AbstractHandlerChain<Command, Boolean> u2MessageChain = new SimpleHandlerChain<Command>();
u2MessageChain.addHandler(new UserDetectionHandler());
u2MessageChain.addHandler(new U2MessageTextHandler());
u2MessageChain.addHandler(new U2MessageTextSecretHandler());
u2MessageChain.addHandler(new U2MessageImageHandler());
u2MessageChain.addHandler(new U2MessageImageSecretHandler());
u2MessageChain.addHandler(new U2MessageVoiceHandler());
u2MessageChain.addHandler(new U2MessageVoiceSecretHandler());
u2MessageChain.addHandler(new U2MessageNoticeHandler());
u2MessageChain.addHandler(new U2MessageWebHandler());
u2MessageChain.addHandler(new U2MessageWebNoticeHandler());
u2MessageChain.addHandler(new U2MessageWebSyncHandler());
u2MessageChain.addHandler(new UserPsnHandler());
u2MessageChain.addHandler(new UserPushHandler());
AbstractHandlerChain<Command, Boolean> groupMessageChain = new SimpleHandlerChain<Command>();
groupMessageChain.addHandler(new GroupDetectionHandler());
groupMessageChain.addHandler(new GroupMessageTextHandler());
groupMessageChain.addHandler(new GroupMessageImageHandler());
groupMessageChain.addHandler(new GroupMessageVoiceHandler());
groupMessageChain.addHandler(new GroupMessageNoticeHandler());
groupMessageChain.addHandler(new GroupMessageWebHandler());
groupMessageChain.addHandler(new GroupMessageWebNoticeHandler());
groupMessageChain.addHandler(new GroupPsnHandler());
groupMessageChain.addHandler(new GroupPushHandler());
AbstractHandlerChain<Command, Boolean> groupMessageChain = new SimpleHandlerChain<Command>();
groupMessageChain.addHandler(new GroupDetectionHandler());
groupMessageChain.addHandler(new GroupMessageTextHandler());
groupMessageChain.addHandler(new GroupMessageImageHandler());
groupMessageChain.addHandler(new GroupMessageVoiceHandler());
groupMessageChain.addHandler(new GroupMessageNoticeHandler());
groupMessageChain.addHandler(new GroupMessageWebHandler());
groupMessageChain.addHandler(new GroupMessageWebNoticeHandler());
groupMessageChain.addHandler(new GroupPsnHandler());
groupMessageChain.addHandler(new GroupPushHandler());
AbstractHandlerChain<Command, Boolean> syncMessageChain = new SimpleHandlerChain<Command>();
syncMessageChain.addHandler(new SyncU2MessageHandler());
syncMessageChain.addHandler(new SyncGroupMessageHandler());
AbstractHandlerChain<Command, Boolean> syncMessageChain = new SimpleHandlerChain<Command>();
syncMessageChain.addHandler(new SyncU2MessageHandler());
syncMessageChain.addHandler(new SyncGroupMessageHandler());
AbstractHandlerChain<Command, Boolean> noticeChain = new SimpleHandlerChain<Command>();
noticeChain.addHandler(new NoticeHandler());
noticeChain.addHandler(new NoticePushHandler());
AbstractHandlerChain<Command, Boolean> noticeChain = new SimpleHandlerChain<Command>();
noticeChain.addHandler(new NoticeHandler());
noticeChain.addHandler(new NoticePushHandler());
// "im.cts.message.u2"
executor.addChain(RequestAction.IM_CTS_MESSAGE_U2.getName(), u2MessageChain);
// "im.cts.message.group"
executor.addChain(RequestAction.IM_CTS_MESSAGE_GROUP.getName(), groupMessageChain);
// "im.sync.message"
executor.addChain(RequestAction.IM_SYNC_MESSAGE.getName(), syncMessageChain);
// "im.sync.finish"
executor.addChain(RequestAction.IM_SYNC_FINISH.getName(), new SyncFinishHandler());
// "im.sync.msgStatus"
executor.addChain(RequestAction.IM_SYNC_MSGSTATUS.getName(), new SyncMsgStatusHandler());
// "im.stc.notice"
executor.addChain(RequestAction.IM_STC_NOTICE.getName(), noticeChain);
}
// "im.cts.message.u2"
executor.addChain(RequestAction.IM_CTS_MESSAGE_U2.getName(), u2MessageChain);
// "im.cts.message.group"
executor.addChain(RequestAction.IM_CTS_MESSAGE_GROUP.getName(), groupMessageChain);
// "im.sync.message"
executor.addChain(RequestAction.IM_SYNC_MESSAGE.getName(), syncMessageChain);
// "im.sync.finish"
executor.addChain(RequestAction.IM_SYNC_FINISH.getName(), new SyncFinishHandler());
// "im.sync.msgStatus"
executor.addChain(RequestAction.IM_SYNC_MSGSTATUS.getName(), new SyncMsgStatusHandler());
// "im.stc.notice"
executor.addChain(RequestAction.IM_STC_NOTICE.getName(), noticeChain);
}
public static AbstracteExecutor<Command, Boolean> getExecutor() {
return executor;
}
public static AbstracteExecutor<Command, Boolean> getExecutor() {
return executor;
}
}

View File

@ -0,0 +1,103 @@
/**
* Copyright 2018-2028 Akaxin Group
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.windchat.im.message.user2.handler;
import com.akaxin.proto.core.CoreProto;
import com.akaxin.proto.site.ImCtsMessageProto;
import com.windchat.common.command.Command;
import com.windchat.common.crypto.AESCrypto;
import com.windchat.common.http.ZalyHttpClient;
import com.windchat.common.logs.LogUtils;
import com.windchat.common.utils.GsonUtils;
import com.windchat.im.message.bean.WebBean;
import com.windchat.im.storage.api.IMessageDao;
import com.windchat.im.storage.bean.U2MessageBean;
import com.windchat.im.storage.service.MessageDaoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* sync message to web
*
* @author an.guoyue254@gmail.com
* @since 2020-10-12 22:45:20
*/
public class U2MessageWebSyncHandler extends AbstractU2Handler<Command> {
private static final Logger logger = LoggerFactory.getLogger(U2MessageWebSyncHandler.class);
private static final int programId = 100;
private static final String programSecret = "gXDayjtSHtTk33Tfo4ILruYmYblO7nJe";
public Boolean handle(Command command) {
try {
switch (command.getMsgType()) {
case CoreProto.MsgType.TEXT_VALUE: {
ImCtsMessageProto.ImCtsMessageRequest request = ImCtsMessageProto.ImCtsMessageRequest
.parseFrom(command.getParams());
final String siteUserId = command.getSiteUserId();
final String proxySiteUserId = request.getText().getSiteUserId();
final String siteFriendId = request.getText().getSiteFriendId();
final String msgId = request.getText().getMsgId();
final String textBody = request.getText().getText().toString(StandardCharsets.UTF_8);
// 构建Json格式消息体
String action = "duckchat.message.send";
Map<String, Object> data = new HashMap<String, Object>(3) {{
put("action", action);
put("body", new HashMap<String, Object>(2) {{
put("@type", "type.googleapis.com/plugin.DuckChatMessageSendRequest");
put("message", new HashMap<String, Object>(6) {{
put("msgId", msgId);
put("fromUserId", siteUserId);
put("toUserId", siteFriendId);
put("type", "MessageText");
put("roomType", "MessageRoomU2");
put("text", new HashMap<String, Object>(1) {{
put("body", textBody);
}});
}});
}});
put("timeServer", System.currentTimeMillis());
}};
// 构建HTTP URL
String requestUrl = "http://localhost:8000//?action=" + action + "&body_format=json&miniProgramId=" + programId;
// AES 加密
byte[] encrptData = AESCrypto.encrypt(programSecret.getBytes(), GsonUtils.toJson(data).getBytes());
// Http Post请求
ZalyHttpClient.getInstance().postBytes(requestUrl, encrptData);
}
break;
default:
break;
}
return true;
} catch (Exception e) {
LogUtils.requestErrorLog(logger, command, this.getClass(), e);
}
return false;
}
}