Files
gochat/backend/internal/service/push_subscription_service.go
T
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

53 lines
1.8 KiB
Go

package service
import (
"context"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
)
// PushSubscriptionService provides business logic for managing push notification tokens.
// Reference: Chatwoot PushSubscription controller + P2B M8 spec
type PushSubscriptionService struct {
pushTokenRepo *repository.PushTokenRepo
}
// NewPushSubscriptionService creates a new PushSubscription service with required dependencies.
func NewPushSubscriptionService(pushTokenRepo *repository.PushTokenRepo) *PushSubscriptionService {
return &PushSubscriptionService{
pushTokenRepo: pushTokenRepo,
}
}
// ListPushTokens retrieves all push tokens for a user.
func (s *PushSubscriptionService) ListPushTokens(ctx context.Context, userID uint) ([]model.PushToken, error) {
return s.pushTokenRepo.ListByUser(ctx, userID)
}
// RegisterPushToken creates a new push token for a user.
func (s *PushSubscriptionService) RegisterPushToken(ctx context.Context, userID uint, token string, platform string, deviceID string, p256dhKey string, authKey string) (*model.PushToken, error) {
pt := &model.PushToken{
UserID: userID,
Token: token,
Platform: platform,
DeviceID: deviceID,
P256DHKey: p256dhKey,
AuthKey: authKey,
}
err := s.pushTokenRepo.Create(ctx, pt)
if err != nil {
return nil, err
}
return pt, nil
}
// RemovePushToken deletes a push token by ID.
func (s *PushSubscriptionService) RemovePushToken(ctx context.Context, id uint) error {
return s.pushTokenRepo.Delete(ctx, id)
}
// RemovePushTokenByValue deletes a push token by token string and user ID.
func (s *PushSubscriptionService) RemovePushTokenByValue(ctx context.Context, token string, userID uint) error {
return s.pushTokenRepo.DeleteByTokenAndUser(ctx, token, userID)
}