- Remove BOM from all Go source files - Standardize import ordering and whitespace across handlers and services - Replace PostgreSQL full-text search with ILIKE pattern matching in post and user repositories - Enhance JPush client with TLS transport, rate limit monitoring, and simplified API - Refactor PushService to include userID in device management methods - Add MaxDevicesPerUser limit and extract helper functions for push payload construction
159 lines
3.7 KiB
Go
159 lines
3.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"with_you/internal/dto"
|
|
"with_you/internal/model"
|
|
"with_you/internal/pkg/response"
|
|
"with_you/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// PushHandler 推送处理器
|
|
type PushHandler struct {
|
|
pushService service.PushService
|
|
}
|
|
|
|
// NewPushHandler 创建推送处理器
|
|
func NewPushHandler(pushService service.PushService) *PushHandler {
|
|
return &PushHandler{
|
|
pushService: pushService,
|
|
}
|
|
}
|
|
|
|
// RegisterDevice 注册设备
|
|
// POST /api/v1/push/devices
|
|
func (h *PushHandler) RegisterDevice(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
var req dto.RegisterDeviceRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
|
|
// 验证设备类型
|
|
deviceType := model.DeviceType(req.DeviceType)
|
|
if !isValidDeviceType(deviceType) {
|
|
response.BadRequest(c, "invalid device type")
|
|
return
|
|
}
|
|
|
|
err := h.pushService.RegisterDevice(c.Request.Context(), userID, req.DeviceID, deviceType, req.PushToken)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to register device")
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "device registered successfully", nil)
|
|
}
|
|
|
|
// UnregisterDevice 注销设备
|
|
// DELETE /api/v1/push/devices/:device_id
|
|
func (h *PushHandler) UnregisterDevice(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
deviceID := c.Param("device_id")
|
|
if deviceID == "" {
|
|
response.BadRequest(c, "device_id is required")
|
|
return
|
|
}
|
|
|
|
err := h.pushService.UnregisterDevice(c.Request.Context(), userID, deviceID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to unregister device")
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "device unregistered successfully", nil)
|
|
}
|
|
|
|
// GetMyDevices 获取当前用户的设备列表
|
|
// GET /api/v1/push/devices
|
|
func (h *PushHandler) GetMyDevices(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
devices, err := h.pushService.GetUserDevices(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to get devices")
|
|
return
|
|
}
|
|
|
|
response.Success(c, dto.DeviceTokensToResponse(devices))
|
|
}
|
|
|
|
// GetPushRecords 获取推送记录
|
|
// GET /api/v1/push/records
|
|
func (h *PushHandler) GetPushRecords(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
records, err := h.pushService.GetPendingPushes(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to get push records")
|
|
return
|
|
}
|
|
|
|
response.Success(c, &dto.PushRecordListResponse{
|
|
Records: dto.PushRecordsToResponse(records),
|
|
Total: int64(len(records)),
|
|
})
|
|
}
|
|
|
|
// 辅助函数:验证设备类型
|
|
func isValidDeviceType(deviceType model.DeviceType) bool {
|
|
switch deviceType {
|
|
case model.DeviceTypeIOS, model.DeviceTypeAndroid, model.DeviceTypeWeb:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// UpdateDeviceToken 更新设备推送Token
|
|
// PUT /api/v1/push/devices/:device_id/token
|
|
func (h *PushHandler) UpdateDeviceToken(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "")
|
|
return
|
|
}
|
|
|
|
deviceID := c.Param("device_id")
|
|
if deviceID == "" {
|
|
response.BadRequest(c, "device_id is required")
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
PushToken string `json:"push_token"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
|
|
err := h.pushService.UpdateDeviceToken(c.Request.Context(), userID, deviceID, req.PushToken)
|
|
if err != nil {
|
|
response.InternalServerError(c, "failed to update device token")
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "device token updated successfully", nil)
|
|
}
|