|
|
|
@ -1,10 +1,12 @@ |
|
|
|
package com.bx.implatform.service.impl; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import com.bx.imclient.IMClient; |
|
|
|
import com.bx.imcommon.contant.Constant; |
|
|
|
import com.bx.imcommon.model.GroupMessageInfo; |
|
|
|
import com.bx.imcommon.contant.IMConstant; |
|
|
|
import com.bx.implatform.vo.GroupMessageVO; |
|
|
|
import com.bx.imcommon.model.IMGroupMessage; |
|
|
|
import com.bx.imcommon.model.IMUserInfo; |
|
|
|
import com.bx.implatform.contant.RedisKey; |
|
|
|
@ -22,7 +24,7 @@ import com.bx.implatform.service.IGroupService; |
|
|
|
import com.bx.implatform.session.SessionContext; |
|
|
|
import com.bx.implatform.session.UserSession; |
|
|
|
import com.bx.implatform.util.BeanUtils; |
|
|
|
import com.bx.implatform.vo.GroupMessageVO; |
|
|
|
import com.bx.implatform.dto.GroupMessageDTO; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
@ -30,37 +32,31 @@ import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
|
public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, GroupMessage> implements IGroupMessageService { |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IGroupService groupService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IGroupMemberService groupMemberService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RedisTemplate<String,Object> redisTemplate; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IMClient imClient; |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送群聊消息(与mysql所有交换都要进行缓存) |
|
|
|
* |
|
|
|
* @param vo |
|
|
|
* @param dto 群聊消息 |
|
|
|
* @return 群聊id |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public Long sendMessage(GroupMessageVO vo) { |
|
|
|
public Long sendMessage(GroupMessageDTO dto) { |
|
|
|
UserSession session = SessionContext.getSession(); |
|
|
|
Group group = groupService.getById(vo.getGroupId()); |
|
|
|
Group group = groupService.getById(dto.getGroupId()); |
|
|
|
if(group == null){ |
|
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"群聊不存在"); |
|
|
|
} |
|
|
|
@ -73,20 +69,20 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"您已不在群聊里面,无法发送消息"); |
|
|
|
} |
|
|
|
// 保存消息
|
|
|
|
GroupMessage msg = BeanUtils.copyProperties(vo, GroupMessage.class); |
|
|
|
GroupMessage msg = BeanUtils.copyProperties(dto, GroupMessage.class); |
|
|
|
msg.setSendId(session.getUserId()); |
|
|
|
msg.setSendTime(new Date()); |
|
|
|
this.save(msg); |
|
|
|
// 不用发给自己
|
|
|
|
userIds = userIds.stream().filter(id->!session.getUserId().equals(id)).collect(Collectors.toList()); |
|
|
|
// 群发
|
|
|
|
GroupMessageInfo msgInfo = BeanUtils.copyProperties(msg, GroupMessageInfo.class); |
|
|
|
IMGroupMessage sendMessage = new IMGroupMessage(); |
|
|
|
GroupMessageVO msgInfo = BeanUtils.copyProperties(msg, GroupMessageVO.class); |
|
|
|
IMGroupMessage<GroupMessageVO> sendMessage = new IMGroupMessage<>(); |
|
|
|
sendMessage.setSender(new IMUserInfo(session.getUserId(),session.getTerminal())); |
|
|
|
sendMessage.setRecvIds(userIds); |
|
|
|
sendMessage.setData(msgInfo); |
|
|
|
imClient.sendGroupMessage(sendMessage); |
|
|
|
log.info("发送群聊消息,发送id:{},群聊id:{},内容:{}",session.getUserId(),vo.getGroupId(),vo.getContent()); |
|
|
|
log.info("发送群聊消息,发送id:{},群聊id:{},内容:{}",session.getUserId(),dto.getGroupId(),dto.getContent()); |
|
|
|
return msg.getId(); |
|
|
|
} |
|
|
|
|
|
|
|
@ -108,7 +104,7 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
|
if(!msg.getSendId().equals(session.getUserId())){ |
|
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"这条消息不是由您发送,无法撤回"); |
|
|
|
} |
|
|
|
if(System.currentTimeMillis() - msg.getSendTime().getTime() > Constant.ALLOW_RECALL_SECOND * 1000){ |
|
|
|
if(System.currentTimeMillis() - msg.getSendTime().getTime() > IMConstant.ALLOW_RECALL_SECOND * 1000){ |
|
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"消息已发送超过5分钟,无法撤回"); |
|
|
|
} |
|
|
|
// 判断是否在群里
|
|
|
|
@ -123,17 +119,25 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
|
List<Long> userIds = groupMemberService.findUserIdsByGroupId(msg.getGroupId()); |
|
|
|
// 不用发给自己
|
|
|
|
userIds = userIds.stream().filter(uid->!session.getUserId().equals(uid)).collect(Collectors.toList()); |
|
|
|
GroupMessageInfo msgInfo = BeanUtils.copyProperties(msg, GroupMessageInfo.class); |
|
|
|
msgInfo.setType(MessageType.TIP.code()); |
|
|
|
GroupMessageVO msgInfo = BeanUtils.copyProperties(msg, GroupMessageVO.class); |
|
|
|
msgInfo.setType(MessageType.RECALL.code()); |
|
|
|
String content = String.format("'%s'撤回了一条消息",member.getAliasName()); |
|
|
|
msgInfo.setContent(content); |
|
|
|
msgInfo.setSendTime(new Date()); |
|
|
|
|
|
|
|
IMGroupMessage sendMessage = new IMGroupMessage(); |
|
|
|
IMGroupMessage<GroupMessageVO> sendMessage = new IMGroupMessage<>(); |
|
|
|
sendMessage.setSender(new IMUserInfo(session.getUserId(),session.getTerminal())); |
|
|
|
sendMessage.setRecvIds(userIds); |
|
|
|
sendMessage.setData(msgInfo); |
|
|
|
sendMessage.setSendResult(false); |
|
|
|
sendMessage.setSendToSelf(false); |
|
|
|
imClient.sendGroupMessage(sendMessage); |
|
|
|
|
|
|
|
// 推给自己其他终端
|
|
|
|
msgInfo.setContent("你撤回了一条消息"); |
|
|
|
sendMessage.setSendToSelf(true); |
|
|
|
sendMessage.setRecvIds(Collections.emptyList()); |
|
|
|
sendMessage.setRecvTerminals(Collections.emptyList()); |
|
|
|
imClient.sendGroupMessage(sendMessage); |
|
|
|
log.info("撤回群聊消息,发送id:{},群聊id:{},内容:{}",session.getUserId(),msg.getGroupId(),msg.getContent()); |
|
|
|
} |
|
|
|
@ -142,7 +146,6 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
|
/** |
|
|
|
* 异步拉取群聊消息,通过websocket异步推送 |
|
|
|
* |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void pullUnreadMessage() { |
|
|
|
@ -152,13 +155,13 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
|
// 获取群聊已读的最大消息id,只推送未读消息
|
|
|
|
String key = String.join(":",RedisKey.IM_GROUP_READED_POSITION,member.getGroupId().toString(),session.getUserId().toString()); |
|
|
|
Integer maxReadedId = (Integer)redisTemplate.opsForValue().get(key); |
|
|
|
QueryWrapper<GroupMessage> wrapper = new QueryWrapper(); |
|
|
|
wrapper.lambda().eq(GroupMessage::getGroupId,member.getGroupId()) |
|
|
|
LambdaQueryWrapper<GroupMessage> wrapper = Wrappers.lambdaQuery(); |
|
|
|
wrapper.eq(GroupMessage::getGroupId,member.getGroupId()) |
|
|
|
.gt(GroupMessage::getSendTime,member.getCreatedTime()) |
|
|
|
.ne(GroupMessage::getSendId, session.getUserId()) |
|
|
|
.ne(GroupMessage::getStatus, MessageStatus.RECALL.code()); |
|
|
|
if(maxReadedId!=null){ |
|
|
|
wrapper.lambda().gt(GroupMessage::getId,maxReadedId); |
|
|
|
wrapper.gt(GroupMessage::getId,maxReadedId); |
|
|
|
} |
|
|
|
wrapper.last("limit 100"); |
|
|
|
List<GroupMessage> messages = this.list(wrapper); |
|
|
|
@ -167,8 +170,8 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
|
} |
|
|
|
// 推送
|
|
|
|
for (GroupMessage message:messages ){ |
|
|
|
GroupMessageInfo msgInfo = BeanUtils.copyProperties(message, GroupMessageInfo.class); |
|
|
|
IMGroupMessage sendMessage = new IMGroupMessage(); |
|
|
|
GroupMessageVO msgInfo = BeanUtils.copyProperties(message, GroupMessageVO.class); |
|
|
|
IMGroupMessage<GroupMessageVO> sendMessage = new IMGroupMessage<>(); |
|
|
|
sendMessage.setSender(new IMUserInfo(session.getUserId(),session.getTerminal())); |
|
|
|
// 只推给自己当前终端
|
|
|
|
sendMessage.setRecvIds(Collections.singletonList(session.getUserId())); |
|
|
|
@ -191,11 +194,11 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
|
* @return 聊天记录列表 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public List<GroupMessageInfo> findHistoryMessage(Long groupId, Long page, Long size) { |
|
|
|
public List<GroupMessageVO> findHistoryMessage(Long groupId, Long page, Long size) { |
|
|
|
page = page > 0 ? page:1; |
|
|
|
size = size > 0 ? size:10; |
|
|
|
Long userId = SessionContext.getSession().getUserId(); |
|
|
|
Long stIdx = (page-1)* size; |
|
|
|
long stIdx = (page-1)* size; |
|
|
|
// 群聊成员信息
|
|
|
|
GroupMember member = groupMemberService.findByGroupAndUserId(groupId,userId); |
|
|
|
if(member == null || member.getQuit()){ |
|
|
|
@ -210,10 +213,7 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
|
.last("limit "+stIdx + ","+size); |
|
|
|
|
|
|
|
List<GroupMessage> messages = this.list(wrapper); |
|
|
|
List<GroupMessageInfo> messageInfos = messages.stream().map(m->{ |
|
|
|
GroupMessageInfo info = BeanUtils.copyProperties(m, GroupMessageInfo.class); |
|
|
|
return info; |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
List<GroupMessageVO> messageInfos = messages.stream().map(m->BeanUtils.copyProperties(m, GroupMessageVO.class)).collect(Collectors.toList()); |
|
|
|
log.info("拉取群聊记录,用户id:{},群聊id:{},数量:{}",userId,groupId,messageInfos.size()); |
|
|
|
return messageInfos; |
|
|
|
} |
|
|
|
|