Commit 49ed72df authored by 刘基明's avatar 刘基明

删除评论时删除消息通知

parent 4a84cf03
...@@ -28,5 +28,7 @@ public class RedisKeyConstant { ...@@ -28,5 +28,7 @@ public class RedisKeyConstant {
public static final String MESSAGE_NOTIFY_COUNT = "MESSAGE_NOTIFY_COUNT_"; public static final String MESSAGE_NOTIFY_COUNT = "MESSAGE_NOTIFY_COUNT_";
public static final String MESSAGE_NOTIFY_LAST_MSG = "MESSAGE_NOTIFY_LAST_MSG_"; public static final String MESSAGE_NOTIFY_LAST_MSG = "MESSAGE_NOTIFY_LAST_MSG_";
public static final String MESSAGE_NOTIFY_LAST_TIME = "MESSAGE_NOTIFY_LAST_TIME_"; public static final String MESSAGE_NOTIFY_LAST_TIME = "MESSAGE_NOTIFY_LAST_TIME_";
// 消息通知-
public static final String MESSAGE_NOTIFY_QUERY_TIME = "MESSAGE_NOTIFY_QUERY_TIME_";
} }
package com.tanpu.community.api.enums; package com.tanpu.community.api.enums;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
/** /**
* 通用操作类型枚举,包括点赞/取消点赞,收藏/取消收藏,关注/取消关注 * 通用操作类型枚举,包括点赞/取消点赞,收藏/取消收藏,关注/取消关注
*/ */
...@@ -33,4 +37,16 @@ public enum NotificationTypeEnum { ...@@ -33,4 +37,16 @@ public enum NotificationTypeEnum {
this.code = code; this.code = code;
this.type = type; this.type = type;
} }
private static final Map<Integer, String> lookup = new HashMap<Integer, String>();
static {
for (NotificationTypeEnum s : EnumSet.allOf(NotificationTypeEnum.class)) {
lookup.put(s.getCode(), s.getType());
}
}
public static String lookup(Integer code){
return lookup.get(code);
}
} }
...@@ -56,6 +56,11 @@ public class RedisCache { ...@@ -56,6 +56,11 @@ public class RedisCache {
redisHelper.incr(key); redisHelper.incr(key);
} }
public void decr(String key) {
key = cacheName + ":" + key;
redisHelper.decr(key);
}
public void put(String key, Object obj, Integer expireSeconds) { public void put(String key, Object obj, Integer expireSeconds) {
key = cacheName + ":" + key; key = cacheName + ":" + key;
String value = JsonUtil.toJson(obj); String value = JsonUtil.toJson(obj);
......
...@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@RestController @RestController
@Slf4j @Slf4j
...@@ -77,5 +78,11 @@ public class CallBackController { ...@@ -77,5 +78,11 @@ public class CallBackController {
} }
} }
public static void main(String[] args) {
ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
map.put("a",1);
}
} }
...@@ -10,7 +10,6 @@ import com.tanpu.community.api.beans.req.comment.CreateCommentReq; ...@@ -10,7 +10,6 @@ import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
import com.tanpu.community.api.beans.req.comment.LikeCommentReq; import com.tanpu.community.api.beans.req.comment.LikeCommentReq;
import com.tanpu.community.api.beans.req.comment.ReportCommentReq; import com.tanpu.community.api.beans.req.comment.ReportCommentReq;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp; import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.constants.RedisKeyConstant;
import com.tanpu.community.api.enums.NotificationTypeEnum; import com.tanpu.community.api.enums.NotificationTypeEnum;
import com.tanpu.community.api.enums.OperationTypeEnum; import com.tanpu.community.api.enums.OperationTypeEnum;
import com.tanpu.community.cache.RedisCache; import com.tanpu.community.cache.RedisCache;
...@@ -23,7 +22,6 @@ import com.tanpu.community.service.NotificationService; ...@@ -23,7 +22,6 @@ import com.tanpu.community.service.NotificationService;
import com.tanpu.community.service.ReportLogService; import com.tanpu.community.service.ReportLogService;
import com.tanpu.community.service.ThemeService; import com.tanpu.community.service.ThemeService;
import com.tanpu.community.util.ConvertUtil; import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.TimeUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -91,7 +89,7 @@ public class CommentManager { ...@@ -91,7 +89,7 @@ public class CommentManager {
// 消息通知 // 消息通知
ThemeEntity themeEntity = themeService.queryByThemeId(req.getThemeId()); ThemeEntity themeEntity = themeService.queryByThemeId(req.getThemeId());
notificationService.insert(userId,themeEntity.getAuthorId(), NotificationTypeEnum.COMMENT,req.getThemeId(),req.getComment()); notificationService.insert(userId,themeEntity.getAuthorId(), NotificationTypeEnum.COMMENT,commentEntity.getCommentId(),req.getComment());
notificationService.putNotifyCache(themeEntity.getAuthorId(),userId,NotificationTypeEnum.COMMENT); notificationService.putNotifyCache(themeEntity.getAuthorId(),userId,NotificationTypeEnum.COMMENT);
return commentQo; return commentQo;
} }
...@@ -171,6 +169,12 @@ public class CommentManager { ...@@ -171,6 +169,12 @@ public class CommentManager {
//删除评论 //删除评论
public void delete(String commentId, String userId) { public void delete(String commentId, String userId) {
commentService.delete(commentId,userId); commentService.delete(commentId,userId);
notificationService.deleteComment(commentId);
CommentEntity commentEntity = commentService.queryByIdIncludeDelete(commentId);
ThemeEntity themeEntity = themeService.queryByThemeId(commentEntity.getThemeId());
notificationService.deleteNotifyCache(themeEntity.getAuthorId(),userId,commentId,commentEntity.getCreateTime());
} }
} }
...@@ -31,11 +31,13 @@ import com.tanpu.community.service.NotificationService; ...@@ -31,11 +31,13 @@ import com.tanpu.community.service.NotificationService;
import com.tanpu.community.service.ThemeService; import com.tanpu.community.service.ThemeService;
import com.tanpu.community.service.TopicService; import com.tanpu.community.service.TopicService;
import com.tanpu.community.util.ConvertUtil; import com.tanpu.community.util.ConvertUtil;
import com.tanpu.community.util.TimeUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -94,6 +96,11 @@ public class NotificationManager { ...@@ -94,6 +96,11 @@ public class NotificationManager {
themeNotifyQo.setBelongUserOrgName(userInfo.getBelongUserOrgName()); themeNotifyQo.setBelongUserOrgName(userInfo.getBelongUserOrgName());
// 封装引用对象 // 封装引用对象
if (StringUtils.isNotEmpty(themeNotifyQo.getFormerThemeId())) { if (StringUtils.isNotEmpty(themeNotifyQo.getFormerThemeId())) {
if (NotificationTypeEnum.COMMENT.getCode().equals(themeNotifyQo.getMessageType())){
// 评论类型将commentId替换为themeId
CommentEntity commentEntity = commentService.queryByCommentId(themeNotifyQo.getFormerThemeId());
themeNotifyQo.setFormerThemeId(commentEntity.getThemeId());
}
ThemeEntity former = themeService.queryByThemeId(themeNotifyQo.getFormerThemeId()); ThemeEntity former = themeService.queryByThemeId(themeNotifyQo.getFormerThemeId());
if (former != null) { if (former != null) {
themeNotifyQo.setFormerUserName(currentUser.getNickName()); themeNotifyQo.setFormerUserName(currentUser.getNickName());
...@@ -131,6 +138,9 @@ public class NotificationManager { ...@@ -131,6 +138,9 @@ public class NotificationManager {
} }
redisCache.evict(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + userId); redisCache.evict(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + userId);
// 更新查询时间,用于删除评论
redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_QUERY_TIME + userId,
TimeUtils.format(LocalDateTime.now()),60 * 60 * 24 * 30);
return themeNotifyQos; return themeNotifyQos;
} }
...@@ -141,7 +151,6 @@ public class NotificationManager { ...@@ -141,7 +151,6 @@ public class NotificationManager {
String count = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + userId); String count = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + userId);
String lastMsg = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + userId); String lastMsg = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + userId);
String lastTime = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + userId); String lastTime = redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + userId);
return NotificationQo.builder().message(lastMsg).updateCount(count == null ? 0 : Integer.parseInt(count)).updateTime(lastTime).build(); return NotificationQo.builder().message(lastMsg).updateCount(count == null ? 0 : Integer.parseInt(count)).updateTime(lastTime).build();
} }
...@@ -183,7 +192,7 @@ public class NotificationManager { ...@@ -183,7 +192,7 @@ public class NotificationManager {
} }
String s = commentEntity.getContent(); String s = commentEntity.getContent();
notificationService.insert(commentEntity.getAuthorId(), themeEntity.getAuthorId(), NotificationTypeEnum.COMMENT notificationService.insert(commentEntity.getAuthorId(), themeEntity.getAuthorId(), NotificationTypeEnum.COMMENT
, commentEntity.getThemeId(), s, commentEntity.getUpdateTime()); , commentEntity.getCommentId(), s, commentEntity.getUpdateTime());
notificationService.putNotifyCache(themeEntity.getAuthorId(), commentEntity.getAuthorId(), NotificationTypeEnum.COMMENT, commentEntity.getUpdateTime()); notificationService.putNotifyCache(themeEntity.getAuthorId(), commentEntity.getAuthorId(), NotificationTypeEnum.COMMENT, commentEntity.getUpdateTime());
} }
......
...@@ -160,4 +160,7 @@ public class CommentService { ...@@ -160,4 +160,7 @@ public class CommentService {
.orderByAsc(CommentEntity::getCreateTime)); .orderByAsc(CommentEntity::getCreateTime));
} }
public CommentEntity queryByCommentId(String commentId) {
return commentMapper.selectOne(new LambdaQueryWrapper<CommentEntity>().eq(CommentEntity::getCommentId,commentId));
}
} }
...@@ -203,6 +203,49 @@ public class NotificationService { ...@@ -203,6 +203,49 @@ public class NotificationService {
return userInfoNewCommonResp.getData(); return userInfoNewCommonResp.getData();
} }
public void deleteNotifyCache(String notifyUserId, String operatorId, String commentId, LocalDateTime commentTime) {
// 物理删除
NotificationEntity commentNotify = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>().eq(NotificationEntity::getTargetId, commentId));
NotificationEntity last = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>().eq(NotificationEntity::getNotifiedUserId, notifyUserId)
.orderByDesc(NotificationEntity::getUpdateTime)
.last("limit 1"));
notificationMapper.deleteById(commentNotify.getId());
// 如果删除的评论是最後一條通知,则需要从库中再找一条最新的
if (last.getNotificationId().equals(commentNotify.getNotificationId())) {
last = notificationMapper.selectOne(new LambdaQueryWrapper<NotificationEntity>().eq(NotificationEntity::getNotifiedUserId, notifyUserId)
.orderByDesc(NotificationEntity::getUpdateTime)
.last("limit 1"));
if (last == null) {
redisCache.evict(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId);
redisCache.evict(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId);
} else {
UserInfoResp userInfo = getUserInfo(last.getOperatorId());
redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_TIME + notifyUserId, TimeUtils.format(last.getUpdateTime()), 60 * 60 * 24 * 30);
if (NotificationTypeEnum.FOLLOW.getCode().equals(last.getMessageType())) {
redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId, userInfo.getNickName() + "关注了你", 60 * 60 * 24 * 30);
} else {
redisCache.set(RedisKeyConstant.MESSAGE_NOTIFY_LAST_MSG + notifyUserId
, userInfo.getNickName() + NotificationTypeEnum.lookup(last.getMessageType()) + "了你的内容"
, 60 * 60 * 24 * 30);
}
}
}
LocalDateTime lastQueryTime = TimeUtils.reFormat(redisCache.get(RedisKeyConstant.MESSAGE_NOTIFY_QUERY_TIME + notifyUserId));
// 如果删除的评论时时间在红点提示时间内,则需要加工缓存
if (TimeUtils.lessThan(lastQueryTime, commentTime)) {
UserInfoResp userInfo = getUserInfo(operatorId);
// 红点数-1
redisCache.decr(RedisKeyConstant.MESSAGE_NOTIFY_COUNT + notifyUserId);
}
}
// init用 // init用
public void putNotifyCache(String notifyUserId, String operatorId, NotificationTypeEnum type, LocalDateTime updateTime) { public void putNotifyCache(String notifyUserId, String operatorId, NotificationTypeEnum type, LocalDateTime updateTime) {
...@@ -226,4 +269,10 @@ public class NotificationService { ...@@ -226,4 +269,10 @@ public class NotificationService {
} }
} }
public void deleteComment(String commentId) {
}
} }
...@@ -99,7 +99,10 @@ public class TimeUtils { ...@@ -99,7 +99,10 @@ public class TimeUtils {
} }
public static boolean lessThan(String cacheTime, LocalDateTime updateTime) { public static boolean lessThan(String cacheTime, LocalDateTime updateTime) {
if (calMillisTillNow(reFormat(cacheTime))<calMillisTillNow(updateTime)) return true; return calMillisTillNow(reFormat(cacheTime)) < calMillisTillNow(updateTime);
return false; }
public static boolean lessThan(LocalDateTime before, LocalDateTime after) {
return calMillisTillNow(before) < calMillisTillNow(after);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment