Browse Source

群聊功能开发中

master
xie.bx 3 years ago
parent
commit
b9e5052cba
  1. 16
      im-platform/src/main/java/com/lx/implatform/controller/PrivateMessageController.java
  2. 12
      im-platform/src/main/java/com/lx/implatform/entity/PrivateMessage.java
  3. 4
      im-platform/src/main/java/com/lx/implatform/mapper/PrivateMessageMapper.java
  4. 21
      im-platform/src/main/java/com/lx/implatform/service/IPrivateMessageService.java
  5. 21
      im-platform/src/main/java/com/lx/implatform/service/ISingleMessageService.java
  6. 36
      im-platform/src/main/java/com/lx/implatform/service/impl/PrivateMessageServiceImpl.java
  7. 14
      im-platform/src/main/java/com/lx/implatform/task/PullAlreadyReadMessageTask.java
  8. 4
      im-platform/src/main/java/com/lx/implatform/vo/PrivateMessageVO.java
  9. 6
      im-platform/src/main/resources/db/db.sql

16
im-platform/src/main/java/com/lx/implatform/controller/SingleMessageController.java → im-platform/src/main/java/com/lx/implatform/controller/PrivateMessageController.java

@ -3,8 +3,8 @@ package com.lx.implatform.controller;
import com.lx.common.result.Result;
import com.lx.common.result.ResultUtils;
import com.lx.implatform.service.ISingleMessageService;
import com.lx.implatform.vo.SingleMessageVO;
import com.lx.implatform.service.IPrivateMessageService;
import com.lx.implatform.vo.PrivateMessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
@ -17,23 +17,23 @@ import javax.validation.Valid;
@Api(tags = "私聊消息")
@RestController
@RequestMapping("/message/single")
public class SingleMessageController {
@RequestMapping("/message/private")
public class PrivateMessageController {
@Autowired
private ISingleMessageService singleMessageService;
private IPrivateMessageService privateMessageService;
@PostMapping("/send")
@ApiOperation(value = "发送消息",notes="发送单人消息")
public Result register(@Valid @RequestBody SingleMessageVO vo){
singleMessageService.sendMessage(vo);
public Result register(@Valid @RequestBody PrivateMessageVO vo){
privateMessageService.sendMessage(vo);
return ResultUtils.success();
}
@PostMapping("/pullUnreadMessage")
@ApiOperation(value = "拉取未读消息",notes="拉取未读消息")
public Result pullUnreadMessage(){
singleMessageService.pullUnreadMessage();
privateMessageService.pullUnreadMessage();
return ResultUtils.success();
}
}

12
im-platform/src/main/java/com/lx/implatform/entity/SingleMessage.java → im-platform/src/main/java/com/lx/implatform/entity/PrivateMessage.java

@ -21,8 +21,8 @@ import java.util.Date;
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("im_single_message")
public class SingleMessage extends Model<SingleMessage> {
@TableName("im_private_message")
public class PrivateMessage extends Model<PrivateMessage> {
private static final long serialVersionUID=1L;
@ -35,14 +35,14 @@ public class SingleMessage extends Model<SingleMessage> {
/**
* 发送用户id
*/
@TableField("send_user_id")
private Long sendUserId;
@TableField("send_id")
private Long sendId;
/**
* 接收用户id
*/
@TableField("recv_user_id")
private Long recvUserId;
@TableField("recv_id")
private Long recvId;
/**
* 发送内容

4
im-platform/src/main/java/com/lx/implatform/mapper/SingleMessageMapper.java → im-platform/src/main/java/com/lx/implatform/mapper/PrivateMessageMapper.java

@ -1,7 +1,7 @@
package com.lx.implatform.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lx.implatform.entity.SingleMessage;
import com.lx.implatform.entity.PrivateMessage;
/**
* <p>
@ -11,6 +11,6 @@ import com.lx.implatform.entity.SingleMessage;
* @author blue
* @since 2022-10-01
*/
public interface SingleMessageMapper extends BaseMapper<SingleMessage> {
public interface PrivateMessageMapper extends BaseMapper<PrivateMessage> {
}

21
im-platform/src/main/java/com/lx/implatform/service/IPrivateMessageService.java

@ -0,0 +1,21 @@
package com.lx.implatform.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lx.implatform.entity.PrivateMessage;
import com.lx.implatform.vo.PrivateMessageVO;
/**
* <p>
* 服务类
* </p>
*
* @author blue
* @since 2022-10-01
*/
public interface IPrivateMessageService extends IService<PrivateMessage> {
void sendMessage(PrivateMessageVO vo);
void pullUnreadMessage();
}

21
im-platform/src/main/java/com/lx/implatform/service/ISingleMessageService.java

@ -1,21 +0,0 @@
package com.lx.implatform.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lx.implatform.entity.SingleMessage;
import com.lx.implatform.vo.SingleMessageVO;
/**
* <p>
* 服务类
* </p>
*
* @author blue
* @since 2022-10-01
*/
public interface ISingleMessageService extends IService<SingleMessage> {
void sendMessage(SingleMessageVO vo);
void pullUnreadMessage();
}

36
im-platform/src/main/java/com/lx/implatform/service/impl/SingleMessageServiceImpl.java → im-platform/src/main/java/com/lx/implatform/service/impl/PrivateMessageServiceImpl.java

@ -5,15 +5,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lx.common.contant.RedisKey;
import com.lx.common.enums.MessageStatusEnum;
import com.lx.common.enums.ResultCode;
import com.lx.common.model.im.SingleMessageInfo;
import com.lx.common.model.im.PrivateMessageInfo;
import com.lx.common.util.BeanUtils;
import com.lx.implatform.entity.SingleMessage;
import com.lx.implatform.entity.PrivateMessage;
import com.lx.implatform.exception.GlobalException;
import com.lx.implatform.mapper.SingleMessageMapper;
import com.lx.implatform.mapper.PrivateMessageMapper;
import com.lx.implatform.service.IFriendService;
import com.lx.implatform.service.ISingleMessageService;
import com.lx.implatform.service.IPrivateMessageService;
import com.lx.implatform.session.SessionContext;
import com.lx.implatform.vo.SingleMessageVO;
import com.lx.implatform.vo.PrivateMessageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
@ -25,7 +25,7 @@ import java.util.stream.Collectors;
@Service
public class SingleMessageServiceImpl extends ServiceImpl<SingleMessageMapper, SingleMessage> implements ISingleMessageService {
public class PrivateMessageServiceImpl extends ServiceImpl<PrivateMessageMapper, PrivateMessage> implements IPrivateMessageService {
@Autowired
private IFriendService friendService;
@ -33,25 +33,25 @@ public class SingleMessageServiceImpl extends ServiceImpl<SingleMessageMapper, S
private RedisTemplate<String, Object> redisTemplate;
@Override
public void sendMessage(SingleMessageVO vo) {
public void sendMessage(PrivateMessageVO vo) {
Long userId = SessionContext.getSession().getId();
Boolean isFriends = friendService.isFriend(userId,vo.getRecvUserId());
Boolean isFriends = friendService.isFriend(userId,vo.getRecvId());
if(!isFriends){
throw new GlobalException(ResultCode.PROGRAM_ERROR,"您已不是对方好友,无法发送消息");
}
// 保存消息
SingleMessage msg = BeanUtils.copyProperties(vo,SingleMessage.class);
msg.setSendUserId(userId);
PrivateMessage msg = BeanUtils.copyProperties(vo, PrivateMessage.class);
msg.setSendId(userId);
msg.setStatus(MessageStatusEnum.UNREAD.getCode());
msg.setSendTime(new Date());
this.save(msg);
// 获取对方连接的channelId
String key = RedisKey.IM_USER_SERVER_ID+msg.getRecvUserId();
String key = RedisKey.IM_USER_SERVER_ID+msg.getRecvId();
String serverId = (String)redisTemplate.opsForValue().get(key);
// 如果对方在线,将数据存储至redis,等待拉取推送
if(!StringUtils.isEmpty(serverId)){
String sendKey = RedisKey.IM_UNREAD_MESSAGE + serverId;
SingleMessageInfo msgInfo = BeanUtils.copyProperties(msg,SingleMessageInfo.class);
PrivateMessageInfo msgInfo = BeanUtils.copyProperties(msg, PrivateMessageInfo.class);
redisTemplate.opsForList().rightPush(sendKey,msgInfo);
}
}
@ -66,14 +66,14 @@ public class SingleMessageServiceImpl extends ServiceImpl<SingleMessageMapper, S
throw new GlobalException(ResultCode.PROGRAM_ERROR,"用户未建立连接");
}
// 获取当前用户所有未读消息
QueryWrapper<SingleMessage> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(SingleMessage::getRecvUserId,userId)
.eq(SingleMessage::getStatus,MessageStatusEnum.UNREAD);
List<SingleMessage> messages = this.list(queryWrapper);
QueryWrapper<PrivateMessage> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(PrivateMessage::getRecvId,userId)
.eq(PrivateMessage::getStatus,MessageStatusEnum.UNREAD);
List<PrivateMessage> messages = this.list(queryWrapper);
// 上传至redis,等待推送
if(!messages.isEmpty()){
List<SingleMessageInfo> infos = messages.stream().map(m->{
SingleMessageInfo msgInfo = BeanUtils.copyProperties(m,SingleMessageInfo.class);
List<PrivateMessageInfo> infos = messages.stream().map(m->{
PrivateMessageInfo msgInfo = BeanUtils.copyProperties(m, PrivateMessageInfo.class);
return msgInfo;
}).collect(Collectors.toList());
String sendKey = RedisKey.IM_UNREAD_MESSAGE + serverId;

14
im-platform/src/main/java/com/lx/implatform/task/PullAlreadyReadMessageTask.java

@ -3,8 +3,8 @@ package com.lx.implatform.task;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lx.common.contant.RedisKey;
import com.lx.common.enums.MessageStatusEnum;
import com.lx.implatform.entity.SingleMessage;
import com.lx.implatform.service.ISingleMessageService;
import com.lx.implatform.entity.PrivateMessage;
import com.lx.implatform.service.IPrivateMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
@ -27,7 +27,7 @@ public class PullAlreadyReadMessageTask {
private RedisTemplate<String,Object> redisTemplate;
@Autowired
private ISingleMessageService singleMessageService;
private IPrivateMessageService privateMessageService;
@PostConstruct
public void init(){
@ -43,10 +43,10 @@ public class PullAlreadyReadMessageTask {
String key = RedisKey.IM_ALREADY_READED_MESSAGE;
Integer msgId = (Integer)redisTemplate.opsForList().leftPop(key,1, TimeUnit.SECONDS);
if(msgId!=null){
UpdateWrapper<SingleMessage> updateWrapper = new UpdateWrapper<>();
updateWrapper.lambda().eq(SingleMessage::getId,msgId)
.set(SingleMessage::getStatus, MessageStatusEnum.ALREADY_READ.getCode());
singleMessageService.update(updateWrapper);
UpdateWrapper<PrivateMessage> updateWrapper = new UpdateWrapper<>();
updateWrapper.lambda().eq(PrivateMessage::getId,msgId)
.set(PrivateMessage::getStatus, MessageStatusEnum.ALREADY_READ.getCode());
privateMessageService.update(updateWrapper);
log.info("消息已读,id:{}",msgId);
}
}catch (Exception e){

4
im-platform/src/main/java/com/lx/implatform/vo/SingleMessageVO.java → im-platform/src/main/java/com/lx/implatform/vo/PrivateMessageVO.java

@ -11,12 +11,12 @@ import javax.validation.constraints.NotNull;
@Data
@ApiModel("单发消息VO")
public class SingleMessageVO {
public class PrivateMessageVO {
@NotNull(message="接收用户id不可为空")
@ApiModelProperty(value = "接收用户id")
private Long recvUserId;
private Long recvId;
@Length(max=1024,message = "内容长度不得大于1024")

6
im-platform/src/main/resources/db/db.sql

@ -25,10 +25,10 @@ create table `im_friend`(
key `idx_friend_id` (`friend_id`)
) ENGINE=InnoDB CHARSET=utf8mb3 comment '好友';
create table `im_single_message`(
create table `im_private_message`(
`id` bigint not null auto_increment primary key comment 'id',
`send_user_id` bigint not null comment '发送用户id',
`recv_user_id` bigint not null comment '接收用户id',
`send_id` bigint not null comment '发送用户id',
`recv_id` bigint not null comment '接收用户id',
`content` text comment '发送内容',
`type` tinyint(1) NOT NULL comment '消息类型 0:文字 1:图片 2:文件',
`status` tinyint(1) NOT NULL comment '状态 0:未读 1:已读 ',

Loading…
Cancel
Save