Files
scrabble-game/backend/internal/pushgrpc/server.go
T
Ilia Denisov 57c778f9b2
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 13s
CI / ui (pull_request) Successful in 54s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 57s
feat(telegram,game): single bot + per-user variant preferences
Collapse the two per-language Telegram bots into one unified bot and
replace language-based variant gating with explicit per-user variant
preferences.

- Telegram: one bot; drop service_language and the supported_languages
  set everywhere (DB, account, auth, FlatBuffers Session wire, gateway,
  connector proto). The single bot renders chat and out-of-app push in
  the recipient's preferred_language; remove the game-language push
  routing override (notify Intent.Language / push Event.language).
- Preferences: new accounts.variant_preferences (text[], DB default
  {erudit_ru}, CHECK non-empty + subset of the three variants). Gates
  the New Game picker, vs-AI and the friend invitation the player
  creates, enforced server-side (HTTP 400 otherwise); an invited friend
  may still accept any variant. Edited on the Settings screen; variants
  are Erudit-first everywhere.
- Admin: drop the per-bot language selectors (broadcast / send-to-user)
  and the feedback channel_lang column/field.
- Env/CI: collapse TELEGRAM_BOT_TOKEN_{EN,RU}, TELEGRAM_GAME_CHANNEL_ID_{EN,RU},
  VITE_TELEGRAM_LINK{,_EN,_RU} and VITE_TELEGRAM_GAME_CHANNEL_NAME_{EN,RU}
  to single unsuffixed names; drop GATEWAY_DEFAULT_SUPPORTED_LANGUAGES.
- Docs updated (ARCHITECTURE, FUNCTIONAL + _ru, platform/telegram, gateway,
  backend, ui, UI_DESIGN, PRERELEASE).

The migration squash is deferred to a follow-up PR.
2026-06-20 14:23:25 +02:00

109 lines
3.1 KiB
Go

// Package pushgrpc serves the backend -> gateway live-event stream: a gRPC
// server exposing the scrabble.push.v1 Push service (docs/ARCHITECTURE.md §2).
// It bridges the in-process notify.Hub to the wire — each Subscribe stream
// drains a hub subscription and forwards every Intent as a push Event. The
// gateway opens one long-lived Subscribe at startup and fans the events out to
// its clients.
package pushgrpc
import (
"context"
"fmt"
"net"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.uber.org/zap"
"google.golang.org/grpc"
"scrabble/backend/internal/notify"
pushv1 "scrabble/pkg/proto/push/v1"
)
// Service implements pushv1.PushServer over a notify.Hub.
type Service struct {
pushv1.UnimplementedPushServer
hub *notify.Hub
log *zap.Logger
}
// NewService constructs a Service that streams the hub's intents.
func NewService(hub *notify.Hub, log *zap.Logger) *Service {
if log == nil {
log = zap.NewNop()
}
return &Service{hub: hub, log: log}
}
// Subscribe opens a hub subscription and forwards every intent to the gateway
// until the stream's context ends (the gateway disconnected or the server is
// shutting down). It returns nil on a clean disconnect.
func (s *Service) Subscribe(req *pushv1.SubscribeRequest, stream grpc.ServerStreamingServer[pushv1.Event]) error {
ch, cancel := s.hub.Subscribe()
defer cancel()
s.log.Info("gateway push subscription opened", zap.String("gateway_id", req.GetGatewayId()))
defer s.log.Info("gateway push subscription closed", zap.String("gateway_id", req.GetGatewayId()))
ctx := stream.Context()
for {
select {
case <-ctx.Done():
return nil
case in, ok := <-ch:
if !ok {
return nil
}
ev := &pushv1.Event{
UserId: in.UserID.String(),
Kind: in.Kind,
Payload: in.Payload,
EventId: in.EventID,
}
if err := stream.Send(ev); err != nil {
return err
}
}
}
}
// Server wraps the gRPC listener serving the Push service. Its Run mirrors the
// HTTP server's: serve until the context is cancelled, then stop gracefully.
type Server struct {
grpc *grpc.Server
addr string
log *zap.Logger
}
// NewServer builds a gRPC server bound to addr that streams hub events.
func NewServer(addr string, hub *notify.Hub, log *zap.Logger) *Server {
if log == nil {
log = zap.NewNop()
}
gs := grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler()))
pushv1.RegisterPushServer(gs, NewService(hub, log))
return &Server{grpc: gs, addr: addr, log: log}
}
// Run starts the listener and blocks until ctx is cancelled, then stops the
// server gracefully. It returns the first error that is not a clean shutdown.
func (s *Server) Run(ctx context.Context) error {
lis, err := net.Listen("tcp", s.addr)
if err != nil {
return fmt.Errorf("pushgrpc: listen %s: %w", s.addr, err)
}
errc := make(chan error, 1)
go func() {
s.log.Info("push grpc listener starting", zap.String("addr", s.addr))
errc <- s.grpc.Serve(lis)
}()
select {
case err := <-errc:
return err
case <-ctx.Done():
s.log.Info("push grpc listener stopping")
s.grpc.GracefulStop()
return nil
}
}