Files
gochat/backend/internal/handler/api/v1/push_subscription_handler.go
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

90 lines
3.2 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/response"
)
// PushSubscriptionHandler handles push notification token registration/removal.
// Reference: Chatwoot PushSubscription controller + P2B M8 spec
type PushSubscriptionHandler struct {
pushSubscriptionService *service.PushSubscriptionService
}
// NewPushSubscriptionHandler creates a new PushSubscription handler with injected service.
func NewPushSubscriptionHandler(pushSubscriptionService *service.PushSubscriptionService) *PushSubscriptionHandler {
return &PushSubscriptionHandler{pushSubscriptionService: pushSubscriptionService}
}
// List returns all push tokens for the current user.
// GET /api/v1/push_subscriptions
func (h *PushSubscriptionHandler) List(c *gin.Context) {
if h.pushSubscriptionService == nil {
response.AbortWithStatusError(c, http.StatusServiceUnavailable, response.ErrInternal, "Push subscription service not available")
return
}
userID := getUserID(c)
tokens, err := h.pushSubscriptionService.ListPushTokens(c.Request.Context(), userID)
if err != nil {
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to fetch push subscriptions")
return
}
response.OK(c, gin.H{"push_subscriptions": tokens})
}
// Create registers a new push token for the current user.
// POST /api/v1/push_subscriptions
func (h *PushSubscriptionHandler) Create(c *gin.Context) {
if h.pushSubscriptionService == nil {
response.AbortWithStatusError(c, http.StatusServiceUnavailable, response.ErrInternal, "Push subscription service not available")
return
}
userID := getUserID(c)
var req struct {
Token string `json:"token" binding:"required"`
Platform string `json:"platform"`
DeviceID string `json:"device_id"`
P256DH string `json:"p256dh"` // Web Push: client ECDH public key
Auth string `json:"auth"` // Web Push: client auth secret
}
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid request: token is required")
return
}
token, err := h.pushSubscriptionService.RegisterPushToken(c.Request.Context(), userID, req.Token, req.Platform, req.DeviceID, req.P256DH, req.Auth)
if err != nil {
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to register push subscription")
return
}
response.Created(c, token)
}
// Delete removes a push token by ID.
// DELETE /api/v1/push_subscriptions/:id
func (h *PushSubscriptionHandler) Delete(c *gin.Context) {
if h.pushSubscriptionService == nil {
response.AbortWithStatusError(c, http.StatusServiceUnavailable, response.ErrInternal, "Push subscription service not available")
return
}
id, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "Invalid push subscription ID")
return
}
if err := h.pushSubscriptionService.RemovePushToken(c.Request.Context(), id); err != nil {
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to remove push subscription")
return
}
response.NoContent(c)
}