SpringBoot+WebSocket实现即时通讯(四)

news2025/1/16 13:41:35

前言

紧接着上文《SpringBoot+WebSocket实现即时通讯(三)》

本博客姊妹篇

  • SpringBoot+WebSocket实现即时通讯(一)
  • SpringBoot+WebSocket实现即时通讯(二)
  • SpringBoot+WebSocket实现即时通讯(三)
  • SpringBoot+WebSocket实现即时通讯(四)

一、功能描述

  • 用户管理:业务自己实现,暂从数据库添加
  • 好友管理:添加好友、删除好友、修改备注、好友列表等
  • 群组管理:新建群、解散群、编辑群、变更群主、拉人进群、踢出群等
  • 聊天模式:私聊、群聊
  • 消息类型:系统、文本、语音、图片、视频
  • 聊天管理:删除聊天、置顶聊天、查看聊天记录等

二、消息、聊天会话功能实现

2.1 消息

mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qiangesoft.im.mapper.ImMessageMapper">

    <select id="listMessage" resultType="com.qiangesoft.im.pojo.vo.ImMessageVO">
        SELECT
        b.id,
        b.sender_id,
        b.message_type,
        b.message,
        b.create_time,
        c.read_flag
        FROM
        im_chat a
        INNER JOIN im_message b ON a.id = b.chat_id
        INNER JOIN im_message_receiver c ON b.id = c.message_id
        WHERE
        a.del_flag = FALSE
        AND a.id = #{chatId}
        AND c.receiver_id = #{userId}
        <if test="messageType != null and messageType != ''">
            AND b.message_type = #{messageType}
        </if>
        <if test="message != null and message != ''">
            AND b.message like concat('%', #{message}, '%')
        </if>
        ORDER BY b.id DESC
    </select>

    <select id="listUnreadMessage" resultType="com.qiangesoft.im.pojo.vo.ImMessageVO">
        SELECT
        b.id,
        b.sender_id,
        b.message_type,
        b.message,
        b.create_time,
        c.read_flag
        FROM
        im_chat a
        INNER JOIN im_message b ON a.id = b.chat_id
        INNER JOIN im_message_receiver c ON b.id = c.message_id
        WHERE
        a.del_flag = FALSE
        AND a.id = #{chatId}
        AND c.receiver_id = #{userId}
        AND c.read_flag = FALSE
        ORDER BY b.id DESC
    </select>

    <select id="listChatUnreadMessage" resultType="com.qiangesoft.im.pojo.bo.ImChatMessageBO">
        SELECT
        a.chat_id AS chatId,
        COUNT(a.id) AS unreadNum
        FROM
        im_message a INNER JOIN
        im_message_receiver b ON a.id=b.message_id
        WHERE
        b.receiver_id=#{userId}
        AND b.read_flag=FALSE
        AND a.chat_id IN
        <foreach collection="chatIdList" item="chatId" open="(" separator="," close=")">
            #{chatId}
        </foreach>
        GROUP BY a.chat_id
    </select>

    <select id="listLatestMessage" resultType="com.qiangesoft.im.entity.ImMessage">
        SELECT * FROM im_message WHERE id IN (SELECT
        max(a.id)
        FROM
        im_message a INNER JOIN
        im_message_receiver b ON a.id=b.message_id
        WHERE
        b.receiver_id=#{userId}
        AND a.chat_id IN
        <foreach collection="chatIdList" item="chatId" open="(" separator="," close=")">
            #{chatId}
        </foreach>
        GROUP BY a.chat_id)
    </select>

</mapper>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qiangesoft.im.mapper.ImMessageReceiverMapper">

    <update id="updateRead">
        UPDATE im_message_receiver a
        INNER JOIN im_message b
        ON b.id = a.message_id
        INNER JOIN im_chat c ON c.id = b.chat_id
        SET read_flag = 1
        WHERE
        a.read_flag = 0
        AND a.receiver_id = #{userId}
        AND c.id = #{chatId}
        <if test="messageId != null">
            AND b.id = #{messageId}
        </if>
    </update>
</mapper>

package com.qiangesoft.im.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiangesoft.im.entity.ImMessage;
import com.qiangesoft.im.pojo.bo.ImChatMessageBO;
import com.qiangesoft.im.pojo.vo.ImMessageVO;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * <p>
 * 消息 Mapper 接口
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface ImMessageMapper extends BaseMapper<ImMessage> {

    /**
     * 消息列表
     *
     * @param page
     * @param userId
     * @param chatId
     * @param messageType
     * @param message
     * @return
     */
    IPage<ImMessageVO> listMessage(@Param("page") IPage<ImMessage> page, @Param("userId") Long userId, @Param("chatId") Long chatId,
                                   @Param("messageType") String messageType, @Param("message") String message);

    /**
     * 未读消息列表
     *
     * @param userId
     * @param chatId
     * @return
     */
    List<ImMessageVO> listUnreadMessage(@Param("userId") Long userId, @Param("chatId") Long chatId);

    /**
     * 会话未读消息
     *
     * @param userId
     * @param chatIdList
     * @return
     */
    List<ImChatMessageBO> listChatUnreadMessage(@Param("userId") Long userId, @Param("chatIdList") List<Long> chatIdList);

    /**
     * 会话最新消息
     *
     * @param userId
     * @param chatIdList
     * @return
     */
    List<ImMessage> listLatestMessage(@Param("userId") Long userId, @Param("chatIdList") List<Long> chatIdList);
}

package com.qiangesoft.im.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiangesoft.im.entity.ImMessageReceiver;
import org.apache.ibatis.annotations.Param;

/**
 * <p>
 * 群用户消息关系 Mapper 接口
 * </p>
 *
 * @author qiangesoft
 * @since 2023-08-23
 */
public interface ImMessageReceiverMapper extends BaseMapper<ImMessageReceiver> {

    /**
     * 置为已读
     *
     * @param userId
     * @param chatId
     * @param messageId
     */
    void updateRead(@Param("userId") Long userId, @Param("chatId") Long chatId, @Param("messageId") Long messageId);
}

service

package com.qiangesoft.im.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qiangesoft.im.entity.ImMessage;
import com.qiangesoft.im.pojo.bo.ImChatMessageBO;
import com.qiangesoft.im.pojo.dto.ImMessageDTO;
import com.qiangesoft.im.pojo.dto.query.MessageQueryDTO;
import com.qiangesoft.im.pojo.dto.query.PageQueryDTO;
import com.qiangesoft.im.pojo.vo.ImMessageVO;
import com.qiangesoft.im.pojo.vo.PageResultVO;

import java.util.List;

/**
 * <p>
 * 消息 服务类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface IImMessageService extends IService<ImMessage> {

    /**
     * 消息列表
     *
     * @param pageQuery
     * @param messageQuery
     * @return
     */
    PageResultVO<ImMessageVO> listMessage(PageQueryDTO pageQuery, MessageQueryDTO messageQuery);

    /**
     * 未读消息列表
     *
     * @param chatId
     * @return
     */
    List<ImMessageVO> listUnreadMessage(Long chatId);

    /**
     * 发送消息
     *
     * @param messageDTO
     * @return
     */
    ImMessage send(ImMessageDTO messageDTO);

    /**
     * 聊天未读消息
     *
     * @param userId
     * @param chatIdList
     * @return
     */
    List<ImChatMessageBO> listChatUnreadMessage(Long userId, List<Long> chatIdList);

    /**
     * 聊天最新消息
     *
     * @param userId
     * @param chatIdList
     * @return
     */
    List<ImMessage> listLatestMessage(Long userId, List<Long> chatIdList);
}

package com.qiangesoft.im.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qiangesoft.im.entity.ImMessageReceiver;

/**
 * <p>
 * 群用户消息关系 服务类
 * </p>
 *
 * @author qiangesoft
 * @since 2023-08-23
 */
public interface IImMessageReceiverService extends IService<ImMessageReceiver> {

    /**
     * 置为已读
     *
     * @param chatId
     * @param messageId
     */
    void updateRead(Long chatId, Long messageId);

}

package com.qiangesoft.im.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.im.auth.UserUtil;
import com.qiangesoft.im.core.constant.ChatTypeEnum;
import com.qiangesoft.im.entity.*;
import com.qiangesoft.im.exception.ServiceException;
import com.qiangesoft.im.mapper.ImMessageMapper;
import com.qiangesoft.im.pojo.bo.ImChatMessageBO;
import com.qiangesoft.im.pojo.dto.ImMessageDTO;
import com.qiangesoft.im.pojo.dto.query.MessageQueryDTO;
import com.qiangesoft.im.pojo.dto.query.PageQueryDTO;
import com.qiangesoft.im.pojo.vo.ImMessageVO;
import com.qiangesoft.im.pojo.vo.PageResultVO;
import com.qiangesoft.im.pojo.vo.SysUserVo;
import com.qiangesoft.im.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 群消息 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Service
public class ImMessageServiceImpl extends ServiceImpl<ImMessageMapper, ImMessage> implements IImMessageService {

    @Lazy
    @Autowired
    private IImChatService chatService;
    @Autowired
    private IImFriendService friendService;
    @Autowired
    private IImGroupUserService groupUserService;
    @Autowired
    private IImMessageReceiverService messageReceiverService;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public PageResultVO<ImMessageVO> listMessage(PageQueryDTO pageQuery, MessageQueryDTO messageQuery) {
        Long chatId = messageQuery.getChatId();
        ImChat chat = chatService.getById(chatId);
        if (chat == null) {
            throw new ServiceException("聊天不存在");
        }

        Long userId = UserUtil.getUserId();

        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();

        PageResultVO<ImMessageVO> pageResult = new PageResultVO<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);

        IPage<ImMessageVO> messagePage = baseMapper.listMessage(new Page<>(pageNum, pageSize), userId, messageQuery.getChatId(), messageQuery.getMessageType(), messageQuery.getMessage());
        pageResult.setTotal(messagePage.getTotal());
        pageResult.setPages(messagePage.getPages());
        List<ImMessageVO> records = messagePage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            pageResult.setResults(records);
            return pageResult;
        }

        Set<Long> senderIdList = records.stream().map(ImMessageVO::getSenderId).collect(Collectors.toSet());
        Long targetId = chat.getTargetId();
        Map<Long, String> avatarMap = sysUserService.listByIds(senderIdList).stream().collect(Collectors.toMap(SysUser::getId, SysUser::getAvatar));
        Map<Long, String> nickNameMap = new HashMap<>();
        if (ChatTypeEnum.GROUP.getCode().equals(chat.getChatType())) {
            LambdaQueryWrapper<ImGroupUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ImGroupUser::getGroupId, targetId)
                    .in(ImGroupUser::getUserId, senderIdList);
            nickNameMap = groupUserService.list(queryWrapper).stream().collect(Collectors.toMap(ImGroupUser::getUserId, ImGroupUser::getNickName));
        } else {
            ImFriend friend = friendService.getById(targetId);
            nickNameMap.put(userId, UserUtil.getNickName());
            nickNameMap.put(targetId, friend.getRemark());
        }
        for (ImMessageVO record : records) {
            SysUserVo sysUserVo = new SysUserVo();
            Long senderId = record.getSenderId();
            sysUserVo.setId(senderId);
            sysUserVo.setAvatar(avatarMap.get(senderId));
            sysUserVo.setNickName(nickNameMap.get(senderId));
            record.setSender(sysUserVo);
        }
        pageResult.setResults(records);
        return pageResult;
    }

    @Override
    public List<ImMessageVO> listUnreadMessage(Long chatId) {
        ImChat chat = chatService.getById(chatId);
        if (chat == null) {
            throw new ServiceException("聊天不存在");
        }

        Long userId = UserUtil.getUserId();
        List<ImMessageVO> records = baseMapper.listUnreadMessage(userId, chatId);
        if (CollectionUtils.isEmpty(records)) {
            return records;
        }

        Set<Long> senderIdList = records.stream().map(ImMessageVO::getSenderId).collect(Collectors.toSet());
        Long targetId = chat.getTargetId();
        Map<Long, String> avatarMap = sysUserService.listByIds(senderIdList).stream().collect(Collectors.toMap(SysUser::getId, SysUser::getAvatar));
        Map<Long, String> nickNameMap = new HashMap<>();
        if (ChatTypeEnum.GROUP.getCode().equals(chat.getChatType())) {
            LambdaQueryWrapper<ImGroupUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ImGroupUser::getGroupId, targetId)
                    .in(ImGroupUser::getUserId, senderIdList);
            nickNameMap = groupUserService.list(queryWrapper).stream().collect(Collectors.toMap(ImGroupUser::getUserId, ImGroupUser::getNickName));
        } else {
            ImFriend friend = friendService.getById(targetId);
            nickNameMap.put(userId, UserUtil.getNickName());
            nickNameMap.put(targetId, friend.getRemark());
        }
        for (ImMessageVO record : records) {
            SysUserVo sysUserVo = new SysUserVo();
            Long senderId = record.getSenderId();
            sysUserVo.setId(senderId);
            sysUserVo.setAvatar(avatarMap.get(senderId));
            sysUserVo.setNickName(nickNameMap.get(senderId));
            record.setSender(sysUserVo);
        }
        return records;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ImMessage send(ImMessageDTO messageDTO) {
        Long userId = UserUtil.getUserId();

        ImMessage message = null;
        String chatType = messageDTO.getChatType();
        if (ChatTypeEnum.GROUP.getCode().equals(chatType)) {
            message = this.sendGroupMessage(messageDTO, userId);
        }
        if (ChatTypeEnum.PERSON.getCode().equals(chatType)) {
            message = this.sendPersonMessage(messageDTO, userId);
        }
        return message;
    }

    /**
     * 私聊消息
     *
     * @param messageDTO
     * @param userId
     * @return
     */
    private ImMessage sendPersonMessage(ImMessageDTO messageDTO, Long userId) {
        Long friendUserId = messageDTO.getTargetId();
        LambdaQueryWrapper<ImFriend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImFriend::getFriendUserId, friendUserId)
                .eq(ImFriend::getUserId, userId)
                .eq(ImFriend::getDelFlag, false);
        ImFriend friend = friendService.getOne(queryWrapper);
        if (friend == null) {
            throw new ServiceException("非好友关系");
        }

        // 聊天会话
        Long chatId = messageDTO.getChatId();
        if (chatId == null) {
            LambdaQueryWrapper<ImChat> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ImChat::getUserId, userId)
                    .eq(ImChat::getChatType, messageDTO.getChatType())
                    .eq(ImChat::getTargetId, friendUserId)
                    .eq(ImChat::getDelFlag, false);
            ImChat chat = chatService.getOne(queryWrapper1);
            if (chat == null) {
                chat = new ImChat();
                chat.setUserId(userId);
                chat.setChatType(messageDTO.getChatType());
                chat.setTargetId(friendUserId);
                chat.setDelFlag(false);
                chatService.save(chat);
            }
            messageDTO.setChatId(chat.getId());
        }

        // 消息
        ImMessage message = new ImMessage();
        message.setSenderId(userId);
        message.setChatId(messageDTO.getChatId());
        message.setMessageType(messageDTO.getMessageType());
        message.setMessage(messageDTO.getMessage());
        message.setDelFlag(false);
        baseMapper.insert(message);

        // 发送
        ImMessageReceiver messageReceiver = new ImMessageReceiver();
        messageReceiver.setMessageId(message.getId());
        messageReceiver.setReceiverId(friendUserId);
        messageReceiver.setDelFlag(false);
        messageReceiver.setReadFlag(false);
        messageReceiverService.save(messageReceiver);
        return message;
    }

    /**
     * 群聊消息
     *
     * @param messageDTO
     * @param userId
     * @return
     */
    private ImMessage sendGroupMessage(ImMessageDTO messageDTO, Long userId) {
        Long groupId = messageDTO.getTargetId();
        LambdaQueryWrapper<ImGroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImGroupUser::getGroupId, groupId)
                .eq(ImGroupUser::getUserId, userId)
                .eq(ImGroupUser::getDelFlag, false);
        ImGroupUser imGroupUser = groupUserService.getOne(queryWrapper);
        if (imGroupUser == null) {
            throw new ServiceException("无法发言,您已不在群中");
        }

        // 聊天会话
        Long chatId = messageDTO.getChatId();
        if (chatId == null) {
            LambdaQueryWrapper<ImChat> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ImChat::getUserId, userId)
                    .eq(ImChat::getChatType, messageDTO.getChatType())
                    .eq(ImChat::getTargetId, groupId)
                    .eq(ImChat::getDelFlag, false);
            ImChat chat = chatService.getOne(queryWrapper1);
            if (chat == null) {
                chat = new ImChat();
                chat.setUserId(userId);
                chat.setChatType(messageDTO.getChatType());
                chat.setTargetId(groupId);
                chat.setDelFlag(false);
                chatService.save(chat);
            }
            messageDTO.setChatId(chat.getId());
        }

        // 消息
        ImMessage message = new ImMessage();
        message.setSenderId(userId);
        message.setChatId(messageDTO.getChatId());
        message.setMessageType(messageDTO.getMessageType());
        message.setMessage(messageDTO.getMessage());
        message.setDelFlag(false);
        baseMapper.insert(message);

        // 发给群成员
        List<SysUserVo> groupUserList = groupUserService.listGroupUser(groupId);
        List<ImMessageReceiver> messageReceiverList = new ArrayList<>();
        for (SysUserVo sysUserVo : groupUserList) {
            ImMessageReceiver messageReceiver = new ImMessageReceiver();
            messageReceiver.setMessageId(message.getId());
            messageReceiver.setReceiverId(sysUserVo.getId());
            messageReceiver.setDelFlag(false);
            messageReceiver.setReadFlag(false);
            if (userId.equals(sysUserVo.getId())) {
                messageReceiver.setReadFlag(true);
            }
            messageReceiverList.add(messageReceiver);
        }
        messageReceiverService.saveBatch(messageReceiverList);
        return message;
    }


    @Override
    public List<ImChatMessageBO> listChatUnreadMessage(Long userId, List<Long> chatIdList) {
        return baseMapper.listChatUnreadMessage(userId, chatIdList);
    }

    @Override
    public List<ImMessage> listLatestMessage(Long userId, List<Long> chatIdList) {
        return baseMapper.listLatestMessage(userId, chatIdList);
    }
}

package com.qiangesoft.im.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.im.auth.UserUtil;
import com.qiangesoft.im.entity.ImMessageReceiver;
import com.qiangesoft.im.mapper.ImMessageReceiverMapper;
import com.qiangesoft.im.service.IImMessageReceiverService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 群用户消息关系 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @since 2023-08-23
 */
@Service
public class ImMessageReceiverServiceImpl extends ServiceImpl<ImMessageReceiverMapper, ImMessageReceiver> implements IImMessageReceiverService {

    @Override
    public void updateRead(Long chatId, Long messageId) {
        baseMapper.updateRead(UserUtil.getUserId(), chatId, messageId);
    }
}

controller

package com.qiangesoft.im.controller;

import com.qiangesoft.im.core.ImWebSocketServer;
import com.qiangesoft.im.entity.ImMessage;
import com.qiangesoft.im.pojo.bo.ImMessageBO;
import com.qiangesoft.im.pojo.dto.ImMessageDTO;
import com.qiangesoft.im.pojo.dto.ImMessageReadDTO;
import com.qiangesoft.im.pojo.dto.query.MessageQueryDTO;
import com.qiangesoft.im.pojo.dto.query.PageQueryDTO;
import com.qiangesoft.im.pojo.vo.ImMessageVO;
import com.qiangesoft.im.pojo.vo.PageResultVO;
import com.qiangesoft.im.pojo.vo.ResultInfo;
import com.qiangesoft.im.service.IImMessageReceiverService;
import com.qiangesoft.im.service.IImMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 群消息 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Api(tags = "消息")
@RestController
@RequestMapping("/im/message")
public class ImMessageController {

    @Autowired
    private IImMessageService messageService;
    @Autowired
    private IImMessageReceiverService messageReceiverService;

    @GetMapping
    @ApiOperation(value = "消息列表")
    public ResultInfo<PageResultVO<ImMessageVO>> listMessage(@Validated PageQueryDTO pageQuery, @Validated MessageQueryDTO messageQuery) {
        PageResultVO<ImMessageVO> pageResult = messageService.listMessage(pageQuery, messageQuery);
        return ResultInfo.ok(pageResult);
    }

    @GetMapping("/unread")
    @ApiOperation(value = "未读消息列表")
    public ResultInfo<List<ImMessageVO>> listUnreadMessage(Long chatId) {
        List<ImMessageVO> messageList = messageService.listUnreadMessage(chatId);
        return ResultInfo.ok(messageList);
    }

    @PostMapping("/send")
    @ApiOperation(value = "发送消息")
    public ResultInfo<Void> send(@Validated @RequestBody ImMessageDTO messageDTO) {
        ImMessage message = messageService.send(messageDTO);

        // 发送消息
        ImMessageBO messageBO = new ImMessageBO();
        messageBO.setId(message.getId());
        messageBO.setSenderId(message.getSenderId());
        messageBO.setChatId(message.getChatId());
        messageBO.setMessageType(message.getMessageType());
        messageBO.setMessage(message.getMessage());
        messageBO.setSendTime(message.getCreateTime());
        messageBO.setChatType(messageDTO.getChatType());
        messageBO.setTargetId(messageDTO.getTargetId());
        messageBO.setExtra(messageDTO.getExtra());
        messageBO.setTimestamp(messageDTO.getTimestamp());
        ImWebSocketServer.sendMessage(messageBO);
        return ResultInfo.ok();
    }

    @PutMapping("/read")
    @ApiOperation(value = "阅读消息")
    public ResultInfo<Void> read(@Validated @RequestBody ImMessageReadDTO messageReadDTO) {
        messageReceiverService.updateRead(messageReadDTO.getChatId(), messageReadDTO.getId());
        return ResultInfo.ok();
    }
}


2.2 聊天会话

mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qiangesoft.im.mapper.ImChatMapper">

    <select id="listChat" resultType="com.qiangesoft.im.entity.ImChat">
        SELECT a.id,
               a.user_id,
               a.chat_type,
               a.target_id,
               a.del_flag,
               a.top_flag,
               b.create_time
        FROM im_chat a
                 INNER JOIN (SELECT chat_id, max(create_time) create_time FROM im_message GROUP BY chat_id) b
                            ON a.id = b.chat_id
        WHERE a.del_flag = FALSE
          AND a.user_id = #{userId}
        ORDER BY a.top_flag, b.create_time
    </select>

</mapper>

package com.qiangesoft.im.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiangesoft.im.entity.ImChat;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * <p>
 * 聊天 Mapper 接口
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface ImChatMapper extends BaseMapper<ImChat> {

    /**
     * 聊天列表
     *
     * @return
     */
    List<ImChat> listChat(@Param("userId") Long userId);

}

service

package com.qiangesoft.im.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qiangesoft.im.entity.ImChat;
import com.qiangesoft.im.pojo.vo.ImChatVO;

import java.util.List;

/**
 * <p>
 * 聊天 服务类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
public interface IImChatService extends IService<ImChat> {

    /**
     * 删除聊天
     *
     * @param id
     */
    void removeChat(Long id);

    /**
     * 聊天列表
     *
     * @return
     */
    List<ImChatVO> listChat();

    /**
     * 置顶聊天
     *
     * @param id
     */
    void setTop(Long id);

    /**
     * 取消置顶聊天
     *
     * @param id
     */
    void cancelTop(Long id);
}

package com.qiangesoft.im.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.im.auth.UserUtil;
import com.qiangesoft.im.core.constant.ChatTypeEnum;
import com.qiangesoft.im.entity.*;
import com.qiangesoft.im.mapper.ImChatMapper;
import com.qiangesoft.im.pojo.bo.ImChatMessageBO;
import com.qiangesoft.im.pojo.vo.ImChatVO;
import com.qiangesoft.im.pojo.vo.ImMessageVO;
import com.qiangesoft.im.pojo.vo.SysUserVo;
import com.qiangesoft.im.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 聊天 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Service
public class ImChatServiceImpl extends ServiceImpl<ImChatMapper, ImChat> implements IImChatService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IImFriendService friendService;
    @Autowired
    private IImGroupService groupService;
    @Autowired
    private IImGroupUserService groupUserService;
    @Autowired
    private IImMessageService messageService;

    @Override
    public void removeChat(Long id) {
        LambdaUpdateWrapper<ImChat> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImChat::getId, id)
                .set(ImChat::getDelFlag, true);
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public List<ImChatVO> listChat() {
        List<ImChatVO> groupVOList = new ArrayList<>();

        Long userId = UserUtil.getUserId();
        List<ImChat> chatList = baseMapper.listChat(userId);
        if (CollectionUtils.isEmpty(chatList)) {
            return groupVOList;
        }

        // 聊天列表对象信息
        List<Long> friendIdList = chatList.stream().filter(e -> ChatTypeEnum.PERSON.getCode().equals(e.getChatType())).map(ImChat::getTargetId).collect(Collectors.toList());
        Map<Long, ImFriend> friendMap = new HashMap<>();
        Map<Long, SysUser> sysUserMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(friendIdList)) {
            List<ImFriend> friendList = friendService.listByIds(friendIdList);
            friendMap = friendList.stream().collect(Collectors.toMap(ImFriend::getId, imFriend -> imFriend));
            List<Long> friendUserIdList = friendList.stream().map(ImFriend::getFriendUserId).collect(Collectors.toList());
            sysUserMap = sysUserService.listByIds(friendUserIdList).stream().collect(Collectors.toMap(SysUser::getId, sysUser -> sysUser));
        }
        List<Long> groupIdList = chatList.stream().filter(e -> ChatTypeEnum.GROUP.getCode().equals(e.getChatType())).map(ImChat::getTargetId).collect(Collectors.toList());
        Map<Long, ImGroup> groupMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(groupIdList)) {
            groupMap = groupService.listByIds(groupIdList).stream().collect(Collectors.toMap(ImGroup::getId, imGroup -> imGroup));
        }

        // 未读消息
        List<Long> chatIdList = chatList.stream().map(ImChat::getId).collect(Collectors.toList());
        List<ImChatMessageBO> unReadMessageList = messageService.listChatUnreadMessage(userId, chatIdList);
        // 最新消息
        List<ImMessage> latestMessageList = messageService.listLatestMessage(userId, chatIdList);
        List<Long> friendChatIdList = chatList.stream().filter(e -> ChatTypeEnum.PERSON.getCode().equals(e.getChatType())).map(ImChat::getId).collect(Collectors.toList());
        List<Long> groupChatIdList = chatList.stream().filter(e -> ChatTypeEnum.GROUP.getCode().equals(e.getChatType())).map(ImChat::getId).collect(Collectors.toList());
        List<ImMessage> friendLatestMessageList = latestMessageList.stream().filter(e -> friendChatIdList.contains(e.getChatId())).collect(Collectors.toList());
        List<ImMessage> groupLatestMessageList = latestMessageList.stream().filter(e -> groupChatIdList.contains(e.getChatId())).collect(Collectors.toList());

        // 昵称
        List<Long> sendFriendIdList = friendLatestMessageList.stream().map(ImMessage::getSenderId).collect(Collectors.toList());
        Map<Long, String> remarkMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(sendFriendIdList)) {
            LambdaQueryWrapper<ImFriend> fqueryWrapper = new LambdaQueryWrapper<>();
            fqueryWrapper.eq(ImFriend::getUserId, userId)
                    .in(ImFriend::getFriendUserId, sendFriendIdList);
            remarkMap = friendService.list(fqueryWrapper).stream().collect(Collectors.toMap(ImFriend::getFriendUserId, ImFriend::getRemark));
        }
        List<Long> sendGroupIdList = groupLatestMessageList.stream().map(ImMessage::getSenderId).collect(Collectors.toList());
        Map<Long, String> nicknameMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(groupIdList) && !CollectionUtils.isEmpty(sendGroupIdList)) {
            LambdaQueryWrapper<ImGroupUser> gqueryWrapper = new LambdaQueryWrapper<>();
            gqueryWrapper.in(ImGroupUser::getGroupId, groupIdList)
                    .in(ImGroupUser::getUserId, sendGroupIdList);
            nicknameMap = groupUserService.list(gqueryWrapper).stream().collect(Collectors.toMap(ImGroupUser::getUserId, ImGroupUser::getNickName));
        }

        for (ImChat chat : chatList) {
            ImChatVO vo = new ImChatVO();
            vo.setId(chat.getId());
//            vo.setAvatar(avatarMap.get());
            Long targetId = chat.getTargetId();
            String chatType = chat.getChatType();
            vo.setTargetId(targetId);
            vo.setChatType(chatType);

            // 未读消息数
            Optional<ImChatMessageBO> first = unReadMessageList.stream().filter(e -> e.getChatId().equals(chat.getId())).findFirst();
            Integer unreadNum = first.isPresent() ? first.get().getUnreadNum() : 0;
            vo.setUnreadNum(unreadNum);

            String name;
            String avatar;
            if (ChatTypeEnum.PERSON.getCode().equals(chatType)) {
                ImFriend friend = friendMap.get(targetId);
                name = friend.getRemark();
                SysUser sysUser = sysUserMap.get(friend.getFriendUserId());
                avatar = sysUser.getAvatar();

                Optional<ImMessage> firstLatest = friendLatestMessageList.stream().filter(e -> e.getChatId().equals(chat.getId())).findFirst();
                if (firstLatest.isPresent()) {
                    ImMessage message = firstLatest.get();
                    ImMessageVO messageVO = new ImMessageVO();
                    messageVO.setId(message.getId());
                    messageVO.setMessageType(message.getMessageType());
                    messageVO.setMessage(message.getMessage());
                    messageVO.setReadFlag(false);
                    messageVO.setSendTime(message.getCreateTime());

                    // 发送人
                    SysUserVo sysUserVo = new SysUserVo();
                    Long senderId = message.getSenderId();
                    sysUserVo.setId(senderId);
                    sysUserVo.setNickName(remarkMap.get(senderId));
                    messageVO.setSender(sysUserVo);
                    vo.setLatestMessage(messageVO);
                }
            } else {
                ImGroup group = groupMap.get(targetId);
                name = group.getName();
                avatar = group.getAvatar();

                Optional<ImMessage> firstLatest = groupLatestMessageList.stream().filter(e -> e.getChatId().equals(chat.getId())).findFirst();
                if (firstLatest.isPresent()) {
                    ImMessage message = firstLatest.get();
                    ImMessageVO messageVO = new ImMessageVO();
                    messageVO.setId(message.getId());
                    messageVO.setMessageType(message.getMessageType());
                    messageVO.setMessage(message.getMessage());
                    messageVO.setReadFlag(false);
                    messageVO.setSendTime(message.getCreateTime());

                    // 发送人
                    SysUserVo sysUserVo = new SysUserVo();
                    Long senderId = message.getSenderId();
                    sysUserVo.setId(senderId);
                    sysUserVo.setNickName(nicknameMap.get(senderId));
                    messageVO.setSender(sysUserVo);
                    vo.setLatestMessage(messageVO);
                }
            }
            vo.setName(name);
            vo.setAvatar(avatar);
            groupVOList.add(vo);
        }
        return groupVOList;
    }

    @Override
    public void setTop(Long id) {
        LambdaUpdateWrapper<ImChat> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImChat::getId, id)
                .set(ImChat::getTopFlag, true);
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void cancelTop(Long id) {
        LambdaUpdateWrapper<ImChat> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ImChat::getId, id)
                .set(ImChat::getTopFlag, false);
        baseMapper.update(null, updateWrapper);
    }

}

package com.qiangesoft.im.controller;

import com.qiangesoft.im.pojo.vo.ImChatVO;
import com.qiangesoft.im.pojo.vo.ResultInfo;
import com.qiangesoft.im.service.IImChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 群组 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @date 2024-02-07
 */
@Api(tags = "聊天")
@RequiredArgsConstructor
@RestController
@RequestMapping("/im/chat")
public class ImChatController {

    private final IImChatService chatService;

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除聊天")
    public ResultInfo<Void> removeChat(@PathVariable Long id) {
        chatService.removeChat(id);
        return ResultInfo.ok();
    }

    @GetMapping
    @ApiOperation(value = "聊天列表")
    public ResultInfo<List<ImChatVO>> listChat() {
        return ResultInfo.ok(chatService.listChat());
    }

    @PutMapping("/setTop/{id}")
    @ApiOperation(value = "置顶聊天")
    public ResultInfo<Void> setTop(@PathVariable Long id) {
        chatService.setTop(id);
        return ResultInfo.ok();
    }

    @PutMapping("/cancelTop/{id}")
    @ApiOperation(value = "取消置顶聊天")
    public ResultInfo<Void> cancelTop(@PathVariable Long id) {
        chatService.cancelTop(id);
        return ResultInfo.ok();
    }

}

三、消息发送接收测试

在这里插入图片描述
在这里插入图片描述

四、源码地址

源码地址:https://gitee.com/qiangesoft/boot-business/tree/master/boot-business-im

后续内容见下章

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1458860.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

代码随想录算法训练营第十八天|235.二叉搜索树的最近公共祖先,701.二叉搜索树中的插入操作,450.删除二叉搜索树节点

235.二叉搜索树的最近公共祖先 701.二叉搜索树中的插入操作 450.删除二叉搜索树节点 235.二叉搜索树的最近公共祖先 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为&#xff1a;“对于有根树 T 的两个结点 p、q&#xff0c;最近…

函数指针概念的理解要点

1&#xff1a;理解函数 函数即为代码&#xff0c;代码就应该放在内存中&#xff0c;是内存&#xff0c;就有地址 void CmpFun(int a,int b){if(a>b)printf("最大的数为%d",a);elseprintf("最大的数为%d",b);}int main(){printf("输出函数CmpFu…

redo日志——log buffer与磁盘的交互详解

目录 一、redo日志刷盘机制&#xff08;时机&#xff09; 1、log buffer 空间不足时 2、事务提交 3、后台线程 4、正常关闭服务器 5、checkpoint时 二、数据目录中的redo日志文件 三、redo日志文件格式 log buffer与磁盘交互的最小单位——block 文件格式 四、Log Se…

Generator生成器函数

<script>//三个ajax异步的请求要保证执行的顺序//1. 传统的callback触发回调地狱 痛苦 处理异步任务按照顺序 异步-->同步化执行 代码的可读性更好//2. promise then then语义化明显//3.Generator生成函数//4.async await //ajax函数 xhr//XHR代表 "XMLHttp…

STM32 系统滴答时钟启动过程 SysTick_Config

STM32 系统滴答时钟启动过程 SysTick_Config 1. 系统滴答时钟1.1 简介1.2 配置1.3 启动和更新 1. 系统滴答时钟 1.1 简介 SysTick&#xff1a;系统滴答时钟&#xff0c;属于Cortex-M4内核中的一个外设&#xff0c;24bit向下递减计数。 Systick定时器常用来做延时&#xff0c;…

Jenkins配置node节点

1、添加节点 2、配置node主机的java环境 注意&#xff0c;jdk的位置和版本要和master保持一致 sudo apt-get update sudo apt-get install openjdk-8-jre vim /etc/enviroment写入&#xff1a;export JAVA_HOME/usr/lib/jvm/openjdk-8-jre 按wq!退出 再输入&#xff1a;s…

onlyoffice jsApi调用 进阶二次开发 连接器开发 api操作office文档 demo可直接运行测试

office这个体系分为四个大教程 1、【document server文档服务器基础搭建】 2、【连接器(connector)或者jsApi调用操作office】-进阶 3、【document builder文档构造器使用】-进阶 4、【Conversion API(文档转化服务)】-进阶 如果需要连接器&#xff0c;可以查看&#xff1a…

动态gif怎么在线生成?一招快速搞定gif动画

在我们生活沟通中都会运用到各种生动有趣gif动图。很多时候我们在使用gif动图的时候都需要从网上下载找起来很麻烦。那么&#xff0c;自己怎么制作gif动态图片呢&#xff1f;很简单&#xff0c;使用Gif制作&#xff08;https://www.gif.cn/&#xff09;工具&#xff0c;不需要下…

前端|Day3:CSS基础(黑马笔记)

Day3:CSS基础 目录 Day3:CSS基础一、CSS初体验二、CSS引入方式三、选择器1.标签选择器2.类选择器3.id选择器4.通配符选择器 四、盒子尺寸和背景色五、文字控制属性1.字体大小2.字体样式&#xff08;是否倾斜&#xff09;3.行高单行文字垂直居中 4.字体族5.font复合属性6.文本缩…

XGB-7: 特征交互约束

决策树是发现自变量&#xff08;特征&#xff09;之间交互关系的强大工具。在遍历路径中一起出现的变量是相互交互的&#xff0c;因为子节点的条件取决于父节点的条件。例如&#xff0c;在下图中&#xff0c;红色突出显示的路径包含三个变量&#xff1a; x 1 x_1 x1​、 x 7 x_…

facebook群控如何做?静态住宅ip代理在多账号运营重的作用

在进行Facebook群控时&#xff0c;ip地址的管理是非常重要的&#xff0c;因为Facebook通常会检测ip地址的使用情况&#xff0c;如果发现有异常的使用行为&#xff0c;比如从同一个ip地址频繁进行登录、发布内容或者在短时间内进行大量的活动等等&#xff0c;就会视为垃圾邮件或…

探究二维码技术:连接现实与数字世界的桥梁

title: 探究二维码技术&#xff1a;连接现实与数字世界的桥梁 date: 2024/2/19 13:15:36 updated: 2024/2/19 13:15:36 tags: 二维码技术数据编码纠错算法图像处理商业应用安全验证实时交互 引言&#xff1a; 二维码已经成为现代社会中广泛应用的一种技术工具。它不仅在商业领…

C#,二进制数的按位交换(Bits swap)的算法与源代码

数字在指定位置指定位数的交换是常见算法。 1 源程序 using System; using System.Text; using System.Collections; using System.Collections.Generic; namespace Legalsoft.Truffer.Algorithm { public static partial class Algorithm_Gallery { /// <…

Java——Stream流的学习

在开发过程中&#xff0c;经常或忽略流的使用&#xff0c;导致用的不熟练&#xff0c;于是抽时间系统的学习下stream的使用&#xff0c;找了哔哩哔哩的教程跟着看看练练。 准备工作 创建Book、Aurhor实体类&#xff0c;初始化数据 public static List<Author> getAuth…

图形渲染基础学习

原文链接&#xff1a;游戏开发入门&#xff08;三&#xff09;图形渲染_如果一个面只有三个像素进行渲染可以理解为是定点渲染吗?-CSDN博客 游戏开发入门&#xff08;三&#xff09;图形渲染笔记&#xff1a; 渲染一般分为离线渲染与实时渲染&#xff0c;游戏中我们用的都是…

浙大恩特客户资源管理系统 FollowAction SQL注入漏洞复现

0x01 产品简介 浙大恩特客户资源管理系统是一款针对企业客户资源管理的软件产品。该系统旨在帮助企业高效地管理和利用客户资源,提升销售和市场营销的效果。 0x02 漏洞概述 浙大恩特客户资源管理系统 FollowAction 接口处存在SQL注入漏洞,未经身份认证的攻击者可以利用该漏…

补-代码随想录第23天|● 669. 修剪二叉搜索树 ● 108.将有序数组转换为二叉搜索树 ● 538.把二叉搜索树转换为累加树

二叉树最后一天 ● 669. 修剪二叉搜索树思路一&#xff1a;递归递归三部曲代码&#xff1a; 思路二&#xff1a;迭代代码&#xff1a; ● 108.将有序数组转换为二叉搜索树思路&#xff1a;递归代码;[左闭右闭] ● 538.把二叉搜索树转换为累加树思路&#xff1a;递归 代码&#…

onlyoffice基础环境搭建+部署+demo可直接运行 最简单的入门

office这个体系分为四个大教程 1、【document server文档服务器基础搭建】 2、【连接器(connector)或者jsApi调用操作office】-进阶 3、【document builder文档构造器使用】-进阶 4、【Conversion API(文档转化服务)】-进阶 如果需要连接器&#xff0c;可以查看&#xff1a;onl…

【SpringBoot3】Spring Security 常用注解

注&#xff1a;本文基于Spring Boot 3.2.1 以及 Spring Security 6.2.1 Spring Security 6 的常用注解包括以下几种&#xff0c;通过这些注解可以更加方便的控制资源权限。 Secured &#xff1a;方法执行前检查&#xff0c;直接判断有没有对应的角色PreAuthorize&#xff1a;方…

Jmeter实现阶梯式线程增加的压测

安装相应jmeter 插件 1&#xff1a;安装jmeter 管理插件&#xff1a; 下载地址&#xff1a;https://jmeter-plugins.org/install/Install/&#xff0c;将下载下来的jar包放到jmeter文件夹下的lib/ext路径下&#xff0c;然后重启jmeter。 2&#xff1a;接着打开 选项-Plugins Ma…