@@ -62,6 +62,9 @@ func (s *Server) HandleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithCancel(r.Context())
|
||||
defer cancel()
|
||||
|
||||
// Auto-subscribe to presence channel so this user appears online
|
||||
s.hub.Subscribe(conn, "_presence:"+session.HumanId)
|
||||
|
||||
// WritePump runs in a separate goroutine
|
||||
go conn.WritePump(ctx)
|
||||
|
||||
@@ -71,29 +74,39 @@ func (s *Server) HandleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
slog.Info("websocket disconnected", "connId", connID, "humanId", session.HumanId)
|
||||
}
|
||||
|
||||
// BulkGetPresence implements the gRPC PusherService.
|
||||
// When channel_ids is empty, returns all connected humanIDs across all channels
|
||||
// under the key "_all" — useful for checking overall online status.
|
||||
func (s *Server) BulkGetPresence(ctx context.Context, req *pbpusher.BulkGetPresenceRequest) (*pbpusher.BulkGetPresenceResponse, error) {
|
||||
// Empty channel_ids → return all connected humans
|
||||
if len(req.ChannelIds) == 0 {
|
||||
humanIDs, err := s.bridge.GetAllConnectedHumanIDs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbpusher.BulkGetPresenceResponse{
|
||||
Presences: map[string]*pbpusher.ChannelPresence{
|
||||
"_all": {HumanIds: humanIDs},
|
||||
},
|
||||
}, nil
|
||||
// GetOnlineHumanIds returns all currently connected human IDs.
|
||||
func (s *Server) GetOnlineHumanIds(ctx context.Context, _ *pbpusher.GetOnlineHumanIdsRequest) (*pbpusher.GetOnlineHumanIdsResponse, error) {
|
||||
humanIDs, err := s.bridge.GetAllConnectedHumanIDs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbpusher.GetOnlineHumanIdsResponse{HumanIds: humanIDs}, nil
|
||||
}
|
||||
|
||||
// IsOnline checks whether specific humans are currently online.
|
||||
func (s *Server) IsOnline(ctx context.Context, req *pbpusher.IsOnlineRequest) (*pbpusher.IsOnlineResponse, error) {
|
||||
allOnline, err := s.bridge.GetAllConnectedHumanIDs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
onlineSet := make(map[string]bool, len(allOnline))
|
||||
for _, id := range allOnline {
|
||||
onlineSet[id] = true
|
||||
}
|
||||
result := make(map[string]bool, len(req.HumanIds))
|
||||
for _, id := range req.HumanIds {
|
||||
result[id] = onlineSet[id]
|
||||
}
|
||||
return &pbpusher.IsOnlineResponse{Online: result}, nil
|
||||
}
|
||||
|
||||
// GetChannelPresence returns presence (human IDs) for specific channels.
|
||||
func (s *Server) GetChannelPresence(ctx context.Context, req *pbpusher.GetChannelPresenceRequest) (*pbpusher.GetChannelPresenceResponse, error) {
|
||||
presence, err := s.bridge.GetPresence(ctx, req.ChannelIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &pbpusher.BulkGetPresenceResponse{
|
||||
resp := &pbpusher.GetChannelPresenceResponse{
|
||||
Presences: make(map[string]*pbpusher.ChannelPresence, len(presence)),
|
||||
}
|
||||
for chID, humanIDs := range presence {
|
||||
|
||||
Reference in New Issue
Block a user