feat: 消息聊天最近列表支持删除会话
This commit is contained in:
@@ -276,4 +276,12 @@ public class MessageController {
|
||||
messageService.markAsRead(id);
|
||||
return ResponseEntity.ok(Map.of("message", "已标记已读"));
|
||||
}
|
||||
|
||||
@DeleteMapping("/conversation/{withUserId}")
|
||||
public ResponseEntity<?> deleteConversation(
|
||||
@AuthenticationPrincipal UserPrincipal principal,
|
||||
@PathVariable Long withUserId) {
|
||||
messageService.deleteConversation(principal.getUserId(), withUserId);
|
||||
return ResponseEntity.ok(Map.of("message", "已删除"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.filesystem.entity.Message;
|
||||
import com.filesystem.mapper.MessageMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
@@ -14,8 +16,16 @@ public class MessageService {
|
||||
|
||||
@Resource
|
||||
private MessageMapper messageMapper;
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
private static final String CONV_DELETED_KEY = "msg:del:";
|
||||
|
||||
public List<Message> getMessages(Long userId1, Long userId2) {
|
||||
String convKey = buildConvKey(userId1, userId2);
|
||||
String deletedKey = CONV_DELETED_KEY + userId1 + ":" + convKey;
|
||||
|
||||
return messageMapper.selectList(
|
||||
new LambdaQueryWrapper<Message>()
|
||||
.and(wrapper -> wrapper
|
||||
@@ -26,6 +36,31 @@ public class MessageService {
|
||||
.orderByAsc(Message::getCreateTime)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除与某个用户的聊天记录(双向,本人会看不到)
|
||||
*/
|
||||
public void deleteConversation(Long userId, Long withUserId) {
|
||||
String convKey = buildConvKey(userId, withUserId);
|
||||
String deletedKey = CONV_DELETED_KEY + userId + ":" + convKey;
|
||||
// 保留7天
|
||||
redisTemplate.opsForValue().set(deletedKey, "1", java.time.Duration.ofDays(7));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查与某人的对话是否已被当前用户删除
|
||||
*/
|
||||
public boolean isConversationDeleted(Long userId, Long withUserId) {
|
||||
String convKey = buildConvKey(userId, withUserId);
|
||||
String deletedKey = CONV_DELETED_KEY + userId + ":" + convKey;
|
||||
return Boolean.TRUE.equals(redisTemplate.hasKey(deletedKey));
|
||||
}
|
||||
|
||||
private String buildConvKey(Long userId1, Long userId2) {
|
||||
long min = Math.min(userId1, userId2);
|
||||
long max = Math.max(userId1, userId2);
|
||||
return min + "_" + max;
|
||||
}
|
||||
|
||||
public Message sendMessage(Long fromUserId, Long toUserId, String content, String type) {
|
||||
Message message = new Message();
|
||||
|
||||
Reference in New Issue
Block a user