【调整群组事件处理结构】

This commit is contained in:
weizhihong 2023-04-21 10:18:08 +08:00
parent e83d5dfae6
commit e16437b347
4 changed files with 222 additions and 195 deletions

View File

@ -77,26 +77,4 @@ public class VoiceDiscriminateService {
replyParseService.doExec(result, simulation); // 回复执行
correctSourceService.doExec(result, simulation);// 原信息纠错
}
/**
* 处理聊天信息
* @param event 聊天事件信息
*/
@EventListener
public void handleMessage(SimulationConversationGroupMessageEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
SimulationMember member = event.getMember();
List<SimulationMember> memberList = conversationGroup.getMemberList().stream()
.filter(cm -> cm.isRobot() && !Objects.equals(cm.getMember(), member))
.map(ConversationMember::getMember).collect(Collectors.toList());
if (CollectionUtils.isEmpty(memberList)) { // 人员全部为真实人员时直接返回
return;
}
// 增加机器人已读操作
memberList.forEach(m -> conversationGroupManagerService.readConversationGroup(event.getSimulation(), m, conversationGroup.getId()));
// 如果消息来源不为机器人则执行语音识别目前先不做语音识别回复
// if (!event.getMessage().getMember().isRobot()) {
// doAnalysis(event.getSimulation(), conversationGroup.getId(), event.getMessage().getContent(), memberList);
// }
}
}

View File

@ -44,9 +44,6 @@ public class ConversationGroupHandlerService {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Autowired
private StompMessageService stompMessageService;
/**
* 获取所有群组信息
* @param group 仿真ID
@ -90,7 +87,7 @@ public class ConversationGroupHandlerService {
*
* @param simulation 仿真
* @param member 用户
* @param groupId 群组主键
* @param id 群组主键
* @param name 群组名称
*/
public void updateConversationGroupName(Simulation simulation, SimulationMember member, Long id, String name) {
@ -284,143 +281,6 @@ public class ConversationGroupHandlerService {
return ConversationGroupMessageVO.convert2VO(messageList);
}
/**
* 处理创建群组事件
* @param event 事件信息
*/
@EventListener
public void handleCreate(SimulationConversationGroupCreateEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
SimulationMember leader = conversationGroup.getLeader();
ConversationGroupSocketMessageVO messageVO = ConversationGroupSocketMessageVO.MessageType.JOIN.generateMessageVO(conversationGroup, null);
if (!leader.isRobot()) {
doSendMessage(event.getSimulation(), Set.of(leader.getUserId()), messageVO);
}
Set<String> userIds = conversationGroup.getMemberUserIdWithOutLeader();
if (CollectionUtils.isEmpty(userIds)) {
return;
}
// 给非群主的群人员发送消息添加tips信息
messageVO.setMessageTips(String.format("你加入了【%s】", conversationGroup.getName()));
doSendMessage(event.getSimulation(), userIds, messageVO);
}
/**
* 处理更新群组信息事件
* @param event 事件信息
*/
@EventListener
public void handleUpdate(SimulationConversationGroupUpdateEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
Set<String> userIds = conversationGroup.getMemberUserId();
if (CollectionUtils.isEmpty(userIds)) {
return;
}
ConversationGroupSocketMessageVO messageVO = event.getMessageType().generateMessageVO(conversationGroup, null);
doSendMessage(event.getSimulation(), userIds, messageVO);
}
/**
* 处理邀请成员事件
* @param event 事件信息
*/
@EventListener
public void handleInvite(SimulationConversationGroupInviteEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
Set<String> newUserIdSet = new HashSet<>(); // 发送加入信息的用户列表
StringBuilder stringBuilder = new StringBuilder();
event.getNewMemberList().forEach(m -> {
if (!m.isRobot()) {
newUserIdSet.add(m.getUserId());
}
stringBuilder.append(m.getName()).append("");
});
// 通知新成员
if (!CollectionUtils.isEmpty(newUserIdSet)) {
ConversationGroupSocketMessageVO joinMessageVO = ConversationGroupSocketMessageVO.MessageType.JOIN.generateMessageVO(conversationGroup, null);
// 添加tips信息
joinMessageVO.setMessageTips(String.format("你加入了【%s】", conversationGroup.getName()));
doSendMessage(event.getSimulation(), newUserIdSet, joinMessageVO);
}
Set<String> oldUserIdSet = conversationGroup.getMemberUserId(event.getGroupMemberIds());
// 通知老成员
if (!CollectionUtils.isEmpty(oldUserIdSet)) {
ConversationGroupSocketMessageVO updateMessageVO = ConversationGroupSocketMessageVO.MessageType.UPDATE_MEMBER.generateMessageVO(conversationGroup, null);
updateMessageVO.setMessageTips("欢迎【" + stringBuilder.substring(0, stringBuilder.length() - 1) + "】加入本群");
doSendMessage(event.getSimulation(), conversationGroup.getMemberUserId(event.getGroupMemberIds()), updateMessageVO);
}
}
/**
* 群组移除人员事件
*
* @param event 事件信息
*/
@EventListener
public void handleRemove(SimulationConversationGroupRemoveEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
Set<String> removeUserIdSet = event.getRemoveGroupMember().stream().filter(m -> !m.isRobot()).map(SimulationMember::getUserId).collect(Collectors.toSet());
// 通知移除
if (!CollectionUtils.isEmpty(removeUserIdSet)) {
ConversationGroupSocketMessageVO exitMessageVO = ConversationGroupSocketMessageVO.MessageType.EXIT.generateMessageVO(conversationGroup, null);
// 添加tips信息
exitMessageVO.setMessageTips(String.format("你被移出了【%s】", conversationGroup.getName()));
doSendMessage(event.getSimulation(), removeUserIdSet, exitMessageVO);
}
// 更新其他用户的成员列表
Set<String> userIdSet = conversationGroup.getMemberUserId();
if (CollectionUtils.isEmpty(userIdSet)) {
return;
}
ConversationGroupSocketMessageVO updateMessageVO = ConversationGroupSocketMessageVO.MessageType.UPDATE_MEMBER.generateMessageVO(conversationGroup, null);
doSendMessage(event.getSimulation(), userIdSet, updateMessageVO);
}
/**
* 处理退出群组事件
* @param event 事件信息
*/
@EventListener
public void handleExit(SimulationConversationGroupExitEvent event) {
if (event.getMember().isRobot()) {
return;
}
ConversationGroup conversationGroup = event.getConversationGroup();
ConversationGroupSocketMessageVO exitMessageVO = ConversationGroupSocketMessageVO.MessageType.EXIT.generateMessageVO(conversationGroup, null);
doSendMessage(event.getSimulation(), Set.of(event.getMember().getUserId()), exitMessageVO);
}
/**
* 解散群主事件
* @param event 事件信息
*/
@EventListener
public void handleDissolve(SimulationConversationGroupDissolveEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
// 更新其他用户的成员列表
Set<String> userIdSet = conversationGroup.getMemberUserId();
if (CollectionUtils.isEmpty(userIdSet)) {
return;
}
ConversationGroupSocketMessageVO exitMessageVO = ConversationGroupSocketMessageVO.MessageType.EXIT.generateMessageVO(conversationGroup, null);
exitMessageVO.setMessageTips(String.format("【%s】解散了", conversationGroup.getName()));
doSendMessage(event.getSimulation(), userIdSet, exitMessageVO);
}
/**
* 处理消息状态
* @param event
*/
@EventListener
public void handleMessageStatus(SimulationConversationGroupMessageStatusEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
event.getMessageList().stream().filter(m -> !m.getMember().isRobot()).forEach(message -> {
ConversationGroupSocketMessageVO messageStatusVO
= ConversationGroupSocketMessageVO.MessageType.MESSAGE_STATUS.generateMessageVO(conversationGroup, new ConversationGroupMessageVO(message));
doSendMessage(event.getSimulation(), Set.of(message.getMember().getUserId()), messageStatusVO);
});
}
/**
* 对群组操作时基础判断
*
@ -462,31 +322,8 @@ public class ConversationGroupHandlerService {
ConversationGroupMessage message = new ConversationGroupMessage(messageId, member,
simulation.getCorrectSystemTime(), handledContent, recognitionResult.getFilePath());
conversationGroup.addMessage(message);
// 发送消息
ConversationGroupMessageVO messageVO = new ConversationGroupMessageVO(message);
Set<String> userIdSet = conversationGroup.getMemberUserId();
if (!CollectionUtils.isEmpty(userIdSet)) {
doSendMessage(simulation, userIdSet, ConversationGroupSocketMessageVO.MessageType.MESSAGE.generateMessageVO(conversationGroup, messageVO));
}
// 语音识别开始
applicationEventPublisher.publishEvent(new SimulationConversationGroupMessageEvent(this, simulation, conversationGroup, member, message));
return messageVO;
applicationEventPublisher.publishEvent(new SimulationConversationGroupMessageEvent(this, simulation, conversationGroup, message));
return new ConversationGroupMessageVO(message);
}
/**
* 发送消息
*
* @param simulation 仿真
* @param userSet 用户列表
* @param messageVO 消息体
*/
private void doSendMessage(Simulation simulation, Set<String> userSet, ConversationGroupSocketMessageVO messageVO) {
if (CollectionUtils.isEmpty(userSet)) {
return;
}
SocketMessageVO<ConversationGroupSocketMessageVO> chatMessage =
SocketMessageFactory.build(WebSocketMessageType.Simulation_Conversation_Group, simulation.getId(), messageVO);
stompMessageService.sendToUser(userSet, chatMessage);
}
}

View File

@ -0,0 +1,218 @@
package club.joylink.rtss.simulation.cbtc.conversation;
import club.joylink.rtss.simulation.cbtc.Simulation;
import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupMessageVO;
import club.joylink.rtss.simulation.cbtc.data.vo.ConversationGroupSocketMessageVO;
import club.joylink.rtss.simulation.cbtc.event.conversation.*;
import club.joylink.rtss.simulation.cbtc.member.SimulationMember;
import club.joylink.rtss.vo.client.SocketMessageVO;
import club.joylink.rtss.vo.client.WebSocketMessageType;
import club.joylink.rtss.vo.client.factory.SocketMessageFactory;
import club.joylink.rtss.websocket.StompMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 用户群组事件监听器
*/
@Component
public class ConversationGroupListener {
@Autowired
private StompMessageService stompMessageService;
@Autowired
private ConversationGroupHandlerService conversationGroupHandlerService;
/**
* 处理创建群组事件
* @param event 事件信息
*/
@EventListener
public void handleCreate(SimulationConversationGroupCreateEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
SimulationMember leader = conversationGroup.getLeader();
ConversationGroupSocketMessageVO messageVO = ConversationGroupSocketMessageVO.MessageType.JOIN.generateMessageVO(conversationGroup, null);
if (!leader.isRobot()) {
doSendMessage(event.getSimulation(), Set.of(leader.getUserId()), messageVO);
}
Set<String> userIds = conversationGroup.getMemberUserIdWithOutLeader();
if (CollectionUtils.isEmpty(userIds)) {
return;
}
// 给非群主的群人员发送消息添加tips信息
messageVO.setMessageTips(String.format("你加入了【%s】", conversationGroup.getName()));
doSendMessage(event.getSimulation(), userIds, messageVO);
}
/**
* 处理更新群组信息事件
* @param event 事件信息
*/
@EventListener
public void handleUpdate(SimulationConversationGroupUpdateEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
Set<String> userIds = conversationGroup.getMemberUserId();
if (CollectionUtils.isEmpty(userIds)) {
return;
}
ConversationGroupSocketMessageVO messageVO = event.getMessageType().generateMessageVO(conversationGroup, null);
doSendMessage(event.getSimulation(), userIds, messageVO);
}
/**
* 处理邀请成员事件
* @param event 事件信息
*/
@EventListener
public void handleInvite(SimulationConversationGroupInviteEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
Set<String> newUserIdSet = new HashSet<>(); // 发送加入信息的用户列表
StringBuilder stringBuilder = new StringBuilder();
event.getNewMemberList().forEach(m -> {
if (!m.isRobot()) {
newUserIdSet.add(m.getUserId());
}
stringBuilder.append(m.getName()).append("");
});
// 通知新成员
if (!CollectionUtils.isEmpty(newUserIdSet)) {
ConversationGroupSocketMessageVO joinMessageVO = ConversationGroupSocketMessageVO.MessageType.JOIN.generateMessageVO(conversationGroup, null);
// 添加tips信息
joinMessageVO.setMessageTips(String.format("你加入了【%s】", conversationGroup.getName()));
doSendMessage(event.getSimulation(), newUserIdSet, joinMessageVO);
}
Set<String> oldUserIdSet = conversationGroup.getMemberUserId(event.getGroupMemberIds());
// 通知老成员
if (!CollectionUtils.isEmpty(oldUserIdSet)) {
ConversationGroupSocketMessageVO updateMessageVO = ConversationGroupSocketMessageVO.MessageType.UPDATE_MEMBER.generateMessageVO(conversationGroup, null);
updateMessageVO.setMessageTips("欢迎【" + stringBuilder.substring(0, stringBuilder.length() - 1) + "】加入本群");
doSendMessage(event.getSimulation(), conversationGroup.getMemberUserId(event.getGroupMemberIds()), updateMessageVO);
}
}
/**
* 群组移除人员事件
*
* @param event 事件信息
*/
@EventListener
public void handleRemove(SimulationConversationGroupRemoveEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
Set<String> removeUserIdSet = event.getRemoveGroupMember().stream().filter(m -> !m.isRobot()).map(SimulationMember::getUserId).collect(Collectors.toSet());
// 通知移除
if (!CollectionUtils.isEmpty(removeUserIdSet)) {
ConversationGroupSocketMessageVO exitMessageVO = ConversationGroupSocketMessageVO.MessageType.EXIT.generateMessageVO(conversationGroup, null);
// 添加tips信息
exitMessageVO.setMessageTips(String.format("你被移出了【%s】", conversationGroup.getName()));
doSendMessage(event.getSimulation(), removeUserIdSet, exitMessageVO);
}
// 更新其他用户的成员列表
Set<String> userIdSet = conversationGroup.getMemberUserId();
if (CollectionUtils.isEmpty(userIdSet)) {
return;
}
ConversationGroupSocketMessageVO updateMessageVO = ConversationGroupSocketMessageVO.MessageType.UPDATE_MEMBER.generateMessageVO(conversationGroup, null);
doSendMessage(event.getSimulation(), userIdSet, updateMessageVO);
}
/**
* 处理退出群组事件
* @param event 事件信息
*/
@EventListener
public void handleExit(SimulationConversationGroupExitEvent event) {
if (event.getMember().isRobot()) {
return;
}
ConversationGroup conversationGroup = event.getConversationGroup();
ConversationGroupSocketMessageVO exitMessageVO = ConversationGroupSocketMessageVO.MessageType.EXIT.generateMessageVO(conversationGroup, null);
doSendMessage(event.getSimulation(), Set.of(event.getMember().getUserId()), exitMessageVO);
}
/**
* 解散群主事件
* @param event 事件信息
*/
@EventListener
public void handleDissolve(SimulationConversationGroupDissolveEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
// 更新其他用户的成员列表
Set<String> userIdSet = conversationGroup.getMemberUserId();
if (CollectionUtils.isEmpty(userIdSet)) {
return;
}
ConversationGroupSocketMessageVO exitMessageVO = ConversationGroupSocketMessageVO.MessageType.EXIT.generateMessageVO(conversationGroup, null);
exitMessageVO.setMessageTips(String.format("【%s】解散了", conversationGroup.getName()));
doSendMessage(event.getSimulation(), userIdSet, exitMessageVO);
}
/**
* 处理消息状态
* @param event
*/
@EventListener
public void handleMessageStatus(SimulationConversationGroupMessageStatusEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
event.getMessageList().stream().filter(m -> !m.getMember().isRobot()).forEach(message -> {
ConversationGroupSocketMessageVO messageStatusVO
= ConversationGroupSocketMessageVO.MessageType.MESSAGE_STATUS.generateMessageVO(conversationGroup, new ConversationGroupMessageVO(message));
doSendMessage(event.getSimulation(), Set.of(message.getMember().getUserId()), messageStatusVO);
});
}
/**
* 处理聊天信息
* @param event 聊天事件信息
*/
@EventListener
public void handleMessage(SimulationConversationGroupMessageEvent event) {
Simulation simulation = event.getSimulation();
ConversationGroup conversationGroup = event.getConversationGroup();
ConversationGroupMessage message = event.getMessage();
// 发送消息
ConversationGroupMessageVO messageVO = new ConversationGroupMessageVO(message);
Set<String> userIdSet = conversationGroup.getMemberUserId();
if (!CollectionUtils.isEmpty(userIdSet)) {
doSendMessage(simulation, userIdSet, ConversationGroupSocketMessageVO.MessageType.MESSAGE.generateMessageVO(conversationGroup, messageVO));
}
// 发送人员
SimulationMember member = message.getMember();
List<SimulationMember> memberList = conversationGroup.getMemberList().stream()
.filter(cm -> cm.isRobot() && !Objects.equals(cm.getMember(), member))
.map(ConversationMember::getMember).collect(Collectors.toList());
if (CollectionUtils.isEmpty(memberList)) { // 人员全部为真实人员时直接返回
return;
}
// 增加机器人已读操作
memberList.forEach(m -> conversationGroupHandlerService.readConversationGroup(simulation, m, conversationGroup.getId()));
// 如果消息来源不为机器人则执行语音识别目前先不做语音识别回复
// if (!event.getMessage().getMember().isRobot()) {
// VoiceDiscriminateService.doAnalysis(event.getSimulation(), conversationGroup.getId(), event.getMessage().getContent(), memberList);
// }
}
/**
* 发送消息
*
* @param simulation 仿真
* @param userSet 用户列表
* @param messageVO 消息体
*/
private void doSendMessage(Simulation simulation, Set<String> userSet, ConversationGroupSocketMessageVO messageVO) {
if (CollectionUtils.isEmpty(userSet)) {
return;
}
SocketMessageVO<ConversationGroupSocketMessageVO> chatMessage =
SocketMessageFactory.build(WebSocketMessageType.Simulation_Conversation_Group, simulation.getId(), messageVO);
stompMessageService.sendToUser(userSet, chatMessage);
}
}

View File

@ -18,21 +18,15 @@ public class SimulationConversationGroupMessageEvent extends AbstractSimulationE
*/
private ConversationGroup conversationGroup;
/**
* 操作用户
*/
private SimulationMember member;
/**
* 消息内容
*/
private ConversationGroupMessage message;
public SimulationConversationGroupMessageEvent(Object source, Simulation simulation, ConversationGroup conversationGroup
, SimulationMember member, ConversationGroupMessage message) {
, ConversationGroupMessage message) {
super(source, simulation);
this.conversationGroup = conversationGroup;
this.member = member;
this.message = message;
}
}