feat(backend): Add user notifications and public video APIs

This commit is contained in:
2025-04-10 02:14:05 +08:00
parent 449f5418b2
commit d1b55d770b
2 changed files with 112 additions and 0 deletions
@@ -10,6 +10,8 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
/**
* 用户通知控制器
*/
@@ -74,6 +76,62 @@ public class UserNotificationController {
return userNotificationService.getUnreadNotificationCount(userId);
}
/**
* 发送用户通知
*/
@PostMapping("/send/{userId}/{type}")
public Result<Void> sendNotification(
@PathVariable("userId") Long userId,
@PathVariable("type") Integer type,
@RequestParam(required = false) String content,
@RequestParam(required = false) Long relatedId,
HttpServletRequest request) {
// 验证调用者身份
Long currentUserId = getUserIdFromRequest(request);
if (currentUserId == null) {
return Result.error("未登录");
}
// 创建通知对象
UserNotification notification = new UserNotification();
notification.setUserId(userId);
notification.setType(type);
// 设置通知内容
String notificationContent;
if (content != null && !content.isEmpty()) {
notificationContent = content;
} else {
// 根据类型生成默认内容
switch (type) {
case 1: // 点赞
notificationContent = "有人赞了你的内容";
break;
case 2: // 评论
notificationContent = "有人评论了你的内容";
break;
case 3: // 关注
notificationContent = "有人关注了你";
break;
case 4: // 系统通知
notificationContent = "系统通知";
break;
default:
notificationContent = "新通知";
}
}
notification.setContent(notificationContent);
notification.setRelatedId(relatedId);
notification.setIsRead(0); // 未读
notification.setCreateTime(LocalDateTime.now());
// 保存通知
userNotificationService.save(notification);
return Result.success();
}
/**
* 从请求中获取用户ID
*/