Browse Source

代码优化

master
blue 2 years ago
parent
commit
4690aa63b6
  1. 20
      im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java
  2. 12
      im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java
  3. 5
      im-platform/src/main/java/com/bx/implatform/util/SensitiveFilterUtil.java

20
im-platform/src/main/java/com/bx/implatform/service/impl/GroupMessageServiceImpl.java

@ -1,7 +1,7 @@
package com.bx.implatform.service.impl; package com.bx.implatform.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@ -73,8 +73,8 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro
msg.setSendId(session.getUserId()); msg.setSendId(session.getUserId());
msg.setSendTime(new Date()); msg.setSendTime(new Date());
msg.setSendNickName(member.getAliasName()); msg.setSendNickName(member.getAliasName());
if (CollUtil.isNotEmpty(dto.getAtUserIds())) { if (CollectionUtil.isNotEmpty(dto.getAtUserIds())) {
msg.setAtUserIds(CharSequenceUtil.join(",", dto.getAtUserIds())); msg.setAtUserIds(StrUtil.join(",", dto.getAtUserIds()));
} }
this.save(msg); this.save(msg);
// 过滤消息内容 // 过滤消息内容
@ -146,7 +146,7 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro
UserSession session = SessionContext.getSession(); UserSession session = SessionContext.getSession();
List<GroupMember> members = groupMemberService.findByUserId(session.getUserId()); List<GroupMember> members = groupMemberService.findByUserId(session.getUserId());
List<Long> ids = members.stream().map(GroupMember::getGroupId).collect(Collectors.toList()); List<Long> ids = members.stream().map(GroupMember::getGroupId).collect(Collectors.toList());
if (CollUtil.isEmpty(ids)) { if (CollectionUtil.isEmpty(ids)) {
return new ArrayList<>(); return new ArrayList<>();
} }
// 只能拉取最近1个月的 // 只能拉取最近1个月的
@ -158,7 +158,6 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro
List<GroupMessage> messages = this.list(wrapper); List<GroupMessage> messages = this.list(wrapper);
// 转成vo // 转成vo
List<GroupMessageVO> vos = messages.stream().map(m -> { List<GroupMessageVO> vos = messages.stream().map(m -> {
m.setContent(sensitiveFilterUtil.filter(m.getContent()));
GroupMessageVO vo = BeanUtils.copyProperties(m, GroupMessageVO.class); GroupMessageVO vo = BeanUtils.copyProperties(m, GroupMessageVO.class);
// 被@用户列表 // 被@用户列表
if (StringUtils.isNotBlank(m.getAtUserIds())) { if (StringUtils.isNotBlank(m.getAtUserIds())) {
@ -212,7 +211,7 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro
sendMessage.setSendResult(true); sendMessage.setSendResult(true);
imClient.sendGroupMessage(sendMessage); imClient.sendGroupMessage(sendMessage);
// 记录已读消息位置 // 记录已读消息位置
String key = CharSequenceUtil.join(":", RedisKey.IM_GROUP_READED_POSITION, groupId, session.getUserId()); String key = StrUtil.join(":", RedisKey.IM_GROUP_READED_POSITION, groupId, session.getUserId());
redisTemplate.opsForValue().set(key, message.getId()); redisTemplate.opsForValue().set(key, message.getId());
} }
@ -225,7 +224,7 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro
long stIdx = (page - 1) * size; long stIdx = (page - 1) * size;
// 群聊成员信息 // 群聊成员信息
GroupMember member = groupMemberService.findByGroupAndUserId(groupId, userId); GroupMember member = groupMemberService.findByGroupAndUserId(groupId, userId);
if (member == null || member.getQuit()) { if (Objects.isNull(member) || member.getQuit()) {
throw new GlobalException(ResultCode.PROGRAM_ERROR, "您已不在群聊中"); throw new GlobalException(ResultCode.PROGRAM_ERROR, "您已不在群聊中");
} }
// 查询聊天记录,只查询加入群聊时间之后的消息 // 查询聊天记录,只查询加入群聊时间之后的消息
@ -235,10 +234,7 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro
List<GroupMessage> messages = this.list(wrapper); List<GroupMessage> messages = this.list(wrapper);
List<GroupMessageVO> messageInfos = List<GroupMessageVO> messageInfos =
messages.stream().map(m -> { messages.stream().map(m -> BeanUtils.copyProperties(m, GroupMessageVO.class)).collect(Collectors.toList());
m.setContent(sensitiveFilterUtil.filter(m.getContent()));
return BeanUtils.copyProperties(m, GroupMessageVO.class);
}).collect(Collectors.toList());
log.info("拉取群聊记录,用户id:{},群聊id:{},数量:{}", userId, groupId, messageInfos.size()); log.info("拉取群聊记录,用户id:{},群聊id:{},数量:{}", userId, groupId, messageInfos.size());
return messageInfos; return messageInfos;
} }

12
im-platform/src/main/java/com/bx/implatform/service/impl/PrivateMessageServiceImpl.java

@ -74,7 +74,7 @@ public class PrivateMessageServiceImpl extends ServiceImpl<PrivateMessageMapper,
public void recallMessage(Long id) { public void recallMessage(Long id) {
UserSession session = SessionContext.getSession(); UserSession session = SessionContext.getSession();
PrivateMessage msg = this.getById(id); PrivateMessage msg = this.getById(id);
if (msg == null) { if (Objects.isNull(msg)) {
throw new GlobalException(ResultCode.PROGRAM_ERROR, "消息不存在"); throw new GlobalException(ResultCode.PROGRAM_ERROR, "消息不存在");
} }
if (!msg.getSendId().equals(session.getUserId())) { if (!msg.getSendId().equals(session.getUserId())) {
@ -126,10 +126,7 @@ public class PrivateMessageServiceImpl extends ServiceImpl<PrivateMessageMapper,
.last("limit " + stIdx + "," + size); .last("limit " + stIdx + "," + size);
List<PrivateMessage> messages = this.list(wrapper); List<PrivateMessage> messages = this.list(wrapper);
List<PrivateMessageVO> messageInfos = messages.stream().map(m -> { List<PrivateMessageVO> messageInfos = messages.stream().map(m -> BeanUtils.copyProperties(m, PrivateMessageVO.class)).collect(Collectors.toList());
m.setContent(sensitiveFilterUtil.filter(m.getContent()));
return BeanUtils.copyProperties(m, PrivateMessageVO.class);
}).collect(Collectors.toList());
log.info("拉取聊天记录,用户id:{},好友id:{},数量:{}", userId, friendId, messageInfos.size()); log.info("拉取聊天记录,用户id:{},好友id:{},数量:{}", userId, friendId, messageInfos.size());
return messageInfos; return messageInfos;
} }
@ -171,10 +168,7 @@ public class PrivateMessageServiceImpl extends ServiceImpl<PrivateMessageMapper,
this.update(updateWrapper); this.update(updateWrapper);
} }
log.info("拉取消息,用户id:{},数量:{}", session.getUserId(), messages.size()); log.info("拉取消息,用户id:{},数量:{}", session.getUserId(), messages.size());
return messages.stream().map(m -> { return messages.stream().map(m -> BeanUtils.copyProperties(m, PrivateMessageVO.class)).collect(Collectors.toList());
m.setContent(sensitiveFilterUtil.filter(m.getContent()));
return BeanUtils.copyProperties(m, PrivateMessageVO.class);
}).collect(Collectors.toList());
} }

5
im-platform/src/main/java/com/bx/implatform/util/SensitiveFilterUtil.java

@ -1,5 +1,6 @@
package com.bx.implatform.util; package com.bx.implatform.util;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.CharUtils; import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -22,11 +23,9 @@ import java.util.Map;
*/ */
@Slf4j @Slf4j
@Component @Component
@NoArgsConstructor
public final class SensitiveFilterUtil { public final class SensitiveFilterUtil {
private SensitiveFilterUtil() {
}
/** /**
* 替换符 * 替换符
*/ */

Loading…
Cancel
Save