fix: emails sending despite online status

Closes #140
This commit is contained in:
talksik
2026-04-09 16:30:07 -07:00
parent 66aec4478f
commit 762c0ccf6e
7 changed files with 432 additions and 123 deletions
+7
View File
@@ -38,6 +38,13 @@ func (a *Authorizer) Authorize(ctx context.Context, channelID, humanID string) e
return a.authorizeNetwork(ctx, rest, humanID)
case "stream":
return a.authorizeStream(ctx, rest, humanID)
case "_presence":
// Always allowed — used for global online presence tracking.
// The channel ID is _presence:{humanId}, so verify the humanId matches.
if rest != humanID {
return ErrUnauthorized
}
return nil
default:
return ErrUnauthorized
}
+5
View File
@@ -231,6 +231,11 @@ func (h *Hub) handleRemoteEvent(evt *remoteEvent) {
}
}
// Subscribe enqueues a subscribe request for the given connection and channel.
func (h *Hub) Subscribe(conn *Conn, channelID string) {
h.subscribeCh <- &subscribeRequest{conn: conn, channelID: channelID}
}
// disconnect sends a connection to the disconnect channel.
func (h *Hub) disconnect(conn *Conn) {
h.disconnectCh <- conn
+30 -17
View File
@@ -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 {