Files
OwnCord/Server/ws/handlers_ping.go
T
J3vb 6e4a007b91 refactor: migrate WS handlers to V2 Command/Event architecture
Strangler-fig migration of 15 WebSocket handlers from V1 (Hub method,
*Client) to V2 (pure functions: Command, ClientInfo, deps -> Result).
V2 handlers are testable without a running Hub and produce declarative
Result values that the dispatch loop applies.

New abstractions:
- Command interface + typed constructors with input validation
- 7 Event routing interfaces (Channel, ExcludeSender, SequencedDM,
  UserTargeted, BroadcastAll, VoiceChannel, VoiceChannelGuarded)
- Per-domain deps structs (PingDeps, ChatDeps, PresenceDeps,
  ReactionDeps, VoiceDeps) with interface-based DI
- EmitEvents router matching events to delivery mechanisms
- DispatchV2 with panic recovery and runtime.Stack logging

Security hardening:
- Pre-sanitize byte length guard before bluemonday (DoS prevention)
- GetRoleForUser single-JOIN query avoids password hash on hot path
- channel_id positivity enforced in all command constructors
- Log injection prevention: msgType/reqID capped to 64 chars
- Nil KeyHolder dep returns ErrCodeInternal (not silent bypass)
- VoiceChannelGuardedEvent atomic check-and-send under h.mu.RLock

V1-only (complex state/mutex requirements): voice_join, voice_leave.

All tests pass with -race. No CI regressions expected.
2026-04-05 19:03:22 +02:00

23 lines
669 B
Go

package ws
import (
"context"
"fmt"
"time"
)
// handlePingV2 is the V2 handler for ping (heartbeat) messages.
// It rate-limits and returns a pong reply on success.
func handlePingV2(_ context.Context, cmd Command, info ClientInfo, deps any) Result {
d := deps.(PingDeps)
if d.Limiter != nil && !d.Limiter.Allow(fmt.Sprintf("ping:%d", info.UserID), 2, time.Second) {
return Result{} // rate limited: silent drop
}
return Result{Reply: buildJSON(map[string]any{"type": MsgTypePong})}
}
// registerPingHandler registers the ping/pong handler (V2).
func registerPingHandler(r *HandlerRegistry, deps PingDeps) {
r.RegisterV2(MsgTypePing, handlePingV2, deps)
}