Files
timeline-server/timeline-user-service/src/main/java/com/timeline/user/controller/WebSocketTestController.java
jianghao 482c32a59c
All checks were successful
test/timeline-server/pipeline/head This commit looks good
feat: 增加通知系统、RabbitMQ集成及Docker一键部署脚本
1. 新增通知中心功能,支持好友请求、评论、点赞等多种通知类型的持久化与推送
2. 集成 RabbitMQ 用于异步处理动态日志,解耦动态服务与日志记录逻辑
3. 提供完整的 Docker Compose 部署方案及一键启动/停止脚本(Shell/Bat)
4. 优化文件服务,增加图片上传时的自动压缩处理以节省存储空间
5. 增强动态服务,支持通过 shareId 公开访问动态项及关键词搜索功能
6. 完善代码健壮性,在关键业务 Service 层增加 @Transactional 事务控制
2026-02-11 14:28:27 +08:00

102 lines
3.6 KiB
Java

package com.timeline.user.controller;
import com.timeline.common.response.ResponseEntity;
import com.timeline.common.utils.UserContextUtils;
import com.timeline.user.ws.WsNotifyService;
import lombok.extern.slf4j.Slf4j;
import com.timeline.user.dto.NotificationPayload;
import com.timeline.user.dto.NotificationType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* WebSocket 测试控制器,用于测试消息推送功能
*/
@Slf4j
@RestController
@RequestMapping("/user/ws-test")
public class WebSocketTestController {
@Autowired
private WsNotifyService wsNotifyService;
/**
* 测试推送通知消息给当前用户
*/
@PostMapping("/notify/self")
public ResponseEntity<String> testNotifySelf(@RequestBody Map<String, Object> message) {
String userId = UserContextUtils.getCurrentUserId();
if (userId == null || userId.isEmpty()) {
return ResponseEntity.error(401, "未获取到用户身份");
}
log.info("测试推送通知给用户: {}", userId);
NotificationPayload payload = new NotificationPayload();
payload.setContent((String) message.getOrDefault("content", "Test Message"));
payload.setCreateTime(java.time.LocalDateTime.now());
String typeStr = (String) message.getOrDefault("type", "SYSTEM_MESSAGE");
try {
payload.setType(NotificationType.valueOf(typeStr));
} catch (IllegalArgumentException e) {
payload.setType(NotificationType.SYSTEM_MESSAGE);
}
wsNotifyService.sendNotificationToUser(userId, payload);
return ResponseEntity.success("通知已推送给用户: " + userId);
}
/**
* 测试推送通知消息给指定用户
*/
@PostMapping("/notify/{targetUserId}")
public ResponseEntity<String> testNotifyUser(
@PathVariable String targetUserId,
@RequestBody Map<String, Object> message) {
log.info("测试推送通知给用户: {}", targetUserId);
NotificationPayload payload = new NotificationPayload();
payload.setContent((String) message.getOrDefault("content", "Test Message"));
payload.setCreateTime(java.time.LocalDateTime.now());
String typeStr = (String) message.getOrDefault("type", "SYSTEM_MESSAGE");
try {
payload.setType(NotificationType.valueOf(typeStr));
} catch (IllegalArgumentException e) {
payload.setType(NotificationType.SYSTEM_MESSAGE);
}
wsNotifyService.sendNotificationToUser(targetUserId, payload);
return ResponseEntity.success("通知已推送");
}
/**
* 测试推送好友通知
*/
@PostMapping("/friend/{targetUserId}")
public ResponseEntity<String> testFriendNotify(
@PathVariable String targetUserId,
@RequestBody Map<String, Object> message) {
log.info("测试推送好友通知给用户: {}", targetUserId);
wsNotifyService.sendFriendNotifyToAllChannels(targetUserId, message);
return ResponseEntity.success("好友通知已推送");
}
/**
* 测试推送聊天消息
*/
@PostMapping("/chat/{targetUserId}")
public ResponseEntity<String> testChatMessage(
@PathVariable String targetUserId,
@RequestBody Map<String, Object> message) {
log.info("测试推送聊天消息给用户: {}", targetUserId);
wsNotifyService.sendChatMessage(targetUserId, message);
return ResponseEntity.success("聊天消息已推送");
}
}