|
|
@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.bx.imclient.IMClient; |
|
|
import com.bx.imclient.IMClient; |
|
|
import com.bx.imcommon.contant.Constant; |
|
|
import com.bx.imcommon.contant.Constant; |
|
|
import com.bx.imcommon.model.GroupMessageInfo; |
|
|
import com.bx.imcommon.model.GroupMessageInfo; |
|
|
|
|
|
import com.bx.imcommon.model.IMGroupMessage; |
|
|
|
|
|
import com.bx.imcommon.model.IMUserInfo; |
|
|
import com.bx.implatform.contant.RedisKey; |
|
|
import com.bx.implatform.contant.RedisKey; |
|
|
import com.bx.implatform.entity.Group; |
|
|
import com.bx.implatform.entity.Group; |
|
|
import com.bx.implatform.entity.GroupMember; |
|
|
import com.bx.implatform.entity.GroupMember; |
|
|
@ -18,6 +20,7 @@ import com.bx.implatform.service.IGroupMemberService; |
|
|
import com.bx.implatform.service.IGroupMessageService; |
|
|
import com.bx.implatform.service.IGroupMessageService; |
|
|
import com.bx.implatform.service.IGroupService; |
|
|
import com.bx.implatform.service.IGroupService; |
|
|
import com.bx.implatform.session.SessionContext; |
|
|
import com.bx.implatform.session.SessionContext; |
|
|
|
|
|
import com.bx.implatform.session.UserSession; |
|
|
import com.bx.implatform.util.BeanUtils; |
|
|
import com.bx.implatform.util.BeanUtils; |
|
|
import com.bx.implatform.vo.GroupMessageVO; |
|
|
import com.bx.implatform.vo.GroupMessageVO; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
@ -56,7 +59,7 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
*/ |
|
|
*/ |
|
|
@Override |
|
|
@Override |
|
|
public Long sendMessage(GroupMessageVO vo) { |
|
|
public Long sendMessage(GroupMessageVO vo) { |
|
|
Long userId = SessionContext.getSession().getUserId(); |
|
|
UserSession session = SessionContext.getSession(); |
|
|
Group group = groupService.getById(vo.getGroupId()); |
|
|
Group group = groupService.getById(vo.getGroupId()); |
|
|
if(group == null){ |
|
|
if(group == null){ |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"群聊不存在"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"群聊不存在"); |
|
|
@ -66,20 +69,24 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
} |
|
|
} |
|
|
// 判断是否在群里
|
|
|
// 判断是否在群里
|
|
|
List<Long> userIds = groupMemberService.findUserIdsByGroupId(group.getId()); |
|
|
List<Long> userIds = groupMemberService.findUserIdsByGroupId(group.getId()); |
|
|
if(!userIds.contains(userId)){ |
|
|
if(!userIds.contains(session.getUserId())){ |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"您已不在群聊里面,无法发送消息"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"您已不在群聊里面,无法发送消息"); |
|
|
} |
|
|
} |
|
|
// 保存消息
|
|
|
// 保存消息
|
|
|
GroupMessage msg = BeanUtils.copyProperties(vo, GroupMessage.class); |
|
|
GroupMessage msg = BeanUtils.copyProperties(vo, GroupMessage.class); |
|
|
msg.setSendId(userId); |
|
|
msg.setSendId(session.getUserId()); |
|
|
msg.setSendTime(new Date()); |
|
|
msg.setSendTime(new Date()); |
|
|
this.save(msg); |
|
|
this.save(msg); |
|
|
// 不用发给自己
|
|
|
// 不用发给自己
|
|
|
userIds = userIds.stream().filter(id->userId.equals(id)).collect(Collectors.toList()); |
|
|
userIds = userIds.stream().filter(id->!session.getUserId().equals(id)).collect(Collectors.toList()); |
|
|
// 群发
|
|
|
// 群发
|
|
|
GroupMessageInfo msgInfo = BeanUtils.copyProperties(msg, GroupMessageInfo.class); |
|
|
GroupMessageInfo msgInfo = BeanUtils.copyProperties(msg, GroupMessageInfo.class); |
|
|
imClient.sendGroupMessage(userIds,msgInfo); |
|
|
IMGroupMessage sendMessage = new IMGroupMessage(); |
|
|
log.info("发送群聊消息,发送id:{},群聊id:{},内容:{}",userId,vo.getGroupId(),vo.getContent()); |
|
|
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()); |
|
|
return msg.getId(); |
|
|
return msg.getId(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -93,19 +100,19 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
*/ |
|
|
*/ |
|
|
@Override |
|
|
@Override |
|
|
public void recallMessage(Long id) { |
|
|
public void recallMessage(Long id) { |
|
|
Long userId = SessionContext.getSession().getUserId(); |
|
|
UserSession session = SessionContext.getSession(); |
|
|
GroupMessage msg = this.getById(id); |
|
|
GroupMessage msg = this.getById(id); |
|
|
if(msg == null){ |
|
|
if(msg == null){ |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"消息不存在"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"消息不存在"); |
|
|
} |
|
|
} |
|
|
if(!msg.getSendId().equals(userId)){ |
|
|
if(!msg.getSendId().equals(session.getUserId())){ |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"这条消息不是由您发送,无法撤回"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"这条消息不是由您发送,无法撤回"); |
|
|
} |
|
|
} |
|
|
if(System.currentTimeMillis() - msg.getSendTime().getTime() > Constant.ALLOW_RECALL_SECOND * 1000){ |
|
|
if(System.currentTimeMillis() - msg.getSendTime().getTime() > Constant.ALLOW_RECALL_SECOND * 1000){ |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"消息已发送超过5分钟,无法撤回"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"消息已发送超过5分钟,无法撤回"); |
|
|
} |
|
|
} |
|
|
// 判断是否在群里
|
|
|
// 判断是否在群里
|
|
|
GroupMember member = groupMemberService.findByGroupAndUserId(msg.getGroupId(),userId); |
|
|
GroupMember member = groupMemberService.findByGroupAndUserId(msg.getGroupId(),session.getUserId()); |
|
|
if(member == null){ |
|
|
if(member == null){ |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"您已不在群聊里面,无法撤回消息"); |
|
|
throw new GlobalException(ResultCode.PROGRAM_ERROR,"您已不在群聊里面,无法撤回消息"); |
|
|
} |
|
|
} |
|
|
@ -115,14 +122,20 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
// 群发
|
|
|
// 群发
|
|
|
List<Long> userIds = groupMemberService.findUserIdsByGroupId(msg.getGroupId()); |
|
|
List<Long> userIds = groupMemberService.findUserIdsByGroupId(msg.getGroupId()); |
|
|
// 不用发给自己
|
|
|
// 不用发给自己
|
|
|
userIds = userIds.stream().filter(uid->userId.equals(uid)).collect(Collectors.toList()); |
|
|
userIds = userIds.stream().filter(uid->!session.getUserId().equals(uid)).collect(Collectors.toList()); |
|
|
GroupMessageInfo msgInfo = BeanUtils.copyProperties(msg, GroupMessageInfo.class); |
|
|
GroupMessageInfo msgInfo = BeanUtils.copyProperties(msg, GroupMessageInfo.class); |
|
|
msgInfo.setType(MessageType.TIP.code()); |
|
|
msgInfo.setType(MessageType.TIP.code()); |
|
|
String content = String.format("'%s'撤回了一条消息",member.getAliasName()); |
|
|
String content = String.format("'%s'撤回了一条消息",member.getAliasName()); |
|
|
msgInfo.setContent(content); |
|
|
msgInfo.setContent(content); |
|
|
msgInfo.setSendTime(new Date()); |
|
|
msgInfo.setSendTime(new Date()); |
|
|
imClient.sendGroupMessage(userIds,msgInfo); |
|
|
|
|
|
log.info("撤回群聊消息,发送id:{},群聊id:{},内容:{}",userId,msg.getGroupId(),msg.getContent()); |
|
|
IMGroupMessage sendMessage = new IMGroupMessage(); |
|
|
|
|
|
sendMessage.setSender(new IMUserInfo(session.getUserId(),session.getTerminal())); |
|
|
|
|
|
sendMessage.setRecvIds(userIds); |
|
|
|
|
|
sendMessage.setData(msgInfo); |
|
|
|
|
|
sendMessage.setSendResult(false); |
|
|
|
|
|
imClient.sendGroupMessage(sendMessage); |
|
|
|
|
|
log.info("撤回群聊消息,发送id:{},群聊id:{},内容:{}",session.getUserId(),msg.getGroupId(),msg.getContent()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -133,18 +146,16 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
*/ |
|
|
*/ |
|
|
@Override |
|
|
@Override |
|
|
public void pullUnreadMessage() { |
|
|
public void pullUnreadMessage() { |
|
|
Long userId = SessionContext.getSession().getUserId(); |
|
|
UserSession session = SessionContext.getSession(); |
|
|
List<Long> recvIds = new LinkedList(); |
|
|
List<GroupMember> members = groupMemberService.findByUserId(session.getUserId()); |
|
|
recvIds.add(userId); |
|
|
|
|
|
List<GroupMember> members = groupMemberService.findByUserId(userId); |
|
|
|
|
|
for(GroupMember member:members){ |
|
|
for(GroupMember member:members){ |
|
|
// 获取群聊已读的最大消息id,只推送未读消息
|
|
|
// 获取群聊已读的最大消息id,只推送未读消息
|
|
|
String key = RedisKey.IM_GROUP_READED_POSITION + member.getGroupId()+":"+userId; |
|
|
String key = String.join(":",RedisKey.IM_GROUP_READED_POSITION,member.getGroupId().toString(),session.getUserId().toString()); |
|
|
Integer maxReadedId = (Integer)redisTemplate.opsForValue().get(key); |
|
|
Integer maxReadedId = (Integer)redisTemplate.opsForValue().get(key); |
|
|
QueryWrapper<GroupMessage> wrapper = new QueryWrapper(); |
|
|
QueryWrapper<GroupMessage> wrapper = new QueryWrapper(); |
|
|
wrapper.lambda().eq(GroupMessage::getGroupId,member.getGroupId()) |
|
|
wrapper.lambda().eq(GroupMessage::getGroupId,member.getGroupId()) |
|
|
.gt(GroupMessage::getSendTime,member.getCreatedTime()) |
|
|
.gt(GroupMessage::getSendTime,member.getCreatedTime()) |
|
|
.ne(GroupMessage::getSendId, userId) |
|
|
.ne(GroupMessage::getSendId, session.getUserId()) |
|
|
.ne(GroupMessage::getStatus, MessageStatus.RECALL.code()); |
|
|
.ne(GroupMessage::getStatus, MessageStatus.RECALL.code()); |
|
|
if(maxReadedId!=null){ |
|
|
if(maxReadedId!=null){ |
|
|
wrapper.lambda().gt(GroupMessage::getId,maxReadedId); |
|
|
wrapper.lambda().gt(GroupMessage::getId,maxReadedId); |
|
|
@ -154,15 +165,19 @@ public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, Gro |
|
|
if(messages.isEmpty()){ |
|
|
if(messages.isEmpty()){ |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
// 组装消息,准备推送
|
|
|
// 推送
|
|
|
List<GroupMessageInfo> messageInfos = messages.stream().map(m->{ |
|
|
for (GroupMessage message:messages ){ |
|
|
GroupMessageInfo msgInfo = BeanUtils.copyProperties(m, GroupMessageInfo.class); |
|
|
GroupMessageInfo msgInfo = BeanUtils.copyProperties(message, GroupMessageInfo.class); |
|
|
return msgInfo; |
|
|
IMGroupMessage sendMessage = new IMGroupMessage(); |
|
|
}).collect(Collectors.toList()); |
|
|
sendMessage.setSender(new IMUserInfo(session.getUserId(),session.getTerminal())); |
|
|
|
|
|
// 只推给自己当前终端
|
|
|
|
|
|
sendMessage.setRecvIds(Collections.singletonList(session.getUserId())); |
|
|
|
|
|
sendMessage.setRecvTerminals(Collections.singletonList(session.getTerminal())); |
|
|
|
|
|
sendMessage.setData(msgInfo); |
|
|
|
|
|
imClient.sendGroupMessage(sendMessage); |
|
|
|
|
|
} |
|
|
// 发送消息
|
|
|
// 发送消息
|
|
|
GroupMessageInfo[] infoArr = messageInfos.toArray(new GroupMessageInfo[messageInfos.size()]); |
|
|
log.info("拉取未读群聊消息,用户id:{},群聊id:{},数量:{}",session.getUserId(),member.getGroupId(),messages.size()); |
|
|
imClient.sendGroupMessage(Collections.singletonList(userId), infoArr); |
|
|
|
|
|
log.info("拉取未读群聊消息,用户id:{},群聊id:{},数量:{}",userId,member.getGroupId(),messageInfos.size()); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|