1.消息撤回功能
2.管理员拉黑功能
3.管理员撤回消息功能
4.消息类型可扩展
This commit is contained in:
zhongzb
2023-06-05 13:02:09 +08:00
parent cf5eb9f455
commit 44244c0ba2
60 changed files with 1182 additions and 74 deletions

View File

@@ -49,10 +49,10 @@ public class MessageDao extends ServiceImpl<MessageMapper, Message> {
.count();
}
public void updateGapCount(Long id, Integer gapCount) {
public void invalidByUid(Long uid) {
lambdaUpdate()
.eq(Message::getId, id)
.set(Message::getGapCount, gapCount)
.eq(Message::getFromUid, uid)
.set(Message::getStatus, MessageStatusEnum.DELETE.getStatus())
.update();
}
}

View File

@@ -0,0 +1,22 @@
package com.abin.mallchat.common.chat.domain.dto;
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 ChatMsgRecallDTO {
private Long msgId;
private Long roomId;
//撤回的用户
private Long recallUid;
}

View File

@@ -1,5 +1,6 @@
package com.abin.mallchat.common.chat.domain.entity;
import com.abin.mallchat.common.chat.domain.entity.msg.MessageExtra;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
@@ -73,7 +74,7 @@ public class Message implements Serializable {
private Integer gapCount;
/**
* 消息类型 1正常文本 2.爆赞 点赞超过103.危险发言举报超5
* 消息类型 1正常文本 2.撤回消息
*
* @see com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum
*/
@@ -81,7 +82,7 @@ public class Message implements Serializable {
private Integer type;
/**
* 最后上下线时间
* 消息扩展字段
*/
@TableField(value = "extra", typeHandler = JacksonTypeHandler.class)
private MessageExtra extra;

View File

@@ -1,5 +1,6 @@
package com.abin.mallchat.common.chat.domain.entity;
package com.abin.mallchat.common.chat.domain.entity.msg;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@@ -17,8 +18,11 @@ import java.util.Map;
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class MessageExtra implements Serializable {
private static final long serialVersionUID = 1L;
//注册时的ip
//url跳转链接
private Map<String, String> urlTitleMap;
//消息撤回详情
private MsgRecall recall;
}

View File

@@ -0,0 +1,26 @@
package com.abin.mallchat.common.chat.domain.entity.msg;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* Description: 消息撤回
* Author: <a href="https://github.com/zongzibinbin">abin</a>
* Date: 2023-06-04
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MsgRecall implements Serializable {
private static final long serialVersionUID = 1L;
//撤回消息的uid
private Long recallUid;
//撤回的时间点
private Date recallTime;
}

View File

@@ -16,14 +16,13 @@ import java.util.stream.Collectors;
@AllArgsConstructor
@Getter
public enum MessageMarkTypeEnum {
LIKE(1, "点赞", 10, MessageTypeEnum.LIKE),
DISLIKE(2, "点踩", 5, MessageTypeEnum.DISLIKE),
LIKE(1, "点赞", 10),
DISLIKE(2, "点踩", 5),
;
private final Integer type;
private final String desc;
private final Integer riseNum;//需要多少个标记升级
private final MessageTypeEnum riseEnum;//消息升级成什么类型的消息
private static Map<Integer, MessageMarkTypeEnum> cache;

View File

@@ -16,9 +16,8 @@ import java.util.stream.Collectors;
@AllArgsConstructor
@Getter
public enum MessageTypeEnum {
NORMAL(1, "正常"),
LIKE(2, "爆赞"),
DISLIKE(3, "危险发言"),
TEXT(1, "正常"),
RECALL(2, "撤回消息"),
;
private final Integer type;

View File

@@ -0,0 +1,42 @@
package com.abin.mallchat.common.chat.service.cache;
import com.abin.mallchat.common.chat.dao.MessageDao;
import com.abin.mallchat.common.chat.domain.entity.Message;
import com.abin.mallchat.common.common.utils.CursorUtils;
import com.abin.mallchat.common.user.dao.BlackDao;
import com.abin.mallchat.common.user.dao.RoleDao;
import com.abin.mallchat.common.user.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
/**
* Description: 消息相关缓存
* Author: <a href="https://github.com/zongzibinbin">abin</a>
* Date: 2023-03-27
*/
@Component
public class MsgCache {
@Autowired
private CursorUtils cursorUtils;
@Autowired
private UserDao userDao;
@Autowired
private BlackDao blackDao;
@Autowired
private RoleDao roleDao;
@Autowired
private MessageDao messageDao;
@Cacheable(cacheNames = "msg", key = "'msg'+#msgId")
public Message getMsg(Long msgId) {
return messageDao.getById(msgId);
}
@CacheEvict(cacheNames = "msg", key = "'msg'+#msgId")
public Message evictMsg(Long msgId) {
return null;
}
}

View File

@@ -22,7 +22,7 @@ import javax.validation.constraints.Max;
public class CursorPageBaseReq {
@ApiModelProperty("页面大小")
@Max(50)
@Max(100)
private Integer pageSize = 10;
@ApiModelProperty("游标初始为null后续请求附带上次翻页的游标")

View File

@@ -0,0 +1,15 @@
package com.abin.mallchat.common.common.event;
import com.abin.mallchat.common.chat.domain.dto.ChatMsgRecallDTO;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
@Getter
public class MessageRecallEvent extends ApplicationEvent {
private ChatMsgRecallDTO recallDTO;
public MessageRecallEvent(Object source, ChatMsgRecallDTO recallDTO) {
super(source);
this.recallDTO = recallDTO;
}
}

View File

@@ -0,0 +1,15 @@
package com.abin.mallchat.common.common.event;
import com.abin.mallchat.common.user.domain.entity.User;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
@Getter
public class UserBlackEvent extends ApplicationEvent {
private final User user;
public UserBlackEvent(Object source, User user) {
super(source);
this.user = user;
}
}

View File

@@ -6,7 +6,7 @@ import org.springframework.context.ApplicationEvent;
@Getter
public class UserOfflineEvent extends ApplicationEvent {
private User user;
private final User user;
public UserOfflineEvent(Object source, User user) {
super(source);

View File

@@ -6,7 +6,7 @@ import org.springframework.context.ApplicationEvent;
@Getter
public class UserOnlineEvent extends ApplicationEvent {
private User user;
private final User user;
public UserOnlineEvent(Object source, User user) {
super(source);

View File

@@ -6,7 +6,7 @@ import org.springframework.context.ApplicationEvent;
@Getter
public class UserRegisterEvent extends ApplicationEvent {
private User user;
private final User user;
public UserRegisterEvent(Object source, User user) {
super(source);

View File

@@ -67,6 +67,12 @@ public class AssertUtil {
}
}
public static void notEqual(Object o1, Object o2, String msg) {
if (ObjectUtil.equal(o1, o2)) {
throwException(msg);
}
}
private static boolean isEmpty(Object obj) {
return ObjectUtil.isEmpty(obj);
}

View File

@@ -0,0 +1,19 @@
package com.abin.mallchat.common.user.dao;
import com.abin.mallchat.common.user.domain.entity.Role;
import com.abin.mallchat.common.user.mapper.RoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 角色表 服务实现类
* </p>
*
* @author <a href="https://github.com/zongzibinbin">abin</a>
* @since 2023-06-04
*/
@Service
public class RoleDao extends ServiceImpl<RoleMapper, Role> {
}

View File

@@ -0,0 +1,26 @@
package com.abin.mallchat.common.user.dao;
import com.abin.mallchat.common.user.domain.entity.UserRole;
import com.abin.mallchat.common.user.mapper.UserRoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
/**
* <p>
* 用户角色关系表 服务实现类
* </p>
*
* @author <a href="https://github.com/zongzibinbin">abin</a>
* @since 2023-06-04
*/
@Service
public class UserRoleDao extends ServiceImpl<UserRoleMapper, UserRole> {
public List<UserRole> listByUid(Long uid) {
return lambdaQuery()
.eq(UserRole::getUid, Objects.requireNonNull(uid))
.list();
}
}

View File

@@ -0,0 +1,53 @@
package com.abin.mallchat.common.user.domain.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
* 角色表
* </p>
*
* @author <a href="https://github.com/zongzibinbin">abin</a>
* @since 2023-06-04
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("role")
public class Role implements Serializable {
private static final long serialVersionUID = 1L;
/**
* id
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 角色名称
*/
@TableField("name")
private String name;
/**
* 创建时间
*/
@TableField("create_time")
private LocalDateTime createTime;
/**
* 修改时间
*/
@TableField("update_time")
private LocalDateTime updateTime;
}

View File

@@ -0,0 +1,59 @@
package com.abin.mallchat.common.user.domain.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
* 用户角色关系表
* </p>
*
* @author <a href="https://github.com/zongzibinbin">abin</a>
* @since 2023-06-04
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("user_role")
public class UserRole implements Serializable {
private static final long serialVersionUID = 1L;
/**
* id
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* uid
*/
@TableField("uid")
private Long uid;
/**
* 角色id
*/
@TableField("role_id")
private Long roleId;
/**
* 创建时间
*/
@TableField("create_time")
private LocalDateTime createTime;
/**
* 修改时间
*/
@TableField("update_time")
private LocalDateTime updateTime;
}

View File

@@ -0,0 +1,35 @@
package com.abin.mallchat.common.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-03-19
*/
@AllArgsConstructor
@Getter
public enum RoleEnum {
ADMIN(1L, "超级管理员"),
CHAT_MANAGER(2L, "抹茶群聊管理"),
;
private final Long id;
private final String desc;
private static Map<Long, RoleEnum> cache;
static {
cache = Arrays.stream(RoleEnum.values()).collect(Collectors.toMap(RoleEnum::getId, Function.identity()));
}
public static RoleEnum of(Integer type) {
return cache.get(type);
}
}

View File

@@ -0,0 +1,16 @@
package com.abin.mallchat.common.user.mapper;
import com.abin.mallchat.common.user.domain.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* 角色表 Mapper 接口
* </p>
*
* @author <a href="https://github.com/zongzibinbin">abin</a>
* @since 2023-06-04
*/
public interface RoleMapper extends BaseMapper<Role> {
}

View File

@@ -0,0 +1,16 @@
package com.abin.mallchat.common.user.mapper;
import com.abin.mallchat.common.user.domain.entity.UserRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* 用户角色关系表 Mapper 接口
* </p>
*
* @author <a href="https://github.com/zongzibinbin">abin</a>
* @since 2023-06-04
*/
public interface UserRoleMapper extends BaseMapper<UserRole> {
}

View File

@@ -0,0 +1,22 @@
package com.abin.mallchat.common.user.service;
import com.abin.mallchat.common.user.domain.enums.RoleEnum;
/**
* <p>
* 角色表 服务类
* </p>
*
* @author <a href="https://github.com/zongzibinbin">abin</a>
* @since 2023-06-04
*/
public interface IRoleService {
/**
* 是否有某个权限,临时做法
*
* @return
*/
boolean hasPower(Long uid, RoleEnum roleEnum);
}

View File

@@ -0,0 +1,16 @@
package com.abin.mallchat.common.user.service;
import com.abin.mallchat.common.user.domain.entity.UserRole;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 用户角色关系表 服务类
* </p>
*
* @author <a href="https://github.com/zongzibinbin">abin</a>
* @since 2023-06-04
*/
public interface IUserRoleService extends IService<UserRole> {
}

View File

@@ -8,9 +8,12 @@ import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
import com.abin.mallchat.common.common.utils.CursorUtils;
import com.abin.mallchat.common.common.utils.RedisUtils;
import com.abin.mallchat.common.user.dao.BlackDao;
import com.abin.mallchat.common.user.dao.RoleDao;
import com.abin.mallchat.common.user.dao.UserDao;
import com.abin.mallchat.common.user.dao.UserRoleDao;
import com.abin.mallchat.common.user.domain.entity.Black;
import com.abin.mallchat.common.user.domain.entity.User;
import com.abin.mallchat.common.user.domain.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
@@ -34,6 +37,10 @@ public class UserCache {
private UserDao userDao;
@Autowired
private BlackDao blackDao;
@Autowired
private RoleDao roleDao;
@Autowired
private UserRoleDao userRoleDao;
public Long getOnlineNum() {
String onlineKey = RedisKey.getKey(RedisKey.ONLINE_UID_ZET);
@@ -45,6 +52,16 @@ public class UserCache {
return RedisUtils.zCard(offlineKey);
}
//移除用户
public void remove(Long uid) {
String onlineKey = RedisKey.getKey(RedisKey.ONLINE_UID_ZET);
String offlineKey = RedisKey.getKey(RedisKey.OFFLINE_UID_ZET);
//移除离线表
RedisUtils.zRemove(offlineKey, uid);
//移除上线表
RedisUtils.zRemove(onlineKey, uid);
}
//用户上线
public void online(Long uid, Date optTime) {
String onlineKey = RedisKey.getKey(RedisKey.ONLINE_UID_ZET);
@@ -80,9 +97,6 @@ public class UserCache {
/**
* 获取用户信息,盘路缓存模式
*
* @param uid
* @return
*/
public User getUserInfo(Long uid) {//todo 后期做二级缓存
return getUserInfoBatch(Collections.singleton(uid)).get(uid);
@@ -90,9 +104,6 @@ public class UserCache {
/**
* 获取用户信息,盘路缓存模式
*
* @param uids
* @return
*/
public Map<Long, User> getUserInfoBatch(Set<Long> uids) {
List<String> keys = uids.stream().map(a -> RedisKey.getKey(RedisKey.USER_INFO_STRING, a)).collect(Collectors.toList());
@@ -128,4 +139,12 @@ public class UserCache {
public Map<Integer, Set<String>> evictBlackMap() {
return null;
}
@Cacheable(cacheNames = "user", key = "'roles'+#uid")
public Set<Long> getRoleSet(Long uid) {
List<UserRole> userRoles = userRoleDao.listByUid(uid);
return userRoles.stream()
.map(UserRole::getRoleId)
.collect(Collectors.toSet());
}
}

View File

@@ -0,0 +1,31 @@
package com.abin.mallchat.common.user.service.impl;
import com.abin.mallchat.common.user.domain.enums.RoleEnum;
import com.abin.mallchat.common.user.service.IRoleService;
import com.abin.mallchat.common.user.service.cache.UserCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
import java.util.Set;
/**
* Description: 角色管理
* Author: <a href="https://github.com/zongzibinbin">abin</a>
* Date: 2023-06-04
*/
@Service
public class RoleServiceImpl implements IRoleService {
@Autowired
private UserCache userCache;
@Override
public boolean hasPower(Long uid, RoleEnum roleEnum) {//超级管理员无敌的好吧,后期做成权限=》资源模式
Set<Long> roleSet = userCache.getRoleSet(uid);
return isAdmin(roleSet) || roleSet.contains(roleEnum.getId());
}
private boolean isAdmin(Set<Long> roleSet) {
return Objects.requireNonNull(roleSet).contains(RoleEnum.ADMIN.getId());
}
}

View File

@@ -4,7 +4,6 @@ import com.abin.mallchat.common.common.annotation.RedissonLock;
import com.abin.mallchat.common.common.domain.enums.IdempotentEnum;
import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum;
import com.abin.mallchat.common.common.event.ItemReceiveEvent;
import com.abin.mallchat.common.user.dao.ItemConfigDao;
import com.abin.mallchat.common.user.dao.UserBackpackDao;
import com.abin.mallchat.common.user.domain.entity.ItemConfig;
import com.abin.mallchat.common.user.domain.entity.UserBackpack;
@@ -13,6 +12,7 @@ import com.abin.mallchat.common.user.service.IUserBackpackService;
import com.abin.mallchat.common.user.service.cache.ItemCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.Objects;
@@ -30,12 +30,11 @@ public class UserBackpackServiceImpl implements IUserBackpackService {
@Autowired
private UserBackpackDao userBackpackDao;
@Autowired
private ItemConfigDao itemConfigDao;
@Autowired
private ItemCache itemCache;
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Autowired
@Lazy
private UserBackpackServiceImpl userBackpackService;
@Override

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.abin.mallchat.common.user.mapper.RoleMapper">
</mapper>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.abin.mallchat.common.user.mapper.UserRoleMapper">
</mapper>