From 59a80110efeb187a14c44c05a76b7f51b0cbe9f1 Mon Sep 17 00:00:00 2001 From: zhongzb <972627721@qq.com> Date: Thu, 1 Jun 2023 21:57:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B6=88=E6=81=AF=E6=A0=87=E8=AE=B0?= =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/mallchat.sql | 16 ---- mallchat-common/pom.xml | 11 ++- .../chat/domain/dto/ChatMessageMarkDTO.java | 10 +++ .../domain/enums/MessageMarkActTypeEnum.java | 35 ++++++++ .../common/domain/enums/NormalOrNoEnum.java | 39 +++++++++ .../common/exception/CommonErrorEnum.java | 4 +- .../common/factory/MyThreadFactory.java | 7 +- .../GlobalUncaughtExceptionHandler.java | 4 +- .../chat/controller/ChatController.java | 2 +- .../vo/response/ChatMemberStatisticResp.java | 1 + .../chat/service/impl/ChatServiceImpl.java | 39 +++------ .../mark/AbstractMsgMarkStrategy.java | 80 +++++++++++++++++++ .../strategy/mark/DisLikeStrategy.java | 31 +++++++ .../service/strategy/mark/LikeStrategy.java | 25 ++++++ .../service/strategy/mark/MsgMarkFactory.java | 26 ++++++ .../event/listener/MessageMarkListener.java | 16 +++- .../event/listener/MessageSendListener.java | 4 +- .../event/listener/UserOnlineListener.java | 2 +- .../event/listener/UserRegisterListener.java | 22 ++--- .../user/domain/enums/WSRespTypeEnum.java | 1 + .../user/domain/vo/response/ws/WSMsgMark.java | 42 ++++++++++ .../vo/response/ws/WSOnlineOfflineNotify.java | 1 + .../custom/user/service/WebSocketService.java | 9 ++- .../user/service/adapter/WSAdapter.java | 19 ++++- .../service/impl/WebSocketServiceImpl.java | 9 ++- pom.xml | 1 - 26 files changed, 377 insertions(+), 79 deletions(-) create mode 100644 mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/enums/MessageMarkActTypeEnum.java create mode 100644 mallchat-common/src/main/java/com/abin/mallchat/common/common/domain/enums/NormalOrNoEnum.java create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/AbstractMsgMarkStrategy.java create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/DisLikeStrategy.java create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/LikeStrategy.java create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/MsgMarkFactory.java create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSMsgMark.java diff --git a/docs/mallchat.sql b/docs/mallchat.sql index 211f22a..944a4eb 100644 --- a/docs/mallchat.sql +++ b/docs/mallchat.sql @@ -1,19 +1,3 @@ -/* - Navicat Premium Data Transfer - - Source Server : mallchat - Source Server Type : MySQL - Source Server Version : 50741 - Source Host : 101.33.251.36:3306 - Source Schema : mallchat - - Target Server Type : MySQL - Target Server Version : 50741 - File Encoding : 65001 - - Date: 10/05/2023 14:25:59 -*/ - SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; diff --git a/mallchat-common/pom.xml b/mallchat-common/pom.xml index 3dc8f61..2a9d84a 100644 --- a/mallchat-common/pom.xml +++ b/mallchat-common/pom.xml @@ -93,7 +93,7 @@ 2.0.9 - org.hibernate + org.hibernate.validator hibernate-validator 6.0.1.Final @@ -116,5 +116,14 @@ 3.17.1 + + + + org.apache.maven.plugins + maven-resources-plugin + 2.4.3 + + + \ No newline at end of file diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/dto/ChatMessageMarkDTO.java b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/dto/ChatMessageMarkDTO.java index 558baef..b74bbd4 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/dto/ChatMessageMarkDTO.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/dto/ChatMessageMarkDTO.java @@ -16,12 +16,22 @@ import lombok.NoArgsConstructor; @AllArgsConstructor @NoArgsConstructor public class ChatMessageMarkDTO { + + @ApiModelProperty("操作者") + private Long uid; + @ApiModelProperty("消息id") private Long msgId; + /** + * @see com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum + */ @ApiModelProperty("标记类型 1点赞 2举报") private Integer markType; + /** + * @see com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum + */ @ApiModelProperty("动作类型 1确认 2取消") private Integer actType; } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/enums/MessageMarkActTypeEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/enums/MessageMarkActTypeEnum.java new file mode 100644 index 0000000..017dcc6 --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/enums/MessageMarkActTypeEnum.java @@ -0,0 +1,35 @@ +package com.abin.mallchat.common.chat.domain.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * Description: 消息标记动作类型 + * Author: abin + * Date: 2023-03-19 + */ +@AllArgsConstructor +@Getter +public enum MessageMarkActTypeEnum { + MARK(1, "确认标记"), + UN_MARK(2, "取消标记"), + ; + + private final Integer type; + private final String desc; + + private static Map cache; + + static { + cache = Arrays.stream(MessageMarkActTypeEnum.values()).collect(Collectors.toMap(MessageMarkActTypeEnum::getType, Function.identity())); + } + + public static MessageMarkActTypeEnum of(Integer type) { + return cache.get(type); + } +} diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/domain/enums/NormalOrNoEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/domain/enums/NormalOrNoEnum.java new file mode 100644 index 0000000..37477ba --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/domain/enums/NormalOrNoEnum.java @@ -0,0 +1,39 @@ +package com.abin.mallchat.common.common.domain.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * Description: 是否正常的通用枚举 + * Author: abin + * Date: 2023-03-19 + */ +@AllArgsConstructor +@Getter +public enum NormalOrNoEnum { + NORMAL(0, "正常"), + NOT_NORMAL(1, "不正常"), + ; + + private final Integer status; + private final String desc; + + private static Map cache; + + static { + cache = Arrays.stream(NormalOrNoEnum.values()).collect(Collectors.toMap(NormalOrNoEnum::getStatus, Function.identity())); + } + + public static NormalOrNoEnum of(Integer type) { + return cache.get(type); + } + + public static Integer toStatus(Boolean bool) { + return bool ? NORMAL.getStatus() : NOT_NORMAL.getStatus(); + } +} diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/CommonErrorEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/CommonErrorEnum.java index 10804e8..edcd880 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/CommonErrorEnum.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/CommonErrorEnum.java @@ -17,8 +17,8 @@ public enum CommonErrorEnum implements ErrorEnum { FREQUENCY_LIMIT(-3, "请求太频繁了,请稍后再试哦~~"), LOCK_LIMIT(-4, "请求太频繁了,请稍后再试哦~~"), ; - private Integer code; - private String msg; + private final Integer code; + private final String msg; @Override public Integer getErrorCode() { diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/factory/MyThreadFactory.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/factory/MyThreadFactory.java index b3733eb..db07ee6 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/factory/MyThreadFactory.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/factory/MyThreadFactory.java @@ -2,10 +2,7 @@ package com.abin.mallchat.common.common.factory; import com.abin.mallchat.common.common.handler.GlobalUncaughtExceptionHandler; import lombok.AllArgsConstructor; -import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.scheduling.concurrent.CustomizableThreadFactory; -import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.ThreadFactory; @@ -13,14 +10,12 @@ import java.util.concurrent.ThreadFactory; @AllArgsConstructor public class MyThreadFactory implements ThreadFactory { - private ThreadFactory factory; + private final ThreadFactory factory; @Override public Thread newThread(Runnable r) { Thread thread =factory.newThread(r); thread.setUncaughtExceptionHandler(new GlobalUncaughtExceptionHandler()); - thread.setDaemon(false); - thread.setPriority(5); return thread; } } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java index 79f4e24..b3229e6 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/handler/GlobalUncaughtExceptionHandler.java @@ -1,7 +1,5 @@ package com.abin.mallchat.common.common.handler; -import lombok.AllArgsConstructor; -import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -10,7 +8,7 @@ public class GlobalUncaughtExceptionHandler implements Thread.UncaughtException @Override public void uncaughtException(Thread t, Throwable e) { - log.error("{} task execute is error",t.getName()); + log.error("Exception in thread {} ", t.getName(), e); e.printStackTrace(); } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java index 77975d8..ce9c756 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java @@ -64,7 +64,7 @@ public class ChatController { return userCache.getBlackMap().getOrDefault(BlackTypeEnum.UID.getType(), new HashSet<>()); } - @GetMapping("public/member/statistic/") + @GetMapping("public/member/statistic") @ApiOperation("群成员人数统计") public ApiResult getMemberStatistic() { return ApiResult.success(chatService.getMemberStatistic()); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/response/ChatMemberStatisticResp.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/response/ChatMemberStatisticResp.java index 80a321f..5f26878 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/response/ChatMemberStatisticResp.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/response/ChatMemberStatisticResp.java @@ -20,5 +20,6 @@ public class ChatMemberStatisticResp { @ApiModelProperty("在线人数") private Long onlineNum;//在线人数 @ApiModelProperty("总人数") + @Deprecated private Long totalNum;//总人数 } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/ChatServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/ChatServiceImpl.java index 9ecec66..191fcba 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/ChatServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/ChatServiceImpl.java @@ -6,15 +6,14 @@ import cn.hutool.core.lang.Pair; import com.abin.mallchat.common.chat.dao.MessageDao; import com.abin.mallchat.common.chat.dao.MessageMarkDao; import com.abin.mallchat.common.chat.dao.RoomDao; -import com.abin.mallchat.common.chat.domain.dto.ChatMessageMarkDTO; import com.abin.mallchat.common.chat.domain.entity.Message; import com.abin.mallchat.common.chat.domain.entity.MessageMark; import com.abin.mallchat.common.chat.domain.entity.Room; +import com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum; import com.abin.mallchat.common.common.annotation.RedissonLock; import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum; import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq; import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp; -import com.abin.mallchat.common.common.event.MessageMarkEvent; import com.abin.mallchat.common.common.event.MessageSendEvent; import com.abin.mallchat.common.common.exception.BusinessException; import com.abin.mallchat.common.common.utils.AssertUtil; @@ -36,8 +35,9 @@ import com.abin.mallchat.custom.chat.service.adapter.MemberAdapter; import com.abin.mallchat.custom.chat.service.adapter.MessageAdapter; import com.abin.mallchat.custom.chat.service.adapter.RoomAdapter; import com.abin.mallchat.custom.chat.service.helper.ChatMemberHelper; +import com.abin.mallchat.custom.chat.service.strategy.mark.AbstractMsgMarkStrategy; +import com.abin.mallchat.custom.chat.service.strategy.mark.MsgMarkFactory; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; @@ -165,38 +165,25 @@ public class ChatServiceImpl implements ChatService { public ChatMemberStatisticResp getMemberStatistic() { System.out.println(Thread.currentThread().getName()); Long onlineNum = userCache.getOnlineNum(); - Long offlineNum = userCache.getOfflineNum(); +// Long offlineNum = userCache.getOfflineNum();不展示总人数 ChatMemberStatisticResp resp = new ChatMemberStatisticResp(); resp.setOnlineNum(onlineNum); - resp.setTotalNum(onlineNum + offlineNum); +// resp.setTotalNum(onlineNum + offlineNum); return resp; } @Override @RedissonLock(key = "#uid") public void setMsgMark(Long uid, ChatMessageMarkReq request) { - //用户对该消息的标记 - MessageMark messageMark = messageMarkDao.get(uid, request.getMsgId(), request.getMarkType()); - if (Objects.nonNull(messageMark)) {//有标记过消息修改一下就好 - MessageMark update = MessageMark.builder() - .id(messageMark.getId()) - .status(transformAct(request.getActType())) - .build(); - messageMarkDao.updateById(update); - return; + AbstractMsgMarkStrategy strategy = MsgMarkFactory.getStrategyNoNull(request.getMarkType()); + switch (MessageMarkActTypeEnum.of(request.getActType())) { + case MARK: + strategy.mark(uid, request.getMsgId()); + break; + case UN_MARK: + strategy.unMark(uid, request.getMsgId()); + break; } - //没标记过消息,插入一条新消息 - MessageMark insert = MessageMark.builder() - .uid(uid) - .msgId(request.getMsgId()) - .type(request.getMarkType()) - .status(transformAct(request.getActType())) - .build(); - messageMarkDao.save(insert); - //发布消息标记事件 - ChatMessageMarkDTO dto = new ChatMessageMarkDTO(); - BeanUtils.copyProperties(request, dto); - applicationEventPublisher.publishEvent(new MessageMarkEvent(this, dto)); } private Integer transformAct(Integer actType) { diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/AbstractMsgMarkStrategy.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/AbstractMsgMarkStrategy.java new file mode 100644 index 0000000..17cf032 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/AbstractMsgMarkStrategy.java @@ -0,0 +1,80 @@ +package com.abin.mallchat.custom.chat.service.strategy.mark; + +import com.abin.mallchat.common.chat.dao.MessageMarkDao; +import com.abin.mallchat.common.chat.domain.dto.ChatMessageMarkDTO; +import com.abin.mallchat.common.chat.domain.entity.MessageMark; +import com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum; +import com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum; +import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum; +import com.abin.mallchat.common.common.event.MessageMarkEvent; +import com.abin.mallchat.common.common.exception.BusinessException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.PostConstruct; +import java.util.Objects; +import java.util.Optional; + +/** + * Description: 消息标记抽象类 + * Author: abin + * Date: 2023-05-30 + */ +public abstract class AbstractMsgMarkStrategy { + @Autowired + private MessageMarkDao messageMarkDao; + @Autowired + private ApplicationEventPublisher applicationEventPublisher; + + protected abstract MessageMarkTypeEnum getTypeEnum(); + + @Transactional + public void mark(Long uid, Long msgId) { + exec(uid, msgId, MessageMarkActTypeEnum.MARK); + } + + @Transactional + public void unMark(Long uid, Long msgId) { + exec(uid, msgId, MessageMarkActTypeEnum.UN_MARK); + } + + @PostConstruct + private void init() { + MsgMarkFactory.register(getTypeEnum().getType(), this); + } + + protected void exec(Long uid, Long msgId, MessageMarkActTypeEnum actTypeEnum) { + Integer markType = getTypeEnum().getType(); + Integer actType = actTypeEnum.getType(); + MessageMark oldMark = messageMarkDao.get(uid, msgId, markType); + if (Objects.isNull(oldMark) && actTypeEnum == MessageMarkActTypeEnum.UN_MARK) { + //取消的类型,数据库一定有记录,没有就直接跳过操作 + return; + } + //插入一条新消息,或者修改一条消息 + MessageMark insertOrUpdate = MessageMark.builder() + .id(Optional.ofNullable(oldMark).map(MessageMark::getId).orElse(null)) + .uid(uid) + .msgId(msgId) + .type(markType) + .status(transformAct(actType)) + .build(); + boolean modify = messageMarkDao.saveOrUpdate(insertOrUpdate); + if (modify) { + //修改成功才发布消息标记事件 + ChatMessageMarkDTO dto = new ChatMessageMarkDTO(uid, msgId, markType, actType); + applicationEventPublisher.publishEvent(new MessageMarkEvent(this, dto)); + } + } + + private Integer transformAct(Integer actType) { + if (actType == 1) { + return YesOrNoEnum.NO.getStatus(); + } else if (actType == 2) { + return YesOrNoEnum.YES.getStatus(); + } + throw new BusinessException("动作类型 1确认 2取消"); + } + +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/DisLikeStrategy.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/DisLikeStrategy.java new file mode 100644 index 0000000..e6ccd05 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/DisLikeStrategy.java @@ -0,0 +1,31 @@ +package com.abin.mallchat.custom.chat.service.strategy.mark; + +import com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +/** + * Description: 点踩标记策略类 + * Author: abin + * Date: 2023-05-30 + */ +@Component +public class DisLikeStrategy extends AbstractMsgMarkStrategy { + @Autowired + @Lazy + private LikeStrategy likeStrategy; + + @Override + protected MessageMarkTypeEnum getTypeEnum() { + return MessageMarkTypeEnum.DISLIKE; + } + + @Override + public void mark(Long uid, Long msgId) { + super.mark(uid, msgId); + //同时取消点赞的动作 + MsgMarkFactory.getStrategyNoNull(MessageMarkTypeEnum.LIKE.getType()).unMark(uid, msgId); + } + +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/LikeStrategy.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/LikeStrategy.java new file mode 100644 index 0000000..4f86696 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/LikeStrategy.java @@ -0,0 +1,25 @@ +package com.abin.mallchat.custom.chat.service.strategy.mark; + +import com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum; +import org.springframework.stereotype.Component; + +/** + * Description: 点赞标记策略类 + * Author: abin + * Date: 2023-05-30 + */ +@Component +public class LikeStrategy extends AbstractMsgMarkStrategy { + + @Override + protected MessageMarkTypeEnum getTypeEnum() { + return MessageMarkTypeEnum.LIKE; + } + + @Override + public void mark(Long uid, Long msgId) { + super.mark(uid, msgId); + //同时取消点踩的动作 + MsgMarkFactory.getStrategyNoNull(MessageMarkTypeEnum.DISLIKE.getType()).unMark(uid, msgId); + } +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/MsgMarkFactory.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/MsgMarkFactory.java new file mode 100644 index 0000000..4b6acc4 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/MsgMarkFactory.java @@ -0,0 +1,26 @@ +package com.abin.mallchat.custom.chat.service.strategy.mark; + +import com.abin.mallchat.common.common.exception.CommonErrorEnum; +import com.abin.mallchat.common.common.utils.AssertUtil; + +import java.util.HashMap; +import java.util.Map; + +/** + * Description: 消息标记策略工厂 + * Author: abin + * Date: 2023-05-30 + */ +public class MsgMarkFactory { + private static final Map STRATEGY_MAP = new HashMap<>(); + + public static void register(Integer markType, AbstractMsgMarkStrategy strategy) { + STRATEGY_MAP.put(markType, strategy); + } + + public static AbstractMsgMarkStrategy getStrategyNoNull(Integer markType) { + AbstractMsgMarkStrategy strategy = STRATEGY_MAP.get(markType); + AssertUtil.isNotEmpty(strategy, CommonErrorEnum.PARAM_VALID); + return strategy; + } +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/MessageMarkListener.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/MessageMarkListener.java index 550ae33..afbab99 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/MessageMarkListener.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/MessageMarkListener.java @@ -10,11 +10,13 @@ import com.abin.mallchat.common.common.domain.enums.IdempotentEnum; import com.abin.mallchat.common.common.event.MessageMarkEvent; import com.abin.mallchat.common.user.domain.enums.ItemEnum; import com.abin.mallchat.common.user.service.IUserBackpackService; +import com.abin.mallchat.custom.user.service.WebSocketService; +import com.abin.mallchat.custom.user.service.adapter.WSAdapter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionalEventListener; import java.util.Objects; @@ -32,9 +34,11 @@ public class MessageMarkListener { private MessageDao messageDao; @Autowired private IUserBackpackService iUserBackpackService; + @Autowired + private WebSocketService webSocketService; @Async - @EventListener(classes = MessageMarkEvent.class) + @TransactionalEventListener(classes = MessageMarkEvent.class, fallbackExecution = true) public void changeMsgType(MessageMarkEvent event) { ChatMessageMarkDTO dto = event.getDto(); Message msg = messageDao.getById(dto.getMsgId()); @@ -53,4 +57,12 @@ public class MessageMarkListener { } } + @Async + @TransactionalEventListener(classes = MessageMarkEvent.class, fallbackExecution = true) + public void notifyAll(MessageMarkEvent event) {//后续可做合并查询,目前异步影响不大 + ChatMessageMarkDTO dto = event.getDto(); + Integer markCount = messageMarkDao.getMarkCount(dto.getMsgId(), dto.getMarkType()); + webSocketService.sendToAllOnline(WSAdapter.buildMsgMarkSend(dto, markCount), dto.getUid()); + } + } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/MessageSendListener.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/MessageSendListener.java index a095c7d..15b7cab 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/MessageSendListener.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/MessageSendListener.java @@ -9,9 +9,9 @@ import com.abin.mallchat.custom.user.service.WebSocketService; import com.abin.mallchat.custom.user.service.adapter.WSAdapter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionalEventListener; /** * 消息发送监听器 @@ -29,7 +29,7 @@ public class MessageSendListener { private MessageDao messageDao; @Async - @EventListener(classes = MessageSendEvent.class) + @TransactionalEventListener(classes = MessageSendEvent.class, fallbackExecution = true) public void notifyAllOnline(MessageSendEvent event) { Message message = messageDao.getById(event.getMsgId()); ChatMessageResp msgResp = chatService.getMsgResp(message, null); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserOnlineListener.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserOnlineListener.java index 07fd514..a794a6a 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserOnlineListener.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserOnlineListener.java @@ -38,7 +38,7 @@ public class UserOnlineListener { User user = event.getUser(); userCache.online(user.getId(), user.getLastOptTime()); //推送给所有在线用户,该用户登录成功 - webSocketService.sendToAllOnline(wsAdapter.buildOnlineNotifyResp(event.getUser()), null); + webSocketService.sendToAllOnline(wsAdapter.buildOnlineNotifyResp(event.getUser())); } @Async diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserRegisterListener.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserRegisterListener.java index 1336082..d6d2775 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserRegisterListener.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserRegisterListener.java @@ -33,16 +33,16 @@ public class UserRegisterListener { iUserBackpackService.acquireItem(user.getId(), ItemEnum.MODIFY_NAME_CARD.getId(), IdempotentEnum.UID, user.getId().toString()); } - @Async - @EventListener(classes = UserRegisterEvent.class) - public void sendBadge(UserRegisterEvent event) { - User user = event.getUser(); - int count = userDao.count();//todo 性能瓶颈,等注册用户多了直接删掉 - if (count <= 10) { - iUserBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP10_BADGE.getId(), IdempotentEnum.UID, user.getId().toString()); - } else if (count <= 100) { - iUserBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP100_BADGE.getId(), IdempotentEnum.UID, user.getId().toString()); - } - } +// @Async +// @EventListener(classes = UserRegisterEvent.class) +// public void sendBadge(UserRegisterEvent event) { +// User user = event.getUser(); +// int count = userDao.count();// 性能瓶颈,等注册用户多了直接删掉 +// if (count <= 10) { +// iUserBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP10_BADGE.getId(), IdempotentEnum.UID, user.getId().toString()); +// } else if (count <= 100) { +// iUserBackpackService.acquireItem(user.getId(), ItemEnum.REG_TOP100_BADGE.getId(), IdempotentEnum.UID, user.getId().toString()); +// } +// } } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/enums/WSRespTypeEnum.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/enums/WSRespTypeEnum.java index c0d0f2a..cd8649f 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/enums/WSRespTypeEnum.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/enums/WSRespTypeEnum.java @@ -24,6 +24,7 @@ public enum WSRespTypeEnum { ONLINE_OFFLINE_NOTIFY(5, "上下线通知", WSOnlineOfflineNotify.class), INVALIDATE_TOKEN(6, "使前端的token失效,意味着前端需要重新登录", null), BLACK(7, "拉黑用户", WSBlack.class), + MARK(8, "消息标记", WSMsgMark.class), ; private final Integer type; diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSMsgMark.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSMsgMark.java new file mode 100644 index 0000000..4e9aa93 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSMsgMark.java @@ -0,0 +1,42 @@ +package com.abin.mallchat.custom.user.domain.vo.response.ws; + +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * Description: + * Author: abin + * Date: 2023-03-19 + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class WSMsgMark { + private List markList; + + @Data + public static class WSMsgMarkItem { + @ApiModelProperty("操作者") + private Long uid; + @ApiModelProperty("消息id") + private Long msgId; + /** + * @see com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum + */ + @ApiModelProperty("标记类型 1点赞 2举报") + private Integer markType; + @ApiModelProperty("被标记的数量") + private Integer markCount; + /** + * @see com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum + */ + @ApiModelProperty("动作类型 1确认 2取消") + private Integer actType; + } +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSOnlineOfflineNotify.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSOnlineOfflineNotify.java index 8ec1f23..9d9c82f 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSOnlineOfflineNotify.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSOnlineOfflineNotify.java @@ -21,6 +21,7 @@ import java.util.List; public class WSOnlineOfflineNotify { private List changeList = new ArrayList<>();//新的上下线用户 private Long onlineNum;//在线人数 + @Deprecated private Long totalNum;//总人数 } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WebSocketService.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WebSocketService.java index 2561d39..43b925a 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WebSocketService.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WebSocketService.java @@ -57,7 +57,14 @@ public interface WebSocketService { * @param wsBaseResp 发送的消息体 * @param skipUid 需要跳过的人 */ - void sendToAllOnline(WSBaseResp wsBaseResp, Long skipUid); + void sendToAllOnline(WSBaseResp wsBaseResp, Long skipUid); + + /** + * 推动消息给所有在线的人 + * + * @param wsBaseResp 发送的消息体 + */ + void sendToAllOnline(WSBaseResp wsBaseResp); } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/WSAdapter.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/WSAdapter.java index 7412b0a..49bddb0 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/WSAdapter.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/WSAdapter.java @@ -1,6 +1,7 @@ package com.abin.mallchat.custom.user.service.adapter; import cn.hutool.core.bean.BeanUtil; +import com.abin.mallchat.common.chat.domain.dto.ChatMessageMarkDTO; import com.abin.mallchat.common.user.domain.entity.User; import com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum; import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberResp; @@ -8,11 +9,9 @@ import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberStatisticResp; import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageResp; import com.abin.mallchat.custom.chat.service.ChatService; import com.abin.mallchat.custom.user.domain.enums.WSRespTypeEnum; -import com.abin.mallchat.custom.user.domain.vo.response.ws.WSBaseResp; -import com.abin.mallchat.custom.user.domain.vo.response.ws.WSLoginSuccess; -import com.abin.mallchat.custom.user.domain.vo.response.ws.WSLoginUrl; -import com.abin.mallchat.custom.user.domain.vo.response.ws.WSOnlineOfflineNotify; +import com.abin.mallchat.custom.user.domain.vo.response.ws.*; import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -110,4 +109,16 @@ public class WSAdapter { wsBaseResp.setData(msgResp); return wsBaseResp; } + + public static WSBaseResp buildMsgMarkSend(ChatMessageMarkDTO dto, Integer markCount) { + WSMsgMark.WSMsgMarkItem item = new WSMsgMark.WSMsgMarkItem(); + BeanUtils.copyProperties(dto, item); + item.setMarkCount(markCount); + WSBaseResp wsBaseResp = new WSBaseResp<>(); + wsBaseResp.setType(WSRespTypeEnum.MARK.getType()); + WSMsgMark mark = new WSMsgMark(); + mark.setMarkList(Collections.singletonList(item)); + wsBaseResp.setData(mark); + return wsBaseResp; + } } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java index 26ba0f6..c21f80a 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java @@ -227,7 +227,7 @@ public class WebSocketServiceImpl implements WebSocketService { //entrySet的值不是快照数据,但是它支持遍历,所以无所谓了,不用快照也行。 @Override - public void sendToAllOnline(WSBaseResp wsBaseResp, Long skipUid) { + public void sendToAllOnline(WSBaseResp wsBaseResp, Long skipUid) { ONLINE_WS_MAP.forEach((channel, ext) -> { if (ObjectUtil.equal(ext.getUid(), skipUid)) { return; @@ -236,7 +236,12 @@ public class WebSocketServiceImpl implements WebSocketService { }); } - private void sendMsg(Channel channel, WSBaseResp wsBaseResp) { + @Override + public void sendToAllOnline(WSBaseResp wsBaseResp) { + sendToAllOnline(wsBaseResp, null); + } + + private void sendMsg(Channel channel, WSBaseResp wsBaseResp) { channel.writeAndFlush(new TextWebSocketFrame(JSONUtil.toJsonStr(wsBaseResp))); } diff --git a/pom.xml b/pom.xml index 4521335..8968511 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,6 @@ 1.8 true http://192.168.3.101:2375 - 0.40.2 5.8.18 3.0.0 1.6.0