mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-14 14:13:42 +08:00
单聊群聊功能提交
This commit is contained in:
@@ -2,17 +2,19 @@ package com.abin.mallchat.custom.chat.controller;
|
||||
|
||||
|
||||
import com.abin.mallchat.common.chat.domain.dto.MsgReadInfoDTO;
|
||||
import com.abin.mallchat.common.chat.domain.vo.response.ChatMessageResp;
|
||||
import com.abin.mallchat.common.common.annotation.FrequencyControl;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.common.utils.RequestHolder;
|
||||
import com.abin.mallchat.common.user.domain.enums.BlackTypeEnum;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberStatisticResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageReadResp;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.custom.user.service.impl.UserServiceImpl;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -43,17 +45,12 @@ public class ChatController {
|
||||
@Autowired
|
||||
private UserCache userCache;
|
||||
|
||||
@GetMapping("/public/room/page")
|
||||
@ApiOperation("会话列表")
|
||||
public ApiResult<CursorPageBaseResp<ChatRoomResp>> getRoomPage(@Valid CursorPageBaseReq request) {
|
||||
return ApiResult.success(chatService.getRoomPage(request, RequestHolder.get().getUid()));
|
||||
}
|
||||
|
||||
@GetMapping("/public/member/page")
|
||||
@ApiOperation("群成员列表")
|
||||
@Deprecated
|
||||
@FrequencyControl(time = 120, count = 20, target = FrequencyControl.Target.IP)
|
||||
public ApiResult<CursorPageBaseResp<ChatMemberResp>> getMemberPage(@Valid CursorPageBaseReq request) {
|
||||
CursorPageBaseResp<ChatMemberResp> memberPage = chatService.getMemberPage(request);
|
||||
public ApiResult<CursorPageBaseResp<ChatMemberResp>> getMemberPage(@Valid MemberReq request) {
|
||||
CursorPageBaseResp<ChatMemberResp> memberPage = chatService.getMemberPage(null, request);
|
||||
filterBlackMember(memberPage);
|
||||
return ApiResult.success(memberPage);
|
||||
}
|
||||
@@ -81,9 +78,6 @@ public class ChatController {
|
||||
return ApiResult.success(chatService.getMemberStatistic());
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private UserServiceImpl userService;
|
||||
|
||||
@GetMapping("/public/msg/page")
|
||||
@ApiOperation("消息列表")
|
||||
@FrequencyControl(time = 120, count = 20, target = FrequencyControl.Target.IP)
|
||||
@@ -138,5 +132,13 @@ public class ChatController {
|
||||
Long uid = RequestHolder.get().getUid();
|
||||
return ApiResult.success(chatService.getMsgReadInfo(uid, request));
|
||||
}
|
||||
|
||||
@PutMapping("/msg/read")
|
||||
@ApiOperation("消息阅读上报")
|
||||
public ApiResult<Void> msgRead(@Valid @RequestBody ChatMessageMemberReq request) {
|
||||
Long uid = RequestHolder.get().getUid();
|
||||
chatService.msgRead(uid, request);
|
||||
return ApiResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,13 @@ package com.abin.mallchat.custom.chat.controller;
|
||||
|
||||
|
||||
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.IdReqVO;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.common.utils.RequestHolder;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatRoomResp;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.custom.chat.service.RoomService;
|
||||
import com.abin.mallchat.custom.chat.service.RoomAppService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -34,7 +35,7 @@ public class ContactController {
|
||||
@Autowired
|
||||
private ChatService chatService;
|
||||
@Autowired
|
||||
private RoomService roomService;
|
||||
private RoomAppService roomService;
|
||||
|
||||
@GetMapping("/public/contact/page")
|
||||
@ApiOperation("会话列表")
|
||||
@@ -43,6 +44,11 @@ public class ContactController {
|
||||
return ApiResult.success(roomService.getContactPage(request, uid));
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/public/contact/detail")
|
||||
@ApiOperation("会话详情")
|
||||
public ApiResult<ChatRoomResp> getRoomPage(@Valid IdReqVO request) {
|
||||
Long uid = RequestHolder.get().getUid();
|
||||
return ApiResult.success(roomService.getContactDetail(uid, request.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
package com.abin.mallchat.custom.chat.controller;
|
||||
|
||||
|
||||
import com.abin.mallchat.common.common.annotation.FrequencyControl;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.IdReqVO;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.IdRespVO;
|
||||
import com.abin.mallchat.common.common.utils.RequestHolder;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageMemberReq;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.MemberAddReq;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.MemberDelReq;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.MemberReq;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.MemberResp;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.custom.chat.service.RoomAppService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -38,41 +34,48 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class RoomController {
|
||||
@Autowired
|
||||
private ChatService chatService;
|
||||
@Autowired
|
||||
private UserCache userCache;
|
||||
private RoomAppService roomService;
|
||||
|
||||
@GetMapping("/public/group")
|
||||
@ApiOperation("群组详情")
|
||||
public ApiResult<MemberResp> groupDetail(@Valid IdReqVO request) {
|
||||
return ApiResult.success();
|
||||
Long uid = RequestHolder.get().getUid();
|
||||
return ApiResult.success(roomService.getGroupDetail(uid, request.getId()));
|
||||
}
|
||||
|
||||
@GetMapping("/public/group/member/page")
|
||||
@ApiOperation("群成员列表")
|
||||
@FrequencyControl(time = 120, count = 20, target = FrequencyControl.Target.IP)
|
||||
public ApiResult<CursorPageBaseResp<ChatMemberResp>> getMemberPage(@Valid MemberReq request) {
|
||||
CursorPageBaseResp<ChatMemberResp> memberPage = chatService.getMemberPage(request);
|
||||
return ApiResult.success(memberPage);
|
||||
return ApiResult.success(roomService.getMemberPage(request));
|
||||
}
|
||||
|
||||
@GetMapping("/group/member/list")
|
||||
@ApiOperation("房间内的所有群成员列表-@专用")
|
||||
public ApiResult<List<ChatMemberListResp>> getMemberList(@Valid ChatMessageMemberReq request) {
|
||||
return ApiResult.success(chatService.getMemberList(request));
|
||||
return ApiResult.success(roomService.getMemberList(request));
|
||||
}
|
||||
|
||||
@DeleteMapping("/group/member")
|
||||
@ApiOperation("移除成员")
|
||||
public ApiResult<Void> delMember(@Valid @RequestBody MemberDelReq request) {
|
||||
Long uid = RequestHolder.get().getUid();
|
||||
roomService.delMember(uid, request);
|
||||
return ApiResult.success();
|
||||
}
|
||||
|
||||
@PostMapping("/group")
|
||||
@ApiOperation("新增群组")
|
||||
public ApiResult<IdRespVO> addGroup(@Valid @RequestBody GroupAddReq request) {
|
||||
Long uid = RequestHolder.get().getUid();
|
||||
Long roomId = roomService.addGroup(uid, request);
|
||||
return ApiResult.success(IdRespVO.id(roomId));
|
||||
}
|
||||
|
||||
@PostMapping("/group/member")
|
||||
@ApiOperation("邀请好友")
|
||||
public ApiResult<List<ChatMemberListResp>> addMember(@Valid @RequestBody MemberAddReq request) {
|
||||
public ApiResult<Void> addMember(@Valid @RequestBody MemberAddReq request) {
|
||||
Long uid = RequestHolder.get().getUid();
|
||||
roomService.addMember(uid, request);
|
||||
return ApiResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.abin.mallchat.custom.chat.domain.vo.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Description: 热点枚举
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum GroupRoleAPPEnum {
|
||||
LEADER(1, "群主"),
|
||||
MANAGER(2, "管理"),
|
||||
MEMBER(3, "普通成员"),
|
||||
REMOVE(4, "被移除的成员"),
|
||||
;
|
||||
|
||||
private final Integer type;
|
||||
private final String desc;
|
||||
|
||||
private static Map<Integer, GroupRoleAPPEnum> cache;
|
||||
|
||||
static {
|
||||
cache = Arrays.stream(GroupRoleAPPEnum.values()).collect(Collectors.toMap(GroupRoleAPPEnum::getType, Function.identity()));
|
||||
}
|
||||
|
||||
public static GroupRoleAPPEnum of(Integer type) {
|
||||
return cache.get(type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.abin.mallchat.custom.chat.domain.vo.request;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Description: 新建群组
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-29
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class GroupAddReq {
|
||||
@NotNull
|
||||
@Size(min = 1, max = 50)
|
||||
@ApiModelProperty("邀请的uid")
|
||||
private List<Long> uidList;
|
||||
}
|
||||
@@ -21,7 +21,7 @@ import java.util.List;
|
||||
@NoArgsConstructor
|
||||
public class MemberAddReq {
|
||||
@NotNull
|
||||
@ApiModelProperty("会话id")
|
||||
@ApiModelProperty("房间id")
|
||||
private Long roomId;
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -7,8 +7,6 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
@@ -20,6 +18,5 @@ import javax.validation.constraints.NotNull;
|
||||
@NoArgsConstructor
|
||||
public class MemberReq extends CursorPageBaseReq {
|
||||
@ApiModelProperty("房间号")
|
||||
@NotNull
|
||||
private Long roomId;
|
||||
private Long roomId = 1L;
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.abin.mallchat.custom.chat.domain.vo.response;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Description: 群成员列表的成员信息
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-23
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ChatMemberResp {
|
||||
@ApiModelProperty("uid")
|
||||
private Long uid;
|
||||
/**
|
||||
* @see com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum
|
||||
*/
|
||||
@ApiModelProperty("在线状态 1在线 2离线")
|
||||
private Integer activeStatus;
|
||||
@ApiModelProperty("最后一次上下线时间")
|
||||
private Date lastOptTime;
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.abin.mallchat.custom.chat.domain.vo.response;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Description: 群成员列表的成员信息
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-23
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ChatMemberRespV1 {
|
||||
@ApiModelProperty("uid")
|
||||
private Long uid;
|
||||
/**
|
||||
* @see com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum
|
||||
*/
|
||||
@ApiModelProperty("在线状态 1在线 2离线")
|
||||
private Integer activeStatus;
|
||||
@ApiModelProperty("最后一次上下线时间")
|
||||
private Date lastOptTime;
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package com.abin.mallchat.custom.chat.domain.vo.response;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Description: 消息
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-23
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ChatMessageResp {
|
||||
|
||||
@ApiModelProperty("发送者信息")
|
||||
private UserInfo fromUser;
|
||||
@ApiModelProperty("消息详情")
|
||||
private Message message;
|
||||
|
||||
@ApiModelProperty("申请消息")
|
||||
private ApplyMessage applyMessage;
|
||||
|
||||
@Data
|
||||
public static class UserInfo {
|
||||
@ApiModelProperty("用户id")
|
||||
private Long uid;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Message {
|
||||
@ApiModelProperty("消息id")
|
||||
private Long id;
|
||||
@ApiModelProperty("消息发送时间")
|
||||
private Date sendTime;
|
||||
@ApiModelProperty("消息类型 1正常文本 2.撤回消息")
|
||||
private Integer type;
|
||||
@ApiModelProperty("消息内容不同的消息类型,内容体不同,见https://www.yuque.com/snab/mallcaht/rkb2uz5k1qqdmcmd")
|
||||
private Object body;
|
||||
@ApiModelProperty("消息标记")
|
||||
private MessageMark messageMark;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class MessageMark {
|
||||
@ApiModelProperty("点赞数")
|
||||
private Integer likeCount;
|
||||
@ApiModelProperty("该用户是否已经点赞 0否 1是")
|
||||
private Integer userLike;
|
||||
@ApiModelProperty("举报数")
|
||||
private Integer dislikeCount;
|
||||
@ApiModelProperty("该用户是否已经举报 0否 1是")
|
||||
private Integer userDislike;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class ApplyMessage {
|
||||
@ApiModelProperty("申请id")
|
||||
private Long applyId;
|
||||
|
||||
@ApiModelProperty("申请信息")
|
||||
private String applyMsg;
|
||||
|
||||
@ApiModelProperty("申请发送时间")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
|
||||
private LocalDateTime sendTime;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package com.abin.mallchat.custom.chat.domain.vo.response;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Description: 消息
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-23
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ChatMessageRespV1 {
|
||||
|
||||
@ApiModelProperty("发送者信息")
|
||||
private UserInfo fromUser;
|
||||
@ApiModelProperty("消息详情")
|
||||
private Message message;
|
||||
|
||||
@Data
|
||||
public static class UserInfo {
|
||||
@ApiModelProperty("用户id")
|
||||
private Long uid;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Message {
|
||||
@ApiModelProperty("消息id")
|
||||
private Long id;
|
||||
@ApiModelProperty("消息发送时间")
|
||||
private Date sendTime;
|
||||
@ApiModelProperty("消息类型 1正常文本 2.撤回消息")
|
||||
private Integer type;
|
||||
@ApiModelProperty("消息内容不同的消息类型,内容体不同,见https://www.yuque.com/snab/mallcaht/rkb2uz5k1qqdmcmd")
|
||||
private Object body;
|
||||
@ApiModelProperty("消息标记")
|
||||
private MessageMark messageMark;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class MessageMark {
|
||||
@ApiModelProperty("点赞数")
|
||||
private Integer likeCount;
|
||||
@ApiModelProperty("该用户是否已经点赞 0否 1是")
|
||||
private Integer userLike;
|
||||
@ApiModelProperty("举报数")
|
||||
private Integer dislikeCount;
|
||||
@ApiModelProperty("该用户是否已经举报 0否 1是")
|
||||
private Integer userDislike;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public class ChatRoomResp {
|
||||
@ApiModelProperty("会话头像")
|
||||
private String avatar;
|
||||
@ApiModelProperty("房间最后活跃时间(用来排序)")
|
||||
private Date lastActiveTime;
|
||||
private Date activeTime;
|
||||
@ApiModelProperty("未读数")
|
||||
private Integer unreadCount;
|
||||
}
|
||||
|
||||
@@ -16,14 +16,17 @@ import lombok.NoArgsConstructor;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class MemberResp {
|
||||
@ApiModelProperty("群id")
|
||||
private Long id;
|
||||
@ApiModelProperty("房间id")
|
||||
private Long roomId;
|
||||
@ApiModelProperty("群名称")
|
||||
private Long groupName;
|
||||
private String groupName;
|
||||
@ApiModelProperty("群头像")
|
||||
private String avatar;
|
||||
@ApiModelProperty("在线人数")
|
||||
private Long onlineNum;//在线人数
|
||||
@ApiModelProperty("群聊描述")
|
||||
private String desc;//在线人数
|
||||
/**
|
||||
* @see com.abin.mallchat.custom.chat.domain.vo.enums.GroupRoleAPPEnum
|
||||
*/
|
||||
@ApiModelProperty("成员角色 1群主 2管理员 3普通成员 4踢出群聊")
|
||||
private Integer role;//在线人数
|
||||
private Integer role;
|
||||
}
|
||||
|
||||
@@ -2,10 +2,14 @@ package com.abin.mallchat.custom.chat.service;
|
||||
|
||||
import com.abin.mallchat.common.chat.domain.dto.MsgReadInfoDTO;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.vo.response.ChatMessageResp;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberStatisticResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageReadResp;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
@@ -46,10 +50,11 @@ public interface ChatService {
|
||||
/**
|
||||
* 获取群成员列表
|
||||
*
|
||||
* @param memberUidList
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
CursorPageBaseResp<ChatMemberResp> getMemberPage(CursorPageBaseReq request);
|
||||
CursorPageBaseResp<ChatMemberResp> getMemberPage(List<Long> memberUidList, CursorPageBaseReq request);
|
||||
|
||||
/**
|
||||
* 获取消息列表
|
||||
@@ -59,15 +64,6 @@ public interface ChatService {
|
||||
*/
|
||||
CursorPageBaseResp<ChatMessageResp> getMsgPage(ChatMessagePageReq request, @Nullable Long receiveUid);
|
||||
|
||||
/**
|
||||
* 获取会话列表
|
||||
*
|
||||
* @param request
|
||||
* @param uid
|
||||
* @return
|
||||
*/
|
||||
CursorPageBaseResp<ChatRoomResp> getRoomPage(CursorPageBaseReq request, Long uid);
|
||||
|
||||
ChatMemberStatisticResp getMemberStatistic();
|
||||
|
||||
void setMsgMark(Long uid, ChatMessageMarkReq request);
|
||||
@@ -79,4 +75,6 @@ public interface ChatService {
|
||||
Collection<MsgReadInfoDTO> getMsgReadInfo(Long uid, ChatMessageReadInfoReq request);
|
||||
|
||||
CursorPageBaseResp<ChatMessageReadResp> getReadPage(Long uid, ChatMessageReadReq request);
|
||||
|
||||
void msgRead(Long uid, ChatMessageMemberReq request);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.abin.mallchat.custom.chat.service;
|
||||
|
||||
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatRoomResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.MemberResp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-07-22
|
||||
*/
|
||||
public interface RoomAppService {
|
||||
/**
|
||||
* 获取会话列表--支持未登录态
|
||||
*/
|
||||
CursorPageBaseResp<ChatRoomResp> getContactPage(CursorPageBaseReq request, Long uid);
|
||||
|
||||
/**
|
||||
* 获取群组信息
|
||||
*/
|
||||
MemberResp getGroupDetail(Long uid, long roomId);
|
||||
|
||||
CursorPageBaseResp<ChatMemberResp> getMemberPage(MemberReq request);
|
||||
|
||||
List<ChatMemberListResp> getMemberList(ChatMessageMemberReq request);
|
||||
|
||||
void delMember(Long uid, MemberDelReq request);
|
||||
|
||||
void addMember(Long uid, MemberAddReq request);
|
||||
|
||||
Long addGroup(Long uid, GroupAddReq request);
|
||||
|
||||
ChatRoomResp getContactDetail(Long uid, Long roomId);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.abin.mallchat.custom.chat.service;
|
||||
|
||||
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatRoomResp;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-07-22
|
||||
*/
|
||||
public interface RoomService {
|
||||
/**
|
||||
* 获取会话列表--支持未登录态
|
||||
*/
|
||||
CursorPageBaseResp<ChatRoomResp> getContactPage(CursorPageBaseReq request, Long uid);
|
||||
}
|
||||
@@ -1,21 +1,29 @@
|
||||
package com.abin.mallchat.custom.chat.service.adapter;
|
||||
|
||||
import cn.hutool.core.lang.Pair;
|
||||
import com.abin.mallchat.common.chat.domain.entity.GroupMember;
|
||||
import com.abin.mallchat.common.chat.domain.enums.GroupRoleEnum;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.domain.entity.UserFriend;
|
||||
import com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSRespTypeEnum;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.WSMemberChange;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.abin.mallchat.common.user.domain.vo.response.ws.WSMemberChange.CHANGE_TYPE_ADD;
|
||||
import static com.abin.mallchat.common.user.domain.vo.response.ws.WSMemberChange.CHANGE_TYPE_REMOVE;
|
||||
|
||||
/**
|
||||
* Description: 成员适配器
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
@@ -27,12 +35,12 @@ public class MemberAdapter {
|
||||
@Autowired
|
||||
private UserCache userCache;
|
||||
|
||||
public List<ChatMemberResp> buildMember(List<Pair<Long, Double>> list, ChatActiveStatusEnum statusEnum) {
|
||||
public static List<ChatMemberResp> buildMember(List<User> list) {
|
||||
return list.stream().map(a -> {
|
||||
ChatMemberResp resp = new ChatMemberResp();
|
||||
resp.setActiveStatus(statusEnum.getStatus());
|
||||
resp.setLastOptTime(new Date(a.getValue().longValue()));
|
||||
resp.setUid(a.getKey());
|
||||
resp.setActiveStatus(a.getActiveStatus());
|
||||
resp.setLastOptTime(a.getLastOptTime());
|
||||
resp.setUid(a.getId());
|
||||
return resp;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
@@ -50,4 +58,53 @@ public class MemberAdapter {
|
||||
return resp;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
public static List<ChatMemberListResp> buildMemberList(List<User> memberList) {
|
||||
return memberList.stream()
|
||||
.map(a -> {
|
||||
ChatMemberListResp resp = new ChatMemberListResp();
|
||||
BeanUtils.copyProperties(a, resp);
|
||||
resp.setUid(a.getId());
|
||||
return resp;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static List<ChatMemberListResp> buildMemberList(Map<Long, User> batch) {
|
||||
return buildMemberList(new ArrayList<>(batch.values()));
|
||||
}
|
||||
|
||||
public static List<GroupMember> buildMemberAdd(Long groupId, List<Long> waitAddUidList) {
|
||||
return waitAddUidList.stream().map(a -> {
|
||||
GroupMember member = new GroupMember();
|
||||
member.setGroupId(groupId);
|
||||
member.setUid(a);
|
||||
member.setRole(GroupRoleEnum.MEMBER.getType());
|
||||
return member;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static WSBaseResp<WSMemberChange> buildMemberAddWS(Long roomId, User user) {
|
||||
WSBaseResp<WSMemberChange> wsBaseResp = new WSBaseResp<>();
|
||||
wsBaseResp.setType(WSRespTypeEnum.MEMBER_CHANGE.getType());
|
||||
WSMemberChange wsMemberChange = new WSMemberChange();
|
||||
wsMemberChange.setActiveStatus(user.getActiveStatus());
|
||||
wsMemberChange.setLastOptTime(user.getLastOptTime());
|
||||
wsMemberChange.setUid(user.getId());
|
||||
wsMemberChange.setRoomId(roomId);
|
||||
wsMemberChange.setChangeType(CHANGE_TYPE_ADD);
|
||||
wsBaseResp.setData(wsMemberChange);
|
||||
return wsBaseResp;
|
||||
}
|
||||
|
||||
public static WSBaseResp<WSMemberChange> buildMemberRemoveWS(Long roomId, Long uid) {
|
||||
WSBaseResp<WSMemberChange> wsBaseResp = new WSBaseResp<>();
|
||||
wsBaseResp.setType(WSRespTypeEnum.MEMBER_CHANGE.getType());
|
||||
WSMemberChange wsMemberChange = new WSMemberChange();
|
||||
wsMemberChange.setUid(uid);
|
||||
wsMemberChange.setRoomId(roomId);
|
||||
wsMemberChange.setChangeType(CHANGE_TYPE_REMOVE);
|
||||
wsBaseResp.setData(wsMemberChange);
|
||||
return wsBaseResp;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,10 @@ import com.abin.mallchat.common.chat.domain.entity.MessageMark;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageStatusEnum;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum;
|
||||
import com.abin.mallchat.common.chat.domain.vo.response.ChatMessageResp;
|
||||
import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum;
|
||||
import com.abin.mallchat.common.user.domain.entity.UserApply;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageReq;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.msg.TextMsgReq;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageResp;
|
||||
import com.abin.mallchat.custom.chat.service.strategy.msg.AbstractMsgHandler;
|
||||
import com.abin.mallchat.custom.chat.service.strategy.msg.MsgHandlerFactory;
|
||||
|
||||
@@ -79,22 +78,6 @@ public class MessageAdapter {
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
public static WSApplyMessage buildApplyResp(UserApply userApply) {
|
||||
WSApplyMessage wsApplyMessage = new WSApplyMessage();
|
||||
wsApplyMessage.setApplyMessage(buildApplyMessage(userApply));
|
||||
wsApplyMessage.setFromUser(buildFromUser(userApply.getUid()));
|
||||
return wsApplyMessage;
|
||||
}
|
||||
|
||||
private static ChatMessageResp.ApplyMessage buildApplyMessage(UserApply userApply) {
|
||||
ChatMessageResp.ApplyMessage applyMessage = new ChatMessageResp.ApplyMessage();
|
||||
applyMessage.setApplyId(userApply.getId());
|
||||
applyMessage.setApplyMsg(userApply.getMsg());
|
||||
applyMessage.setSendTime(userApply.getCreateTime());
|
||||
return applyMessage;
|
||||
}
|
||||
|
||||
|
||||
public static ChatMessageReq buildAgreeMsg(Long roomId) {
|
||||
ChatMessageReq chatMessageReq = new ChatMessageReq();
|
||||
chatMessageReq.setRoomId(roomId);
|
||||
|
||||
@@ -2,11 +2,18 @@ package com.abin.mallchat.custom.chat.service.adapter;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Contact;
|
||||
import com.abin.mallchat.common.chat.domain.entity.GroupMember;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Room;
|
||||
import com.abin.mallchat.common.chat.domain.entity.RoomGroup;
|
||||
import com.abin.mallchat.common.chat.domain.enums.GroupRoleEnum;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageReq;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageReadResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatRoomResp;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -22,7 +29,7 @@ public class RoomAdapter {
|
||||
.map(a -> {
|
||||
ChatRoomResp resp = new ChatRoomResp();
|
||||
BeanUtil.copyProperties(a, resp);
|
||||
resp.setLastActiveTime(a.getActiveTime());
|
||||
resp.setActiveTime(a.getActiveTime());
|
||||
return resp;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
@@ -34,4 +41,31 @@ public class RoomAdapter {
|
||||
return resp;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static List<GroupMember> buildGroupMemberBatch(List<Long> uidList, Long groupId) {
|
||||
return uidList.stream()
|
||||
.distinct()
|
||||
.map(uid -> {
|
||||
GroupMember member = new GroupMember();
|
||||
member.setRole(GroupRoleEnum.MEMBER.getType());
|
||||
member.setUid(uid);
|
||||
member.setGroupId(groupId);
|
||||
return member;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static ChatMessageReq buildGroupAddMessage(RoomGroup groupRoom, User inviter, Map<Long, User> member) {
|
||||
ChatMessageReq chatMessageReq = new ChatMessageReq();
|
||||
chatMessageReq.setRoomId(groupRoom.getRoomId());
|
||||
chatMessageReq.setMsgType(MessageTypeEnum.SYSTEM.getType());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("\"")
|
||||
.append(inviter.getName())
|
||||
.append("\"")
|
||||
.append("邀请")
|
||||
.append(member.values().stream().map(u -> "\"" + u.getName() + "\"").collect(Collectors.joining(",")))
|
||||
.append("加入群聊");
|
||||
chatMessageReq.setBody(sb.toString());
|
||||
return chatMessageReq;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ import com.abin.mallchat.common.chat.domain.dto.MsgReadInfoDTO;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Contact;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.entity.MessageMark;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Room;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum;
|
||||
import com.abin.mallchat.common.chat.domain.vo.response.ChatMessageResp;
|
||||
import com.abin.mallchat.common.chat.service.ContactService;
|
||||
import com.abin.mallchat.common.common.annotation.RedissonLock;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
@@ -23,13 +23,17 @@ import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.common.event.MessageSendEvent;
|
||||
import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
import com.abin.mallchat.common.user.dao.UserDao;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum;
|
||||
import com.abin.mallchat.common.user.domain.enums.RoleEnum;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.common.user.service.IRoleService;
|
||||
import com.abin.mallchat.common.user.service.cache.ItemCache;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberStatisticResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageReadResp;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.MemberAdapter;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.MessageAdapter;
|
||||
@@ -116,26 +120,26 @@ public class ChatServiceImpl implements ChatService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CursorPageBaseResp<ChatMemberResp> getMemberPage(CursorPageBaseReq request) {
|
||||
public CursorPageBaseResp<ChatMemberResp> getMemberPage(List<Long> memberUidList, CursorPageBaseReq request) {
|
||||
Pair<ChatActiveStatusEnum, String> pair = ChatMemberHelper.getCursorPair(request.getCursor());
|
||||
ChatActiveStatusEnum activeStatusEnum = pair.getKey();
|
||||
String timeCursor = pair.getValue();
|
||||
List<ChatMemberResp> resultList = new ArrayList<>();//最终列表
|
||||
Boolean isLast = Boolean.FALSE;
|
||||
if (activeStatusEnum == ChatActiveStatusEnum.ONLINE) {//在线列表
|
||||
CursorPageBaseResp<Pair<Long, Double>> cursorPage = userCache.getOnlineCursorPage(new CursorPageBaseReq(request.getPageSize(), timeCursor));
|
||||
resultList.addAll(memberAdapter.buildMember(cursorPage.getList(), ChatActiveStatusEnum.ONLINE));//添加在线列表
|
||||
CursorPageBaseResp<User> cursorPage = userDao.getCursorPage(memberUidList, request, ChatActiveStatusEnum.ONLINE);
|
||||
resultList.addAll(MemberAdapter.buildMember(cursorPage.getList()));//添加在线列表
|
||||
if (cursorPage.getIsLast()) {//如果是最后一页,从离线列表再补点数据
|
||||
Integer leftSize = request.getPageSize() - cursorPage.getList().size();
|
||||
cursorPage = userCache.getOfflineCursorPage(new CursorPageBaseReq(leftSize, null));
|
||||
resultList.addAll(memberAdapter.buildMember(cursorPage.getList(), ChatActiveStatusEnum.OFFLINE));//添加离线线列表
|
||||
activeStatusEnum = ChatActiveStatusEnum.OFFLINE;
|
||||
Integer leftSize = request.getPageSize() - cursorPage.getList().size();
|
||||
cursorPage = userDao.getCursorPage(memberUidList, new CursorPageBaseReq(leftSize, null), ChatActiveStatusEnum.OFFLINE);
|
||||
resultList.addAll(MemberAdapter.buildMember(cursorPage.getList()));//添加离线线列表
|
||||
}
|
||||
timeCursor = cursorPage.getCursor();
|
||||
isLast = cursorPage.getIsLast();
|
||||
} else if (activeStatusEnum == ChatActiveStatusEnum.OFFLINE) {//离线列表
|
||||
CursorPageBaseResp<Pair<Long, Double>> cursorPage = userCache.getOfflineCursorPage(new CursorPageBaseReq(request.getPageSize(), timeCursor));
|
||||
resultList.addAll(memberAdapter.buildMember(cursorPage.getList(), ChatActiveStatusEnum.OFFLINE));//添加离线线列表
|
||||
CursorPageBaseResp<User> cursorPage = userDao.getCursorPage(memberUidList, request, ChatActiveStatusEnum.OFFLINE);
|
||||
resultList.addAll(MemberAdapter.buildMember(cursorPage.getList()));//添加离线线列表
|
||||
timeCursor = cursorPage.getCursor();
|
||||
isLast = cursorPage.getIsLast();
|
||||
}
|
||||
@@ -152,18 +156,6 @@ public class ChatServiceImpl implements ChatService {
|
||||
return CursorPageBaseResp.init(cursorPage, getMsgRespBatch(cursorPage.getList(), receiveUid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CursorPageBaseResp<ChatRoomResp> getRoomPage(CursorPageBaseReq request, Long uid) {
|
||||
CursorPageBaseResp<Room> cursorPage = roomDao.getCursorPage(request);
|
||||
ArrayList<Room> rooms = new ArrayList<>(cursorPage.getList());
|
||||
if (request.isFirstPage()) {
|
||||
//第一页插入置顶的大群聊
|
||||
Room group = roomDao.getById(ROOM_GROUP_ID);
|
||||
rooms.add(0, group);
|
||||
}
|
||||
return CursorPageBaseResp.init(cursorPage, RoomAdapter.buildResp(rooms));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatMemberStatisticResp getMemberStatistic() {
|
||||
System.out.println(Thread.currentThread().getName());
|
||||
@@ -240,6 +232,24 @@ public class ChatServiceImpl implements ChatService {
|
||||
return CursorPageBaseResp.init(page, RoomAdapter.buildReadResp(page.getList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@RedissonLock(key = "#uid")
|
||||
public void msgRead(Long uid, ChatMessageMemberReq request) {
|
||||
Contact contact = contactDao.get(uid, request.getRoomId());
|
||||
if (Objects.nonNull(contact)) {
|
||||
Contact update = new Contact();
|
||||
update.setId(contact.getId());
|
||||
update.setReadTime(new Date());
|
||||
contactDao.updateById(update);
|
||||
} else {
|
||||
Contact insert = new Contact();
|
||||
insert.setUid(uid);
|
||||
insert.setRoomId(request.getRoomId());
|
||||
insert.setReadTime(new Date());
|
||||
contactDao.save(insert);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkRecall(Long uid, Message message) {
|
||||
AssertUtil.isNotEmpty(message, "消息有误");
|
||||
AssertUtil.notEqual(message.getType(), MessageTypeEnum.RECALL, "消息无法撤回");
|
||||
|
||||
@@ -0,0 +1,377 @@
|
||||
package com.abin.mallchat.custom.chat.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.lang.Pair;
|
||||
import com.abin.mallchat.common.chat.dao.ContactDao;
|
||||
import com.abin.mallchat.common.chat.dao.GroupMemberDao;
|
||||
import com.abin.mallchat.common.chat.dao.MessageDao;
|
||||
import com.abin.mallchat.common.chat.domain.dto.RoomBaseInfo;
|
||||
import com.abin.mallchat.common.chat.domain.entity.*;
|
||||
import com.abin.mallchat.common.chat.domain.enums.GroupRoleEnum;
|
||||
import com.abin.mallchat.common.chat.domain.enums.HotFlagEnum;
|
||||
import com.abin.mallchat.common.chat.domain.enums.RoomTypeEnum;
|
||||
import com.abin.mallchat.common.chat.service.RoomService;
|
||||
import com.abin.mallchat.common.chat.service.adapter.ChatAdapter;
|
||||
import com.abin.mallchat.common.chat.service.cache.*;
|
||||
import com.abin.mallchat.common.common.annotation.RedissonLock;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.common.event.GroupMemberAddEvent;
|
||||
import com.abin.mallchat.common.common.event.WSPushEvent;
|
||||
import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
import com.abin.mallchat.common.user.dao.UserDao;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.domain.enums.RoleEnum;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.WSMemberChange;
|
||||
import com.abin.mallchat.common.user.service.IRoleService;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.common.user.service.cache.UserInfoCache;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.enums.GroupRoleAPPEnum;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatRoomResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.MemberResp;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.custom.chat.service.RoomAppService;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.MemberAdapter;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.RoomAdapter;
|
||||
import com.abin.mallchat.custom.chat.service.strategy.msg.AbstractMsgHandler;
|
||||
import com.abin.mallchat.custom.chat.service.strategy.msg.MsgHandlerFactory;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.redis.core.ZSetOperations;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-07-22
|
||||
*/
|
||||
@Service
|
||||
public class RoomAppServiceImpl implements RoomAppService {
|
||||
|
||||
@Autowired
|
||||
private ContactDao contactDao;
|
||||
@Autowired
|
||||
private RoomCache roomCache;
|
||||
@Autowired
|
||||
private RoomGroupCache roomGroupCache;
|
||||
@Autowired
|
||||
private RoomFriendCache roomFriendCache;
|
||||
@Autowired
|
||||
private UserInfoCache userInfoCache;
|
||||
@Autowired
|
||||
private MessageDao messageDao;
|
||||
@Autowired
|
||||
private HotRoomCache hotRoomCache;
|
||||
@Autowired
|
||||
private UserCache userCache;
|
||||
@Autowired
|
||||
private GroupMemberDao groupMemberDao;
|
||||
@Autowired
|
||||
private UserDao userDao;
|
||||
@Autowired
|
||||
private ChatService chatService;
|
||||
@Autowired
|
||||
private IRoleService iRoleService;
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
@Autowired
|
||||
private RoomService roomService;
|
||||
@Autowired
|
||||
private GroupMemberCache groupMemberCache;
|
||||
|
||||
@Override
|
||||
public CursorPageBaseResp<ChatRoomResp> getContactPage(CursorPageBaseReq request, Long uid) {
|
||||
//查出用户要展示的会话列表
|
||||
CursorPageBaseResp<Long> page;
|
||||
if (Objects.nonNull(uid)) {
|
||||
Double hotStart = getCursorOrNull(request.getCursor());
|
||||
Double hotEnd;
|
||||
//用户基础会话
|
||||
CursorPageBaseResp<Contact> contactPage = contactDao.getContactPage(uid, request);
|
||||
List<Long> baseRoomIds = contactPage.getList().stream().map(Contact::getRoomId).collect(Collectors.toList());
|
||||
hotEnd = getCursorOrNull(contactPage.getCursor());
|
||||
//热门房间
|
||||
Set<ZSetOperations.TypedTuple<String>> typedTuples = hotRoomCache.getRoomRange(hotStart, hotEnd);
|
||||
List<Long> hotRoomIds = typedTuples.stream().map(ZSetOperations.TypedTuple::getValue).filter(Objects::nonNull).map(Long::parseLong).collect(Collectors.toList());
|
||||
baseRoomIds.addAll(hotRoomIds);
|
||||
//基础会话和热门房间合并
|
||||
page = CursorPageBaseResp.init(contactPage, baseRoomIds);
|
||||
} else {//用户未登录,只查全局房间
|
||||
CursorPageBaseResp<Pair<Long, Double>> roomCursorPage = hotRoomCache.getRoomCursorPage(request);
|
||||
List<Long> roomIds = roomCursorPage.getList().stream().map(Pair::getKey).collect(Collectors.toList());
|
||||
page = CursorPageBaseResp.init(roomCursorPage, roomIds);
|
||||
}
|
||||
//最后组装会话信息(名称,头像,未读数等)
|
||||
List<ChatRoomResp> result = buildContactResp(uid, page.getList());
|
||||
return CursorPageBaseResp.init(page, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatRoomResp getContactDetail(Long uid, Long roomId) {
|
||||
Room room = roomCache.get(roomId);
|
||||
AssertUtil.isNotEmpty(room, "房间号有误");
|
||||
return buildContactResp(uid, Collections.singletonList(roomId)).get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MemberResp getGroupDetail(Long uid, long roomId) {
|
||||
RoomGroup roomGroup = roomGroupCache.get(roomId);
|
||||
Room room = roomCache.get(roomId);
|
||||
AssertUtil.isNotEmpty(roomGroup, "roomId有误");
|
||||
Long onlineNum;
|
||||
if (isHotGroup(room)) {//热点群从redis取人数
|
||||
onlineNum = userCache.getOnlineNum();
|
||||
} else {
|
||||
List<Long> memberUidList = groupMemberDao.getMemberUidList(roomGroup.getId());
|
||||
onlineNum = userDao.getOnlineCount(memberUidList).longValue();
|
||||
}
|
||||
GroupRoleAPPEnum groupRole = getGroupRole(uid, roomGroup, room);
|
||||
return MemberResp.builder()
|
||||
.avatar(roomGroup.getAvatar())
|
||||
.roomId(roomId)
|
||||
.groupName(roomGroup.getName())
|
||||
.onlineNum(onlineNum)
|
||||
.role(groupRole.getType())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CursorPageBaseResp<ChatMemberResp> getMemberPage(MemberReq request) {
|
||||
Room room = roomCache.get(request.getRoomId());
|
||||
AssertUtil.isNotEmpty(room, "房间号有误");
|
||||
List<Long> memberUidList = null;
|
||||
if (isHotGroup(room)) {//全员群展示所有用户
|
||||
memberUidList = null;
|
||||
} else {//只展示房间内的群成员
|
||||
RoomGroup roomGroup = roomGroupCache.get(request.getRoomId());
|
||||
memberUidList = groupMemberDao.getMemberUidList(roomGroup.getId());
|
||||
}
|
||||
return chatService.getMemberPage(memberUidList, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "member", key = "'memberList.'+#request.roomId")
|
||||
public List<ChatMemberListResp> getMemberList(ChatMessageMemberReq request) {
|
||||
Room room = roomCache.get(request.getRoomId());
|
||||
AssertUtil.isNotEmpty(room, "房间号有误");
|
||||
if (isHotGroup(room)) {//全员群展示所有用户100名
|
||||
List<User> memberList = userDao.getMemberList();
|
||||
return MemberAdapter.buildMemberList(memberList);
|
||||
} else {
|
||||
RoomGroup roomGroup = roomGroupCache.get(request.getRoomId());
|
||||
List<Long> memberUidList = groupMemberDao.getMemberUidList(roomGroup.getId());
|
||||
Map<Long, User> batch = userInfoCache.getBatch(memberUidList);
|
||||
return MemberAdapter.buildMemberList(batch);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delMember(Long uid, MemberDelReq request) {
|
||||
Room room = roomCache.get(request.getRoomId());
|
||||
AssertUtil.isNotEmpty(room, "房间号有误");
|
||||
RoomGroup roomGroup = roomGroupCache.get(request.getRoomId());
|
||||
AssertUtil.isNotEmpty(roomGroup, "房间号有误");
|
||||
GroupMember self = groupMemberDao.getMember(roomGroup.getId(), uid);
|
||||
AssertUtil.isNotEmpty(self, "您不是群管理");
|
||||
AssertUtil.isTrue(hasPower(self), "您不是群管理");
|
||||
GroupMember member = groupMemberDao.getMember(roomGroup.getId(), request.getUid());
|
||||
AssertUtil.isNotEmpty(self, "用户已经移除");
|
||||
groupMemberDao.removeById(member.getId());
|
||||
//发送移除事件告知群成员
|
||||
List<Long> memberUidList = groupMemberCache.getMemberUidList(roomGroup.getRoomId());
|
||||
WSBaseResp<WSMemberChange> ws = MemberAdapter.buildMemberRemoveWS(roomGroup.getRoomId(), member.getUid());
|
||||
applicationEventPublisher.publishEvent(new WSPushEvent(this, memberUidList, ws));
|
||||
groupMemberCache.evictMemberUidList(member.getId());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@RedissonLock(key = "#request.roomId")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void addMember(Long uid, MemberAddReq request) {
|
||||
Room room = roomCache.get(request.getRoomId());
|
||||
AssertUtil.isNotEmpty(room, "房间号有误");
|
||||
AssertUtil.isFalse(isHotGroup(room), "全员群无需邀请好友");
|
||||
RoomGroup roomGroup = roomGroupCache.get(request.getRoomId());
|
||||
AssertUtil.isNotEmpty(roomGroup, "房间号有误");
|
||||
GroupMember self = groupMemberDao.getMember(roomGroup.getId(), uid);
|
||||
AssertUtil.isNotEmpty(self, "您不是群成员");
|
||||
List<Long> memberBatch = groupMemberDao.getMemberBatch(roomGroup.getId(), request.getUidList());
|
||||
Set<Long> existUid = new HashSet<>(memberBatch);
|
||||
List<Long> waitAddUidList = request.getUidList().stream().filter(a -> !existUid.contains(a)).collect(Collectors.toList());
|
||||
if (CollectionUtils.isEmpty(waitAddUidList)) {
|
||||
return;
|
||||
}
|
||||
List<GroupMember> groupMembers = MemberAdapter.buildMemberAdd(roomGroup.getId(), waitAddUidList);
|
||||
groupMemberDao.saveBatch(groupMembers);
|
||||
applicationEventPublisher.publishEvent(new GroupMemberAddEvent(this, roomGroup, groupMembers, uid));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Long addGroup(Long uid, GroupAddReq request) {
|
||||
RoomGroup roomGroup = roomService.createGroupRoom(uid);
|
||||
//批量保存群成员
|
||||
List<GroupMember> groupMembers = RoomAdapter.buildGroupMemberBatch(request.getUidList(), roomGroup.getId());
|
||||
groupMemberDao.saveBatch(groupMembers);
|
||||
//发送邀请加群消息==》触发每个人的会话
|
||||
applicationEventPublisher.publishEvent(new GroupMemberAddEvent(this, roomGroup, groupMembers, uid));
|
||||
return roomGroup.getRoomId();
|
||||
}
|
||||
|
||||
private boolean hasPower(GroupMember self) {
|
||||
return Objects.equals(self.getRole(), GroupRoleEnum.LEADER.getType())
|
||||
|| Objects.equals(self.getRole(), GroupRoleEnum.MANAGER.getType())
|
||||
|| iRoleService.hasPower(self.getUid(), RoleEnum.ADMIN);
|
||||
|
||||
}
|
||||
|
||||
private GroupRoleAPPEnum getGroupRole(Long uid, RoomGroup roomGroup, Room room) {
|
||||
GroupMember member = Objects.isNull(uid) ? null : groupMemberDao.getMember(roomGroup.getId(), uid);
|
||||
if (Objects.nonNull(member)) {
|
||||
return GroupRoleAPPEnum.of(member.getRole());
|
||||
} else if (isHotGroup(room)) {
|
||||
return GroupRoleAPPEnum.MEMBER;
|
||||
} else {
|
||||
return GroupRoleAPPEnum.REMOVE;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isHotGroup(Room room) {
|
||||
return HotFlagEnum.YES.getType().equals(room.getHotFlag());
|
||||
}
|
||||
|
||||
private List<Contact> buildContact(List<Pair<Long, Double>> list, Long uid) {
|
||||
List<Long> roomIds = list.stream().map(Pair::getKey).collect(Collectors.toList());
|
||||
Map<Long, Room> batch = roomCache.getBatch(roomIds);
|
||||
Map<Long, Contact> contactMap = new HashMap<>();
|
||||
if (Objects.nonNull(uid)) {
|
||||
List<Contact> byRoomIds = contactDao.getByRoomIds(roomIds, uid);
|
||||
contactMap = byRoomIds.stream().collect(Collectors.toMap(Contact::getRoomId, Function.identity()));
|
||||
}
|
||||
Map<Long, Contact> finalContactMap = contactMap;
|
||||
return list.stream().map(pair -> {
|
||||
Long roomId = pair.getKey();
|
||||
Contact contact = finalContactMap.get(roomId);
|
||||
if (Objects.isNull(contact)) {
|
||||
contact = new Contact();
|
||||
contact.setRoomId(pair.getKey());
|
||||
Room room = batch.get(roomId);
|
||||
contact.setLastMsgId(room.getLastMsgId());
|
||||
}
|
||||
contact.setActiveTime(new Date(pair.getValue().longValue()));
|
||||
return contact;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private Double getCursorOrNull(String cursor) {
|
||||
return Optional.ofNullable(cursor).map(Double::parseDouble).orElse(null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<ChatRoomResp> buildContactResp(Long uid, List<Long> roomIds) {
|
||||
//表情和头像
|
||||
Map<Long, RoomBaseInfo> roomBaseInfoMap = getRoomBaseInfoMap(roomIds, uid);
|
||||
//最后一条消息
|
||||
List<Long> msgIds = roomBaseInfoMap.values().stream().map(RoomBaseInfo::getLastMsgId).collect(Collectors.toList());
|
||||
List<Message> messages = CollectionUtil.isEmpty(msgIds) ? new ArrayList<>() : messageDao.listByIds(msgIds);
|
||||
Map<Long, Message> msgMap = messages.stream().collect(Collectors.toMap(Message::getId, Function.identity()));
|
||||
Map<Long, User> lastMsgUidMap = userInfoCache.getBatch(messages.stream().map(Message::getFromUid).collect(Collectors.toList()));
|
||||
//消息未读数
|
||||
Map<Long, Integer> unReadCountMap = getUnReadCountMap(uid, roomIds);
|
||||
return roomBaseInfoMap.values().stream().map(room -> {
|
||||
ChatRoomResp resp = new ChatRoomResp();
|
||||
RoomBaseInfo roomBaseInfo = roomBaseInfoMap.get(room.getRoomId());
|
||||
resp.setAvatar(roomBaseInfo.getAvatar());
|
||||
resp.setRoomId(room.getRoomId());
|
||||
resp.setActiveTime(room.getActiveTime());
|
||||
resp.setHot_Flag(roomBaseInfo.getHotFlag());
|
||||
resp.setType(roomBaseInfo.getType());
|
||||
resp.setName(roomBaseInfo.getName());
|
||||
Message message = msgMap.get(room.getLastMsgId());
|
||||
if (Objects.nonNull(message)) {
|
||||
AbstractMsgHandler strategyNoNull = MsgHandlerFactory.getStrategyNoNull(message.getType());
|
||||
resp.setText(lastMsgUidMap.get(message.getFromUid()).getName() + ":" + strategyNoNull.showContactMsg(message));
|
||||
}
|
||||
resp.setUnreadCount(unReadCountMap.getOrDefault(room.getRoomId(), 0));
|
||||
return resp;
|
||||
}).sorted(Comparator.comparing(ChatRoomResp::getActiveTime).reversed())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取未读数
|
||||
*/
|
||||
private Map<Long, Integer> getUnReadCountMap(Long uid, List<Long> roomIds) {
|
||||
if (Objects.isNull(uid)) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
List<Contact> contacts = contactDao.getByRoomIds(roomIds, uid);
|
||||
return contacts.parallelStream()
|
||||
.map(contact -> Pair.of(contact.getRoomId(), messageDao.getUnReadCount(contact.getRoomId(), contact.getReadTime())))
|
||||
.collect(Collectors.toMap(Pair::getKey, Pair::getValue));
|
||||
}
|
||||
|
||||
private Map<Long, User> getFriendRoomMap(List<Long> roomIds, Long uid) {
|
||||
if (CollectionUtil.isEmpty(roomIds)) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
Map<Long, RoomFriend> roomFriendMap = roomFriendCache.getBatch(roomIds);
|
||||
Set<Long> friendUidSet = ChatAdapter.getFriendUidSet(roomFriendMap.values(), uid);
|
||||
Map<Long, User> userBatch = userInfoCache.getBatch(new ArrayList<>(friendUidSet));
|
||||
return roomFriendMap.values()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(RoomFriend::getRoomId, roomFriend -> {
|
||||
Long friendUid = ChatAdapter.getFriendUid(roomFriend, uid);
|
||||
return userBatch.get(friendUid);
|
||||
}));
|
||||
}
|
||||
|
||||
private Map<Long, RoomBaseInfo> getRoomBaseInfoMap(List<Long> roomIds, Long uid) {
|
||||
Map<Long, Room> roomMap = roomCache.getBatch(roomIds);
|
||||
//房间根据好友和群组类型分组
|
||||
Map<Integer, List<Long>> groupRoomIdMap = roomMap.values().stream().collect(Collectors.groupingBy(Room::getType,
|
||||
Collectors.mapping(Room::getId, Collectors.toList())));
|
||||
//获取群组信息
|
||||
List<Long> groupRoomId = groupRoomIdMap.get(RoomTypeEnum.GROUP.getType());
|
||||
Map<Long, RoomGroup> roomInfoBatch = roomGroupCache.getBatch(groupRoomId);
|
||||
//获取好友信息
|
||||
List<Long> friendRoomId = groupRoomIdMap.get(RoomTypeEnum.FRIEND.getType());
|
||||
Map<Long, User> friendRoomMap = getFriendRoomMap(friendRoomId, uid);
|
||||
|
||||
return roomMap.values().stream().map(room -> {
|
||||
RoomBaseInfo roomBaseInfo = new RoomBaseInfo();
|
||||
roomBaseInfo.setRoomId(room.getId());
|
||||
roomBaseInfo.setType(room.getType());
|
||||
roomBaseInfo.setHotFlag(room.getHotFlag());
|
||||
roomBaseInfo.setLastMsgId(room.getLastMsgId());
|
||||
roomBaseInfo.setActiveTime(room.getActiveTime());
|
||||
if (RoomTypeEnum.of(room.getType()) == RoomTypeEnum.GROUP) {
|
||||
RoomGroup roomGroup = roomInfoBatch.get(room.getId());
|
||||
roomBaseInfo.setName(roomGroup.getName());
|
||||
roomBaseInfo.setAvatar(roomGroup.getAvatar());
|
||||
} else if (RoomTypeEnum.of(room.getType()) == RoomTypeEnum.FRIEND) {
|
||||
User user = friendRoomMap.get(room.getId());
|
||||
roomBaseInfo.setName(user.getName());
|
||||
roomBaseInfo.setAvatar(user.getAvatar());
|
||||
}
|
||||
return roomBaseInfo;
|
||||
}).collect(Collectors.toMap(RoomBaseInfo::getRoomId, Function.identity()));
|
||||
}
|
||||
|
||||
private void fillRoomActive(Long uid, Map<Long, Room> roomMap) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
package com.abin.mallchat.custom.chat.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.abin.mallchat.common.chat.dao.ContactDao;
|
||||
import com.abin.mallchat.common.chat.dao.MessageDao;
|
||||
import com.abin.mallchat.common.chat.domain.dto.RoomBaseInfo;
|
||||
import com.abin.mallchat.common.chat.domain.entity.*;
|
||||
import com.abin.mallchat.common.chat.domain.enums.RoomTypeEnum;
|
||||
import com.abin.mallchat.common.chat.service.adapter.ChatAdapter;
|
||||
import com.abin.mallchat.common.chat.service.cache.RoomCache;
|
||||
import com.abin.mallchat.common.chat.service.cache.RoomFriendCache;
|
||||
import com.abin.mallchat.common.chat.service.cache.RoomGroupCache;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.service.cache.UserInfoCache;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatRoomResp;
|
||||
import com.abin.mallchat.custom.chat.service.RoomService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-07-22
|
||||
*/
|
||||
@Service
|
||||
public class RoomServiceImpl implements RoomService {
|
||||
|
||||
@Autowired
|
||||
private ContactDao contactDao;
|
||||
@Autowired
|
||||
private RoomCache roomCache;
|
||||
@Autowired
|
||||
private RoomGroupCache roomGroupCache;
|
||||
@Autowired
|
||||
private RoomFriendCache roomFriendCache;
|
||||
@Autowired
|
||||
private UserInfoCache userInfoCache;
|
||||
@Autowired
|
||||
private MessageDao messageDao;
|
||||
|
||||
@Override
|
||||
public CursorPageBaseResp<ChatRoomResp> getContactPage(CursorPageBaseReq request, Long uid) {
|
||||
|
||||
if (Objects.nonNull(uid)) {
|
||||
CursorPageBaseResp<Contact> contactPage = contactDao.getContactPage(uid, request);
|
||||
List<Long> roomIds = contactPage.getList().stream().map(Contact::getRoomId).collect(Collectors.toList());
|
||||
//表情和头像
|
||||
Map<Long, RoomBaseInfo> roomBaseInfoMap = getRoomBaseInfoMap(roomIds, uid);
|
||||
//最后一条消息
|
||||
List<Long> msgIds = contactPage.getList().stream().map(Contact::getLastMsgId).collect(Collectors.toList());
|
||||
List<Message> messages = messageDao.listByIds(msgIds);
|
||||
Map<Long, Message> msgMap = messages.stream().collect(Collectors.toMap(Message::getId, Function.identity()));
|
||||
List<ChatRoomResp> collect = contactPage.getList().stream().map(contact -> {
|
||||
ChatRoomResp resp = new ChatRoomResp();
|
||||
BeanUtil.copyProperties(contact, resp);
|
||||
RoomBaseInfo roomBaseInfo = roomBaseInfoMap.get(contact.getRoomId());
|
||||
resp.setAvatar(roomBaseInfo.getAvatar());
|
||||
resp.setName(roomBaseInfo.getName());
|
||||
Message message = msgMap.get(contact.getLastMsgId());
|
||||
if (Objects.nonNull(message)) {
|
||||
resp.setText();
|
||||
}
|
||||
return resp;
|
||||
}).collect(Collectors.toList());
|
||||
CursorPageBaseResp.init(contactPage, collect);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map<Long, User> getFriendRoomMap(List<Long> roomIds, Long uid) {
|
||||
Map<Long, RoomFriend> roomFriendMap = roomFriendCache.getBatch(roomIds);
|
||||
Set<Long> friendUidSet = ChatAdapter.getFriendUidSet(roomFriendMap.values(), uid);
|
||||
Map<Long, User> userBatch = userInfoCache.getBatch(new ArrayList<>(friendUidSet));
|
||||
return roomFriendMap.values()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(RoomFriend::getRoomId, roomFriend -> {
|
||||
Long friendUid = ChatAdapter.getFriendUid(roomFriend, uid);
|
||||
return userBatch.get(friendUid);
|
||||
}));
|
||||
}
|
||||
|
||||
private Map<Long, RoomBaseInfo> getRoomBaseInfoMap(List<Long> roomIds, Long uid) {
|
||||
Map<Long, Room> roomMap = roomCache.getBatch(roomIds);
|
||||
Map<Integer, List<Long>> groupRoomIdMap = roomMap.values().stream().collect(Collectors.groupingBy(Room::getType,
|
||||
Collectors.mapping(Room::getId, Collectors.toList())));
|
||||
//获取群组信息
|
||||
List<Long> groupRoomId = groupRoomIdMap.get(RoomTypeEnum.GROUP.getType());
|
||||
Map<Long, RoomGroup> roomInfoBatch = roomGroupCache.getBatch(groupRoomId);
|
||||
//获取好友信息
|
||||
List<Long> friendRoomId = groupRoomIdMap.get(RoomTypeEnum.FRIEND.getType());
|
||||
Map<Long, User> friendRoomMap = getFriendRoomMap(friendRoomId, uid);
|
||||
return roomMap.values().stream().map(room -> {
|
||||
RoomBaseInfo roomBaseInfo = new RoomBaseInfo();
|
||||
roomBaseInfo.setRoomId(room.getId());
|
||||
if (RoomTypeEnum.of(room.getType()) == RoomTypeEnum.GROUP) {
|
||||
RoomGroup roomGroup = roomInfoBatch.get(room.getId());
|
||||
roomBaseInfo.setName(roomGroup.getName());
|
||||
roomBaseInfo.setAvatar(roomGroup.getAvatar());
|
||||
} else if (RoomTypeEnum.of(room.getType()) == RoomTypeEnum.FRIEND) {
|
||||
User user = friendRoomMap.get(room.getId());
|
||||
roomBaseInfo.setName(user.getName());
|
||||
roomBaseInfo.setAvatar(user.getAvatar());
|
||||
}
|
||||
return roomBaseInfo;
|
||||
}).collect(Collectors.toMap(RoomBaseInfo::getRoomId, Function.identity()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,4 +43,9 @@ public abstract class AbstractMsgHandler {
|
||||
*/
|
||||
public abstract Object showReplyMsg(Message msg);
|
||||
|
||||
/**
|
||||
* 会话列表——展示的消息
|
||||
*/
|
||||
public abstract String showContactMsg(Message msg);
|
||||
|
||||
}
|
||||
|
||||
@@ -54,4 +54,9 @@ public class EmojisMsgHandler extends AbstractMsgHandler {
|
||||
public Object showReplyMsg(Message msg) {
|
||||
return "表情";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showContactMsg(Message msg) {
|
||||
return "[表情包]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,4 +54,9 @@ public class FileMsgHandler extends AbstractMsgHandler {
|
||||
public Object showReplyMsg(Message msg) {
|
||||
return "文件:" + msg.getExtra().getFileMsg().getFileName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showContactMsg(Message msg) {
|
||||
return "[文件]" + msg.getExtra().getFileMsg().getFileName();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,4 +54,9 @@ public class ImgMsgHandler extends AbstractMsgHandler {
|
||||
public Object showReplyMsg(Message msg) {
|
||||
return "图片";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showContactMsg(Message msg) {
|
||||
return "[图片]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,4 +76,9 @@ public class RecallMsgHandler extends AbstractMsgHandler {
|
||||
applicationEventPublisher.publishEvent(new MessageRecallEvent(this, new ChatMsgRecallDTO(message.getId(), message.getRoomId(), recallUid)));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showContactMsg(Message msg) {
|
||||
return "撤回了一条消息";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,4 +54,9 @@ public class SoundMsgHandler extends AbstractMsgHandler {
|
||||
public Object showReplyMsg(Message msg) {
|
||||
return "语音";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showContactMsg(Message msg) {
|
||||
return "[语音]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.abin.mallchat.custom.chat.service.strategy.msg;
|
||||
|
||||
import com.abin.mallchat.common.chat.dao.MessageDao;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageReq;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Description:系统消息
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-06-04
|
||||
*/
|
||||
@Component
|
||||
public class SystemMsgHandler extends AbstractMsgHandler {
|
||||
|
||||
@Autowired
|
||||
private MessageDao messageDao;
|
||||
|
||||
@Override
|
||||
MessageTypeEnum getMsgTypeEnum() {
|
||||
return MessageTypeEnum.SYSTEM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkMsg(ChatMessageReq request, Long uid) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveMsg(Message msg, ChatMessageReq request) {
|
||||
Message update = new Message();
|
||||
update.setId(msg.getId());
|
||||
update.setContent((String) request.getBody());
|
||||
messageDao.updateById(update);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object showMsg(Message msg) {
|
||||
return msg.getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object showReplyMsg(Message msg) {
|
||||
return msg.getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showContactMsg(Message msg) {
|
||||
return msg.getContent();
|
||||
}
|
||||
}
|
||||
@@ -135,4 +135,9 @@ public class TextMsgHandler extends AbstractMsgHandler {
|
||||
public Object showReplyMsg(Message msg) {
|
||||
return msg.getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showContactMsg(Message msg) {
|
||||
return msg.getContent();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,4 +54,9 @@ public class VideoMsgHandler extends AbstractMsgHandler {
|
||||
public Object showReplyMsg(Message msg) {
|
||||
return "视频";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String showContactMsg(Message msg) {
|
||||
return "[视频]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.abin.mallchat.custom.common.event.listener;
|
||||
|
||||
import com.abin.mallchat.common.chat.dao.GroupMemberDao;
|
||||
import com.abin.mallchat.common.chat.domain.entity.GroupMember;
|
||||
import com.abin.mallchat.common.chat.domain.entity.RoomGroup;
|
||||
import com.abin.mallchat.common.chat.service.cache.GroupMemberCache;
|
||||
import com.abin.mallchat.common.chat.service.cache.MsgCache;
|
||||
import com.abin.mallchat.common.common.event.GroupMemberAddEvent;
|
||||
import com.abin.mallchat.common.common.event.WSPushEvent;
|
||||
import com.abin.mallchat.common.user.dao.UserDao;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.WSMemberChange;
|
||||
import com.abin.mallchat.common.user.service.cache.UserInfoCache;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageReq;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.MemberAdapter;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.RoomAdapter;
|
||||
import com.abin.mallchat.custom.user.service.WebSocketService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.event.TransactionalEventListener;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 添加群成员监听器
|
||||
*
|
||||
* @author zhongzb create on 2022/08/26
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class GroupMemberAddListener {
|
||||
@Autowired
|
||||
private WebSocketService webSocketService;
|
||||
@Autowired
|
||||
private ChatService chatService;
|
||||
@Autowired
|
||||
private MsgCache msgCache;
|
||||
@Autowired
|
||||
private UserInfoCache userInfoCache;
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
@Autowired
|
||||
private GroupMemberDao groupMemberDao;
|
||||
@Autowired
|
||||
private UserDao userDao;
|
||||
@Autowired
|
||||
private GroupMemberCache groupMemberCache;
|
||||
|
||||
|
||||
@Async
|
||||
@TransactionalEventListener(classes = GroupMemberAddEvent.class, fallbackExecution = true)
|
||||
public void sendAddMsg(GroupMemberAddEvent event) {
|
||||
List<GroupMember> memberList = event.getMemberList();
|
||||
RoomGroup roomGroup = event.getRoomGroup();
|
||||
Long inviteUid = event.getInviteUid();
|
||||
User user = userInfoCache.get(inviteUid);
|
||||
List<Long> uidList = memberList.stream().map(GroupMember::getUid).collect(Collectors.toList());
|
||||
ChatMessageReq chatMessageReq = RoomAdapter.buildGroupAddMessage(roomGroup, user, userInfoCache.getBatch(uidList));
|
||||
chatService.sendMsg(chatMessageReq, User.UID_SYSTEM);
|
||||
}
|
||||
|
||||
@Async
|
||||
|
||||
@TransactionalEventListener(classes = GroupMemberAddEvent.class, fallbackExecution = true)
|
||||
public void sendChangePush(GroupMemberAddEvent event) {
|
||||
List<GroupMember> memberList = event.getMemberList();
|
||||
RoomGroup roomGroup = event.getRoomGroup();
|
||||
List<Long> memberUidList = groupMemberCache.getMemberUidList(roomGroup.getRoomId());
|
||||
List<Long> uidList = memberList.stream().map(GroupMember::getUid).collect(Collectors.toList());
|
||||
List<User> users = userDao.listByIds(uidList);
|
||||
users.forEach(user -> {
|
||||
WSBaseResp<WSMemberChange> ws = MemberAdapter.buildMemberAddWS(roomGroup.getRoomId(), user);
|
||||
applicationEventPublisher.publishEvent(new WSPushEvent(this, memberUidList, ws));
|
||||
});
|
||||
//移除缓存
|
||||
groupMemberCache.evictMemberUidList(roomGroup.getRoomId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +1,38 @@
|
||||
package com.abin.mallchat.custom.common.event.listener;
|
||||
|
||||
import com.abin.mallchat.common.chat.dao.ContactDao;
|
||||
import com.abin.mallchat.common.chat.dao.MessageDao;
|
||||
import com.abin.mallchat.common.chat.dao.RoomDao;
|
||||
import com.abin.mallchat.common.chat.dao.RoomFriendDao;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Room;
|
||||
import com.abin.mallchat.common.chat.domain.entity.RoomFriend;
|
||||
import com.abin.mallchat.common.chat.domain.enums.HotFlagEnum;
|
||||
import com.abin.mallchat.common.chat.domain.enums.RoomTypeEnum;
|
||||
import com.abin.mallchat.common.chat.domain.vo.response.ChatMessageResp;
|
||||
import com.abin.mallchat.common.chat.service.cache.GroupMemberCache;
|
||||
import com.abin.mallchat.common.chat.service.cache.HotRoomCache;
|
||||
import com.abin.mallchat.common.chat.service.cache.RoomCache;
|
||||
import com.abin.mallchat.common.common.event.MessageSendEvent;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageResp;
|
||||
import com.abin.mallchat.common.common.event.WSPushEvent;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.custom.chat.service.WeChatMsgOperationService;
|
||||
import com.abin.mallchat.custom.chat.service.impl.WeChatMsgOperationServiceImpl;
|
||||
import com.abin.mallchat.custom.chatai.service.IChatAIService;
|
||||
import com.abin.mallchat.custom.user.service.WebSocketService;
|
||||
import com.abin.mallchat.custom.user.service.adapter.WSAdapter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.event.TransactionalEventListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -35,22 +51,67 @@ public class MessageSendListener {
|
||||
private MessageDao messageDao;
|
||||
@Autowired
|
||||
private IChatAIService openAIService;
|
||||
|
||||
@Autowired
|
||||
WeChatMsgOperationService weChatMsgOperationService;
|
||||
@Autowired
|
||||
private RoomCache roomCache;
|
||||
@Autowired
|
||||
private RoomDao roomDao;
|
||||
@Autowired
|
||||
private GroupMemberCache groupMemberCache;
|
||||
@Autowired
|
||||
private UserCache userCache;
|
||||
@Autowired
|
||||
private RoomFriendDao roomFriendDao;
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
@Autowired
|
||||
private ContactDao contactDao;
|
||||
@Autowired
|
||||
private HotRoomCache hotRoomCache;
|
||||
|
||||
@Async
|
||||
@TransactionalEventListener(classes = MessageSendEvent.class, fallbackExecution = true)
|
||||
public void notifyAllOnline(MessageSendEvent event) {
|
||||
public void messageRoute(MessageSendEvent event) {
|
||||
Message message = messageDao.getById(event.getMsgId());
|
||||
Room room = roomCache.get(message.getRoomId());
|
||||
ChatMessageResp msgResp = chatService.getMsgResp(message, null);
|
||||
webSocketService.sendToAllOnline(WSAdapter.buildMsgSend(msgResp), message.getFromUid());
|
||||
//更新房间最新消息
|
||||
roomDao.refreshActiveTime(room.getId(), message.getId(), message.getCreateTime());
|
||||
roomCache.delete(room.getId());
|
||||
if (isHotRoom(room)) {//热门群聊推送所有在线的人
|
||||
//更新热门群聊列表
|
||||
hotRoomCache.refreshActiveTime(room.getId(), message.getCreateTime());
|
||||
//推送所有人
|
||||
applicationEventPublisher.publishEvent(new WSPushEvent(this, WSAdapter.buildMsgSend(msgResp)));
|
||||
} else {
|
||||
List<Long> memberUidList = new ArrayList<>();
|
||||
if (Objects.equals(room.getType(), RoomTypeEnum.GROUP.getType())) {//普通群聊推送所有群成员
|
||||
memberUidList = groupMemberCache.getMemberUidList(room.getId());
|
||||
} else if (Objects.equals(room.getType(), RoomTypeEnum.FRIEND.getType())) {//单聊对象
|
||||
//对单人推送
|
||||
RoomFriend roomFriend = roomFriendDao.getByRoomId(room.getId());
|
||||
memberUidList = Arrays.asList(roomFriend.getUid1(), roomFriend.getUid2());
|
||||
}
|
||||
//更新所有群成员的会话时间
|
||||
contactDao.refreshOrCreateActiveTime(room.getId(), memberUidList, message.getId(), message.getCreateTime());
|
||||
//推送房间成员
|
||||
applicationEventPublisher.publishEvent(new WSPushEvent(this, memberUidList, WSAdapter.buildMsgSend(msgResp)));
|
||||
}
|
||||
}
|
||||
|
||||
@TransactionalEventListener(classes = MessageSendEvent.class, fallbackExecution = true)
|
||||
public void handlerMsg(@NotNull MessageSendEvent event) {
|
||||
Message message = messageDao.getById(event.getMsgId());
|
||||
openAIService.chat(message);
|
||||
Room room = roomCache.get(message.getRoomId());
|
||||
if (isHotRoom(room)) {
|
||||
openAIService.chat(message);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public boolean isHotRoom(Room room) {
|
||||
return Objects.equals(HotFlagEnum.YES.getType(), room.getHotFlag());
|
||||
}
|
||||
|
||||
@TransactionalEventListener(classes = MessageSendEvent.class, fallbackExecution = true)
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.abin.mallchat.custom.common.event.listener;
|
||||
import com.abin.mallchat.common.common.event.UserApplyEvent;
|
||||
import com.abin.mallchat.common.user.dao.UserApplyDao;
|
||||
import com.abin.mallchat.common.user.domain.entity.UserApply;
|
||||
import com.abin.mallchat.custom.user.domain.vo.response.ws.WSFriendApply;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.WSFriendApply;
|
||||
import com.abin.mallchat.custom.user.service.WebSocketService;
|
||||
import com.abin.mallchat.custom.user.service.adapter.WSAdapter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -30,7 +30,7 @@ public class UserApplyListener {
|
||||
public void notifyFriend(UserApplyEvent event) {
|
||||
UserApply userApply = event.getUserApply();
|
||||
Integer unReadCount = userApplyDao.getUnReadCount(userApply.getTargetId());
|
||||
webSocketService.sendToFriend(WSAdapter.buildApplySend(new WSFriendApply(userApply.getUid(), unReadCount)), userApply.getTargetId());
|
||||
webSocketService.sendToUid(WSAdapter.buildApplySend(new WSFriendApply(userApply.getUid(), unReadCount)), userApply.getTargetId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@ package com.abin.mallchat.custom.common.event.listener;
|
||||
|
||||
import com.abin.mallchat.common.chat.dao.MessageDao;
|
||||
import com.abin.mallchat.common.common.event.UserBlackEvent;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSRespTypeEnum;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.WSBlack;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.user.domain.enums.WSRespTypeEnum;
|
||||
import com.abin.mallchat.custom.user.domain.vo.response.ws.WSBaseResp;
|
||||
import com.abin.mallchat.custom.user.domain.vo.response.ws.WSBlack;
|
||||
import com.abin.mallchat.custom.user.service.WebSocketService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.abin.mallchat.custom.common.event.listener;
|
||||
import com.abin.mallchat.common.common.event.UserOfflineEvent;
|
||||
import com.abin.mallchat.common.user.dao.UserDao;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.user.service.WebSocketService;
|
||||
import com.abin.mallchat.custom.user.service.adapter.WSAdapter;
|
||||
@@ -45,6 +46,7 @@ public class UserOfflineListener {
|
||||
User update = new User();
|
||||
update.setId(user.getId());
|
||||
update.setLastOptTime(user.getLastOptTime());
|
||||
update.setActiveStatus(ChatActiveStatusEnum.OFFLINE.getStatus());
|
||||
userDao.updateById(update);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.abin.mallchat.custom.common.event.listener;
|
||||
import com.abin.mallchat.common.common.event.UserOnlineEvent;
|
||||
import com.abin.mallchat.common.user.dao.UserDao;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum;
|
||||
import com.abin.mallchat.common.user.service.IpService;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.user.service.WebSocketService;
|
||||
@@ -49,6 +50,7 @@ public class UserOnlineListener {
|
||||
update.setId(user.getId());
|
||||
update.setLastOptTime(user.getLastOptTime());
|
||||
update.setIpInfo(user.getIpInfo());
|
||||
update.setActiveStatus(ChatActiveStatusEnum.ONLINE.getStatus());
|
||||
userDao.updateById(update);
|
||||
//更新用户ip详情
|
||||
ipService.refreshIpDetailAsync(user.getId());
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.abin.mallchat.custom.common.event.listener;
|
||||
|
||||
import com.abin.mallchat.common.common.event.WSPushEvent;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSPushTypeEnum;
|
||||
import com.abin.mallchat.custom.user.service.WebSocketService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.event.TransactionalEventListener;
|
||||
|
||||
/**
|
||||
* 好友申请监听器
|
||||
*
|
||||
* @author zhongzb create on 2022/08/26
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WSPushListener {
|
||||
@Autowired
|
||||
private WebSocketService webSocketService;
|
||||
|
||||
@Async
|
||||
@TransactionalEventListener(classes = WSPushEvent.class, fallbackExecution = true)
|
||||
public void wsPush(WSPushEvent event) {
|
||||
WSPushTypeEnum wsPushTypeEnum = WSPushTypeEnum.of(event.getPushType());
|
||||
switch (wsPushTypeEnum) {
|
||||
case USER:
|
||||
webSocketService.sendToUidList(event.getWsBaseMsg(), event.getUidList());
|
||||
break;
|
||||
case ALL:
|
||||
webSocketService.sendToAllOnline(event.getWsBaseMsg(), null);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,9 +7,7 @@ import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.PageBaseResp;
|
||||
import com.abin.mallchat.common.common.utils.RequestHolder;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendApplyReq;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendApproveReq;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendCheckReq;
|
||||
@@ -21,7 +19,6 @@ import com.abin.mallchat.custom.user.service.FriendService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -40,11 +37,6 @@ import javax.validation.Valid;
|
||||
@Api(tags = "好友相关接口")
|
||||
@Slf4j
|
||||
public class FriendController {
|
||||
@Autowired
|
||||
private ChatService chatService;
|
||||
@Autowired
|
||||
private UserCache userCache;
|
||||
|
||||
@Resource
|
||||
private FriendService friendService;
|
||||
|
||||
@@ -86,13 +78,13 @@ public class FriendController {
|
||||
}
|
||||
|
||||
@PutMapping("/apply")
|
||||
@ApiOperation("申请审批")
|
||||
@ApiOperation("审批申请")
|
||||
public ApiResult<Void> applyApprove(@Valid @RequestBody FriendApproveReq request) {
|
||||
friendService.applyApprove(RequestHolder.get().getUid(), request);
|
||||
return ApiResult.success();
|
||||
}
|
||||
|
||||
@PutMapping("/page")
|
||||
@GetMapping("/page")
|
||||
@ApiOperation("联系人列表")
|
||||
public ApiResult<CursorPageBaseResp<ChatMemberResp>> friendList(@Valid CursorPageBaseReq request) {
|
||||
Long uid = RequestHolder.get().getUid();
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Description: 场景枚举
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-06-20
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum OssSceneEnum {
|
||||
CHAT(1, "聊天", "/chat"),
|
||||
EMOJI(2, "表情包", "/emoji"),
|
||||
;
|
||||
|
||||
private final Integer type;
|
||||
private final String desc;
|
||||
private final String path;
|
||||
|
||||
private static final Map<Integer, OssSceneEnum> cache;
|
||||
|
||||
static {
|
||||
cache = Arrays.stream(OssSceneEnum.values()).collect(Collectors.toMap(OssSceneEnum::getType, Function.identity()));
|
||||
}
|
||||
|
||||
public static OssSceneEnum of(Integer type) {
|
||||
return cache.get(type);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Description: ws前端请求类型枚举
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum WSReqTypeEnum {
|
||||
LOGIN(1, "请求登录二维码"),
|
||||
HEARTBEAT(2, "心跳包"),
|
||||
AUTHORIZE(3, "登录认证"),
|
||||
;
|
||||
|
||||
private final Integer type;
|
||||
private final String desc;
|
||||
|
||||
private static Map<Integer, WSReqTypeEnum> cache;
|
||||
|
||||
static {
|
||||
cache = Arrays.stream(WSReqTypeEnum.values()).collect(Collectors.toMap(WSReqTypeEnum::getType, Function.identity()));
|
||||
}
|
||||
|
||||
public static WSReqTypeEnum of(Integer type) {
|
||||
return cache.get(type);
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.enums;
|
||||
|
||||
import com.abin.mallchat.custom.user.domain.vo.response.ws.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Description: ws前端请求类型枚举
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum WSRespTypeEnum {
|
||||
LOGIN_URL(1, "登录二维码返回", WSLoginUrl.class),
|
||||
LOGIN_SCAN_SUCCESS(2, "用户扫描成功等待授权", null),
|
||||
LOGIN_SUCCESS(3, "用户登录成功返回用户信息", WSLoginSuccess.class),
|
||||
MESSAGE(4, "新消息", WSMessage.class),
|
||||
ONLINE_OFFLINE_NOTIFY(5, "上下线通知", WSOnlineOfflineNotify.class),
|
||||
INVALIDATE_TOKEN(6, "使前端的token失效,意味着前端需要重新登录", null),
|
||||
BLACK(7, "拉黑用户", WSBlack.class),
|
||||
MARK(8, "消息标记", WSMsgMark.class),
|
||||
RECALL(9, "消息撤回", WSMsgRecall.class),
|
||||
APPLY(10, "好友申请", WSFriendApply.class),
|
||||
MEMBER_CHANGE(11, "成员变动", WSMemberChange.class),
|
||||
MESSAGE_READ(12, "消息已读数", WSMessageRead.class),
|
||||
;
|
||||
|
||||
private final Integer type;
|
||||
private final String desc;
|
||||
private final Class dataClass;
|
||||
|
||||
private static Map<Integer, WSRespTypeEnum> cache;
|
||||
|
||||
static {
|
||||
cache = Arrays.stream(WSRespTypeEnum.values()).collect(Collectors.toMap(WSRespTypeEnum::getType, Function.identity()));
|
||||
}
|
||||
|
||||
public static WSRespTypeEnum of(Integer type) {
|
||||
return cache.get(type);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
|
||||
/**
|
||||
@@ -24,7 +25,7 @@ public class FriendApplyReq {
|
||||
@ApiModelProperty("申请信息")
|
||||
private String msg;
|
||||
|
||||
@NotBlank
|
||||
@NotNull
|
||||
@ApiModelProperty("好友uid")
|
||||
private Long targetUid;
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
|
||||
/**
|
||||
@@ -20,7 +20,7 @@ import javax.validation.constraints.NotBlank;
|
||||
@NoArgsConstructor
|
||||
public class FriendDeleteReq {
|
||||
|
||||
@NotBlank
|
||||
@NotNull
|
||||
@ApiModelProperty("好友uid")
|
||||
private Long targetUid;
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.request.ws;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WSAuthorize {
|
||||
private String token;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.request.ws;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Description: websocket前端请求体
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
public class WSBaseReq {
|
||||
/**
|
||||
* 请求类型 1.请求登录二维码,2心跳检测
|
||||
*
|
||||
* @see com.abin.mallchat.custom.user.domain.enums.WSReqTypeEnum
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 每个请求包具体的数据,类型不同结果不同
|
||||
*/
|
||||
private String data;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Description: ws的基本返回信息体
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
public class WSBaseResp<T> {
|
||||
/**
|
||||
* ws推送给前端的消息
|
||||
*
|
||||
* @see com.abin.mallchat.custom.user.domain.enums.WSRespTypeEnum
|
||||
*/
|
||||
private Integer type;
|
||||
private T data;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WSBlack {
|
||||
private Long uid;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WSFriendApply {
|
||||
@ApiModelProperty("申请人")
|
||||
private Long uid;
|
||||
@ApiModelProperty("申请未读数")
|
||||
private Integer unreadCount;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WSLoginSuccess {
|
||||
private Long uid;
|
||||
private String avatar;
|
||||
private String token;
|
||||
private String name;
|
||||
//用户权限 0普通用户 1超管
|
||||
private Integer power;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WSLoginUrl {
|
||||
private String loginUrl;
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WSMemberChange {
|
||||
@ApiModelProperty("群组id")
|
||||
private Long roomId;
|
||||
@ApiModelProperty("变动人")
|
||||
private Long uid;
|
||||
@ApiModelProperty("变动类型 1加入群组 2移除群组")
|
||||
private Integer changeType;
|
||||
/**
|
||||
* @see com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum
|
||||
*/
|
||||
@ApiModelProperty("在线状态 1在线 2离线")
|
||||
private Integer activeStatus;
|
||||
@ApiModelProperty("最后一次上下线时间")
|
||||
private Date lastOptTime;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageResp;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Description: 用户消息推送
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
public class WSMessage extends ChatMessageResp {
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WSMessageRead {
|
||||
@ApiModelProperty("消息")
|
||||
private Long msgId;
|
||||
@ApiModelProperty("阅读人数(可能为0)")
|
||||
private Integer readCount;
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WSMsgMark {
|
||||
private List<WSMsgMarkItem> markList;
|
||||
|
||||
@Data
|
||||
public static class WSMsgMarkItem {
|
||||
@ApiModelProperty("操作者")
|
||||
private Long uid;
|
||||
@ApiModelProperty("消息id")
|
||||
private Long msgId;
|
||||
/**
|
||||
* @see com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum
|
||||
*/
|
||||
@ApiModelProperty("标记类型 1点赞 2举报")
|
||||
private Integer markType;
|
||||
@ApiModelProperty("被标记的数量")
|
||||
private Integer markCount;
|
||||
/**
|
||||
* @see com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum
|
||||
*/
|
||||
@ApiModelProperty("动作类型 1确认 2取消")
|
||||
private Integer actType;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import com.abin.mallchat.common.chat.domain.dto.ChatMsgRecallDTO;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Description:消息撤回的推送类
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
public class WSMsgRecall extends ChatMsgRecallDTO {
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.abin.mallchat.custom.user.domain.vo.response.ws;
|
||||
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberResp;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Description:用户上下线变动的推送类
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-03-19
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class WSOnlineOfflineNotify {
|
||||
private List<ChatMemberResp> changeList = new ArrayList<>();//新的上下线用户
|
||||
private Long onlineNum;//在线人数
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.PageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.PageBaseResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendApplyReq;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendApproveReq;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendCheckReq;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.abin.mallchat.custom.user.service;
|
||||
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.ws.WSAuthorize;
|
||||
import com.abin.mallchat.custom.user.domain.vo.response.ws.WSBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.request.ws.WSAuthorize;
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface WebSocketService {
|
||||
/**
|
||||
* 处理用户登录请求,需要返回一张带code的二维码
|
||||
@@ -66,7 +68,9 @@ public interface WebSocketService {
|
||||
*/
|
||||
void sendToAllOnline(WSBaseResp<?> wsBaseResp);
|
||||
|
||||
void sendToFriend(WSBaseResp<?> wsBaseResp, Long friendUid);
|
||||
void sendToUid(WSBaseResp<?> wsBaseResp, Long uid);
|
||||
|
||||
void sendToUidList(WSBaseResp<?> wsBaseResp, List<Long> uidList);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ public class FriendAdapter {
|
||||
FriendApplyResp friendApplyResp = new FriendApplyResp();
|
||||
friendApplyResp.setUid(userApply.getUid());
|
||||
friendApplyResp.setType(userApply.getType());
|
||||
friendApplyResp.setApplyId(userApply.getId());
|
||||
friendApplyResp.setMsg(userApply.getMsg());
|
||||
friendApplyResp.setStatus(userApply.getStatus());
|
||||
return friendApplyResp;
|
||||
|
||||
@@ -3,14 +3,14 @@ package com.abin.mallchat.custom.user.service.adapter;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.abin.mallchat.common.chat.domain.dto.ChatMessageMarkDTO;
|
||||
import com.abin.mallchat.common.chat.domain.dto.ChatMsgRecallDTO;
|
||||
import com.abin.mallchat.common.chat.domain.vo.response.ChatMessageResp;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.domain.enums.ChatActiveStatusEnum;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberResp;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSRespTypeEnum;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberStatisticResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageResp;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.custom.user.domain.enums.WSRespTypeEnum;
|
||||
import com.abin.mallchat.custom.user.domain.vo.response.ws.*;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -87,7 +87,6 @@ public class WSAdapter {
|
||||
private void assembleNum(WSOnlineOfflineNotify onlineOfflineNotify) {
|
||||
ChatMemberStatisticResp memberStatistic = chatService.getMemberStatistic();
|
||||
onlineOfflineNotify.setOnlineNum(memberStatistic.getOnlineNum());
|
||||
onlineOfflineNotify.setTotalNum(memberStatistic.getTotalNum());
|
||||
}
|
||||
|
||||
private static ChatMemberResp buildOnlineInfo(User user) {
|
||||
|
||||
@@ -17,7 +17,7 @@ import com.abin.mallchat.common.user.dao.UserFriendDao;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.domain.entity.UserApply;
|
||||
import com.abin.mallchat.common.user.domain.entity.UserFriend;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.chat.service.ChatService;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.MemberAdapter;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.MessageAdapter;
|
||||
@@ -36,6 +36,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -43,7 +44,7 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.abin.mallchat.common.user.domain.enums.ApplyStatusEnum.AGREE;
|
||||
import static com.abin.mallchat.common.user.domain.enums.ApplyStatusEnum.WAIT_APPROVAL;
|
||||
|
||||
/**
|
||||
* @author : limeng
|
||||
@@ -148,22 +149,22 @@ public class FriendServiceImpl implements FriendService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@RedissonLock(key = "#uid")
|
||||
public void applyApprove(Long uid, FriendApproveReq request) {
|
||||
UserApply userApply = userApplyDao.getById(request.getApplyId());
|
||||
AssertUtil.isNotEmpty(userApply, "不存在申请记录");
|
||||
AssertUtil.equal(userApply.getTargetId(), uid, "不存在申请记录");
|
||||
AssertUtil.equal(userApply.getStatus(), AGREE.getCode(), "已同意好友申请");
|
||||
AssertUtil.equal(userApply.getStatus(), WAIT_APPROVAL.getCode(), "已同意好友申请");
|
||||
//同意申请
|
||||
userApplyDao.agree(request.getApplyId());
|
||||
//创建双方好友关系
|
||||
createFriend(uid, userApply.getUid());
|
||||
//创建一个聊天房间
|
||||
RoomFriend roomFriend = roomService.createFriendRoom(Arrays.asList(uid, userApply.getUid()));
|
||||
//创建双方的会话
|
||||
contactService.createContact(uid, roomFriend.getRoomId());
|
||||
contactService.createContact(userApply.getUid(), roomFriend.getRoomId());
|
||||
// //创建双方的会话
|
||||
// contactService.createContact(uid, roomFriend.getRoomId());
|
||||
// contactService.createContact(userApply.getUid(), roomFriend.getRoomId());
|
||||
//发送一条同意消息。。我们已经是好友了,开始聊天吧
|
||||
chatService.sendMsg(MessageAdapter.buildAgreeMsg(roomFriend.getRoomId()), uid);
|
||||
}
|
||||
@@ -188,6 +189,9 @@ public class FriendServiceImpl implements FriendService {
|
||||
@Override
|
||||
public CursorPageBaseResp<ChatMemberResp> friendList(Long uid, CursorPageBaseReq request) {
|
||||
CursorPageBaseResp<UserFriend> friendPage = userFriendDao.getFriendPage(uid, request);
|
||||
if (CollectionUtils.isEmpty(friendPage.getList())) {
|
||||
return CursorPageBaseResp.empty();
|
||||
}
|
||||
List<Long> friendUids = friendPage.getList()
|
||||
.stream().map(UserFriend::getFriendUid)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
import com.abin.mallchat.common.common.utils.oss.MinIOTemplate;
|
||||
import com.abin.mallchat.common.common.utils.oss.domain.OssReq;
|
||||
import com.abin.mallchat.common.common.utils.oss.domain.OssResp;
|
||||
import com.abin.mallchat.custom.user.domain.enums.OssSceneEnum;
|
||||
import com.abin.mallchat.common.user.domain.enums.OssSceneEnum;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.oss.UploadUrlReq;
|
||||
import com.abin.mallchat.custom.user.service.OssService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@@ -10,11 +10,11 @@ import com.abin.mallchat.common.common.event.UserOnlineEvent;
|
||||
import com.abin.mallchat.common.user.dao.UserDao;
|
||||
import com.abin.mallchat.common.user.domain.entity.User;
|
||||
import com.abin.mallchat.common.user.domain.enums.RoleEnum;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSBaseResp;
|
||||
import com.abin.mallchat.common.user.domain.vo.request.ws.WSAuthorize;
|
||||
import com.abin.mallchat.common.user.service.IRoleService;
|
||||
import com.abin.mallchat.common.user.service.cache.UserCache;
|
||||
import com.abin.mallchat.custom.user.domain.dto.ws.WSChannelExtraDTO;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.ws.WSAuthorize;
|
||||
import com.abin.mallchat.custom.user.domain.vo.response.ws.WSBaseResp;
|
||||
import com.abin.mallchat.custom.user.service.LoginService;
|
||||
import com.abin.mallchat.custom.user.service.WebSocketService;
|
||||
import com.abin.mallchat.custom.user.service.adapter.WSAdapter;
|
||||
@@ -98,7 +98,7 @@ public class WebSocketServiceImpl implements WebSocketService {
|
||||
*/
|
||||
@SneakyThrows
|
||||
@Override
|
||||
@FrequencyControl(time = 100, count = 5, spEl = "T(com.abin.mallchat.common.common.utils.RequestHolder).get().getIp()")
|
||||
@FrequencyControl(time = 1000, count = 50, spEl = "T(com.abin.mallchat.common.common.utils.RequestHolder).get().getIp()")
|
||||
public void handleLoginReq(Channel channel) {
|
||||
//生成随机不重复的登录码
|
||||
Integer code = generateLoginCode(channel);
|
||||
@@ -245,7 +245,7 @@ public class WebSocketServiceImpl implements WebSocketService {
|
||||
@Override
|
||||
public void sendToAllOnline(WSBaseResp<?> wsBaseResp, Long skipUid) {
|
||||
ONLINE_WS_MAP.forEach((channel, ext) -> {
|
||||
if (ObjectUtil.equal(ext.getUid(), skipUid)) {
|
||||
if (Objects.nonNull(skipUid) && Objects.equals(ext.getUid(), skipUid)) {
|
||||
return;
|
||||
}
|
||||
threadPoolTaskExecutor.execute(() -> sendMsg(channel, wsBaseResp));
|
||||
@@ -258,13 +258,21 @@ public class WebSocketServiceImpl implements WebSocketService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendToFriend(WSBaseResp<?> wsBaseResp, Long friendUid) {
|
||||
CopyOnWriteArrayList<Channel> channels = ONLINE_UID_MAP.get(friendUid);
|
||||
public void sendToUid(WSBaseResp<?> wsBaseResp, Long uid) {
|
||||
CopyOnWriteArrayList<Channel> channels = ONLINE_UID_MAP.get(uid);
|
||||
if (CollectionUtil.isEmpty(channels)) {
|
||||
log.info("用户:{}不在线", friendUid);
|
||||
log.info("用户:{}不在线", uid);
|
||||
return;
|
||||
}
|
||||
threadPoolTaskExecutor.execute(() -> sendMsg(channels.get(0), wsBaseResp));
|
||||
channels.forEach(channel -> {
|
||||
threadPoolTaskExecutor.execute(() -> sendMsg(channel, wsBaseResp));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendToUidList(WSBaseResp<?> wsBaseResp, List<Long> uidList) {
|
||||
uidList.forEach(uid -> sendToUid(wsBaseResp, uid));
|
||||
}
|
||||
|
||||
private void sendMsg(Channel channel, WSBaseResp<?> wsBaseResp) {
|
||||
|
||||
@@ -3,9 +3,9 @@ package com.abin.mallchat.custom.user.websocket;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.abin.mallchat.custom.user.domain.enums.WSReqTypeEnum;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.ws.WSAuthorize;
|
||||
import com.abin.mallchat.custom.user.domain.vo.request.ws.WSBaseReq;
|
||||
import com.abin.mallchat.common.user.domain.enums.WSReqTypeEnum;
|
||||
import com.abin.mallchat.common.user.domain.vo.request.ws.WSAuthorize;
|
||||
import com.abin.mallchat.common.user.domain.vo.request.ws.WSBaseReq;
|
||||
import com.abin.mallchat.custom.user.service.WebSocketService;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.abin.mallchat.custom.common;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.abin.mallchat.custom.user.service.adapter.FriendAdapter.buildFriendRoomKey;
|
||||
|
||||
/**
|
||||
* Description:
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-07-22
|
||||
*/
|
||||
public class CommonTest {
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(buildFriendRoomKey(100L, 102L));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user