diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/dao/RoomFriendDao.java b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/dao/RoomFriendDao.java index f8d928e..9918153 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/dao/RoomFriendDao.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/dao/RoomFriendDao.java @@ -30,6 +30,13 @@ public class RoomFriendDao extends ServiceImpl { .update(); } + public void disableRoom(String key) { + lambdaUpdate() + .eq(RoomFriend::getRoomKey, key) + .set(RoomFriend::getStatus, NormalOrNoEnum.NOT_NORMAL.getStatus()) + .update(); + } + public List listByRoomIds(List roomIds) { return lambdaQuery() .in(RoomFriend::getRoomId, roomIds) diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/entity/Room.java b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/entity/Room.java index 5597d0e..cbcfe88 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/entity/Room.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/entity/Room.java @@ -1,9 +1,12 @@ package com.abin.mallchat.common.chat.domain.entity; +import com.abin.mallchat.common.chat.domain.enums.HotFlagEnum; +import com.abin.mallchat.common.chat.domain.enums.RoomTypeEnum; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import lombok.EqualsAndHashCode; @@ -77,4 +80,18 @@ public class Room implements Serializable { private Date updateTime; + @JsonIgnore + public boolean isHotRoom() { + return HotFlagEnum.of(this.hotFlag) == HotFlagEnum.YES; + } + + @JsonIgnore + public boolean isRoomFriend() { + return RoomTypeEnum.of(this.type) == RoomTypeEnum.FRIEND; + } + + @JsonIgnore + public boolean isRoomGroup() { + return RoomTypeEnum.of(this.hotFlag) == RoomTypeEnum.GROUP; + } } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/RoomService.java b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/RoomService.java index f4c069d..a6f680e 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/RoomService.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/RoomService.java @@ -17,6 +17,12 @@ public interface RoomService { */ RoomFriend createFriendRoom(List uidList); + /** + * 禁用一个单聊房间 + */ + void disableFriendRoom(List uidList); + + /** * 创建一个群聊房间 */ diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/impl/RoomServiceImpl.java b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/impl/RoomServiceImpl.java index 47022e4..9e202a8 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/impl/RoomServiceImpl.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/impl/RoomServiceImpl.java @@ -59,6 +59,14 @@ public class RoomServiceImpl implements RoomService { return roomFriend; } + @Override + public void disableFriendRoom(List uidList) { + AssertUtil.isNotEmpty(uidList, "房间创建失败,好友数量不对"); + AssertUtil.equal(uidList.size(), 2, "房间创建失败,好友数量不对"); + String key = ChatAdapter.generateRoomKey(uidList); + roomFriendDao.disableRoom(key); + } + @Override @Transactional(rollbackFor = Exception.class) public RoomGroup createGroupRoom(Long uid) { 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 6171cc8..c81b4fe 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 @@ -5,19 +5,17 @@ import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import cn.hutool.core.lang.Pair; -import com.abin.mallchat.common.chat.dao.ContactDao; -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.dao.*; import com.abin.mallchat.common.chat.domain.dto.MsgReadInfoDTO; -import com.abin.mallchat.common.chat.domain.entity.Contact; -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.*; import com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum; import com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum; import com.abin.mallchat.common.chat.domain.vo.response.ChatMessageResp; import com.abin.mallchat.common.chat.service.ContactService; +import com.abin.mallchat.common.chat.service.cache.RoomCache; +import com.abin.mallchat.common.chat.service.cache.RoomGroupCache; import com.abin.mallchat.common.common.annotation.RedissonLock; +import com.abin.mallchat.common.common.domain.enums.NormalOrNoEnum; 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.MessageSendEvent; @@ -28,7 +26,6 @@ import com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum; import com.abin.mallchat.common.user.domain.enums.RoleEnum; import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp; import com.abin.mallchat.common.user.service.IRoleService; -import com.abin.mallchat.common.user.service.cache.ItemCache; import com.abin.mallchat.common.user.service.cache.UserCache; import com.abin.mallchat.custom.chat.domain.vo.request.*; import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp; @@ -81,7 +78,7 @@ public class ChatServiceImpl implements ChatService { @Autowired private MessageMarkDao messageMarkDao; @Autowired - private ItemCache itemCache; + private RoomFriendDao roomFriendDao; @Autowired private IRoleService iRoleService; @Autowired @@ -90,6 +87,12 @@ public class ChatServiceImpl implements ChatService { private ContactService contactService; @Autowired private ContactDao contactDao; + @Autowired + private RoomCache roomCache; + @Autowired + private GroupMemberDao groupMemberDao; + @Autowired + private RoomGroupCache roomGroupCache; /** * 发送消息 @@ -97,6 +100,7 @@ public class ChatServiceImpl implements ChatService { @Override @Transactional public Long sendMsg(ChatMessageReq request, Long uid) { + check(request, uid); AbstractMsgHandler msgHandler = MsgHandlerFactory.getStrategyNoNull(request.getMsgType());//todo 这里先不扩展,后续再改 msgHandler.checkMsg(request, uid); //同步获取消息的跳转链接标题 @@ -108,6 +112,24 @@ public class ChatServiceImpl implements ChatService { return insert.getId(); } + private void check(ChatMessageReq request, Long uid) { + Room room = roomCache.get(request.getRoomId()); + if (room.isHotRoom()) {//全员群跳过校验 + return; + } + if (room.isRoomFriend()) { + RoomFriend roomFriend = roomFriendDao.getByRoomId(request.getRoomId()); + AssertUtil.equal(NormalOrNoEnum.NORMAL.getStatus(), roomFriend.getStatus(), "您已经被对方拉黑"); + AssertUtil.isTrue(uid.equals(roomFriend.getUid1()) || uid.equals(roomFriend.getUid2()), "您已经被对方拉黑"); + } + if (room.isRoomGroup()) { + RoomGroup roomGroup = roomGroupCache.get(request.getRoomId()); + GroupMember member = groupMemberDao.getMember(roomGroup.getId(), uid); + AssertUtil.isNotEmpty(member, "您已经被移除该群"); + } + + } + @Override public ChatMessageResp getMsgResp(Message message, Long receiveUid) { return CollUtil.getFirst(getMsgRespBatch(Collections.singletonList(message), receiveUid)); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/RoomAppServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/RoomAppServiceImpl.java index 70d3054..51d05e2 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/RoomAppServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/RoomAppServiceImpl.java @@ -178,6 +178,7 @@ public class RoomAppServiceImpl implements RoomAppService { } @Override + @Transactional(rollbackFor = Exception.class) public void delMember(Long uid, MemberDelReq request) { Room room = roomCache.get(request.getRoomId()); AssertUtil.isNotEmpty(room, "房间号有误"); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/FriendServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/FriendServiceImpl.java index ef2fdca..d9e56e9 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/FriendServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/FriendServiceImpl.java @@ -1,6 +1,7 @@ package com.abin.mallchat.custom.user.service.impl; import cn.hutool.core.collection.CollectionUtil; +import com.abin.mallchat.common.chat.dao.RoomFriendDao; import com.abin.mallchat.common.chat.domain.entity.RoomFriend; import com.abin.mallchat.common.chat.service.ContactService; import com.abin.mallchat.common.chat.service.RoomService; @@ -67,9 +68,10 @@ public class FriendServiceImpl implements FriendService { private ContactService contactService; @Autowired private ChatService chatService; - @Autowired private UserDao userDao; + @Autowired + private RoomFriendDao roomFriendDao; /** * 检查 @@ -176,6 +178,7 @@ public class FriendServiceImpl implements FriendService { * @param friendUid 朋友uid */ @Override + @Transactional(rollbackFor = Exception.class) public void deleteFriend(Long uid, Long friendUid) { List userFriends = userFriendDao.getUserFriend(uid, friendUid); if (CollectionUtil.isEmpty(userFriends)) { @@ -184,6 +187,8 @@ public class FriendServiceImpl implements FriendService { } List friendRecordIds = userFriends.stream().map(UserFriend::getId).collect(Collectors.toList()); userFriendDao.removeByIds(friendRecordIds); + //禁用房间 + roomService.disableFriendRoom(friendRecordIds); } @Override